I am working on this simple Android application. I have three Menu Items on the Action Bar and each of the menu items inflate 3 different fragments when clicked on them.
My Question: When I click on one of the Menu Items, I want that Menu Item's icon changing to another icon in the drawables folder and STAY like that until I click on another Menu Item and then it will change to the first icon before I clicked on it.
I tried to use a selector .xml with "state_pressed" and when I click on the Menu Item it changes the icon for a second but then it goes back to the first icon as soon as I stop clicking. You can find the related codes below. I'd appreciate it if you could help me with this one.
submenu.xml (Action Bar Menu):
<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" >
<item
android:id="@+id/action_todo"
android:title=""
android:icon="@drawable/action_bar_selector"
app:showAsAction="always" />
</menu>
action_bar_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_check_box_white_24dp"
android:state_pressed="true" />
<item
android:drawable="@drawable/ic_check_box_white_24dp_unpressed" />
</selector>
ProjectTodo.java (Inflating Action Bar in Fragment Class):
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.submenu, menu);
}
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.action_back){
ProjectDetails frag = new ProjectDetails();
ProjectMainListFragmentChanger fragInterface = (ProjectMainListFragmentChanger) getActivity();
fragInterface.projectMainListChangeFragment(frag);
return false;
}
if(id == R.id.action_details){
return true;
}
if(id == R.id.action_todo){
ProjectDetails frag = new ProjectDetails();
ProjectTodoFragmentChanger fragInterface = (ProjectTodoFragmentChanger) getActivity();
fragInterface.projectTodoChangeFragment(frag);
return false;
}
return super.onOptionsItemSelected(item);
}
You can see a quick .gif image of the current application. When I click on the Menu Item, it only changes to the 2nd icon for a second and then goes back to its original icon: