i have problem with styling Custom CheckBox Menu Item ActionBar. I have implemented, but It's not styled.
I want to changed with resource this
if checkbox is checked
if checkbox is not checked
this is my menu item Action Bar.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.tracking.bus.maps.SingleViewMapsActivity" >
<item
android:id="@+id/actionbar_alarm"
android:title="@string/actionbar_alarm"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/actionbar_interval"
android:title="@string/actionbar_interval"
app:actionViewClass="android.widget.Spinner"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/actionbar_log_track"
android:title="@string/actionbar_log_track"
app:showAsAction="never"/>
</menu>
style Action Bar
<style name="Theme.Myab" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarItemBackground">@drawable/selectable_background_myab</item>
<item name="popupMenuStyle">@style/PopupMenu.Myab</item>
<item name="dropDownListViewStyle">@style/DropDownListView.Myab</item>
<item name="actionBarTabStyle">@style/ActionBarTabStyle.Myab</item>
<item name="actionDropDownStyle">@style/DropDownNav.Myab</item>
<item name="actionBarStyle">@style/ActionBar.Solid.Myab</item>
<item name="actionModeBackground">@drawable/cab_background_top_myab</item>
<item name="actionModeSplitBackground">@drawable/cab_background_bottom_myab</item>
<item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Myab</item>
<item name="spinnerDropDownItemStyle">@style/DropDownItem.Myab</item>
<item name="android:spinnerItemStyle">@style/SpinnerItem.Myab</item>
<item name="android:checkboxStyle">@style/customCheckBoxStyle.Myab</item>
<item name="actionBarWidgetTheme">@style/Theme.Myab.Widget</item>
</style>
<!-- style checkbox -->
<style name="customCheckBoxStyle.Myab" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/custom_checkbox_design</item>
</style>
custom_checkbox_design.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/alram_on" />
<item android:state_checked="false" android:drawable="@drawable/alram_off" />
</selector>
so how to solve it ? thanks sorry for my english
First things, android:checkboxStyle
should run on device with API 11 or later.
Try set your checkbox selector like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="300">
<item android:state_selected="true" android:drawable="@drawable/checked"></item>
<item android:state_checked="true" android:drawable="@drawable/checked"></item>
<item android:drawable="@drawable/unchecked"></item> <!--default state, unchecked-->
</selector>
I got same issue, I have figured this out tricky way. See below:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.webview_menu, menu);
menu.getItem(0).setIcon(R.drawable.unfavorite);
favorite = false;
return true; }
On optionitemselected:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.favoriteItem:
if(favourite){
item.setIcon(R.drawable.unfavorite);
favourite = false;
} else {
item.setIcon(R.drawable.favorite);
favourite = true;
}
new FavoriteTask(favourite).execute();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This was easiest way to implement the same.
In the java file, you have to set the icon programmatically. If your selector file and menu files are correct, the change of the state of the checkbox will be reflected on the checkbox icon automatically.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu,menu);//insert the name of the menu file
CheckBox checkBox = (CheckBox)menu.findItem(R.id.menu_item).getActionView();//find the checkbox, in your case R.id.actionbar_alarm
checkBox.setButtonDrawable(R.drawable.star);//the name of your selector file custom_checkbox_design.xml in your case
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//add the action of your checkbox
}
});
return super.onCreateOptionsMenu(menu);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.