问题
I want change the background drawable of a button. My min. SDK version is 14. Therefore I check the current version, which is installed on the device:
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
buttonClubPublic.setBackgroundDrawable(buttonClubPublic.getContext().getResources().getDrawable(R.drawable.check_button));
buttonClubPrivate.setBackgroundDrawable(buttonClubPrivate.getContext().getResources().getDrawable(R.drawable.uncheck_button));
} else {
buttonClubPublic.setBackground(buttonClubPublic.getContext().getResources().getDrawable(R.drawable.check_button));
buttonClubPrivate.setBackground(buttonClubPrivate.getContext().getResources().getDrawable(R.drawable.uncheck_button));
}
When the current version is equal or above 16 I use setBackground
otherwise I use setBackgroundDrawable
.
However, it seems that Eclipse doesn't recognize the if/else check of the version, because I get an error:
Error: Call requires API level 16 (current min is 14)
I set the button in XML:
<Button
android:id="@+id/create_club3_club_privat"
android:text="@string/create_club3_privat"
android:textColor="#000000"
android:textSize="@dimen/check_button_size"
android:layout_width="@dimen/check_button_width"
android:layout_height="wrap_content"
android:background="@drawable/uncheck_button"
android:layout_marginLeft="@dimen/create_team_check_button_margin"
/>
来源:https://stackoverflow.com/questions/26549550/android-setbackground-with-checking-android-version-not-working