Hi I would like to set a item id for the new icon I set in OnPrepareOptionMenu.
The new icon code is menu.getItem(0).setIcon(R.drawable.bluetooth);
I want to set id for this new icon so that I can used it on the onOptionsItemSelected
.
Here is my Main Activity code
boolean canAddItem = false;
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(canAddItem){
menu.getItem(0).setIcon(R.drawable.bluetooth);
MenuItem mi =menu.add(0,MENU_ADD,Menu.NONE,R.string.bluetooth_on);
mi.setIcon(R.drawable.ic_bluetooth_searching_white_24dp);
mi.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
canAddItem = false;
}else{
menu.getItem(0).setIcon(R.drawable.ic_bluetooth_white_24dp);
canAddItem = true;
}
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
switch (item.getItemId()){
case R.id.Bluetooth_connect:{
invalidateOptionsMenu();
Toast.makeText(getApplicationContext(), "Turned on", Toast.LENGTH_LONG).show();
}
break;
case MENU_ADD: Toast.makeText(getApplicationContext(), "search", Toast.LENGTH_LONG).show();
break;
}//end of switch
//noinspection SimplifiableIfStatement
//return false;
return super.onOptionsItemSelected(item);
}
I trying to make something like when I press the onbutton, the onbutton icon will changed to a new icon + additional icon as shown in newicon image. But now I would like to add action to the new icon. Which mean I pressed the new icon a certain action will be done and the search icon will disappear and go back to the original On icon. So the only thing I can think of is to set a id on the new icon, but I not sure whether is this possible?
menu_main.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="course.examples.healthcare_application.MainActivity">
<!--lower value will be on the left-->
<item
android:id="@+id/Bluetooth_connect"
android:icon="@drawable/ic_bluetooth_white_24dp"
app:showAsAction="always"
android:orderInCategory="1"
android:title="@string/bluetooth_connect" />
</menu>