There is no way that Android sets the application icon on the right side of the action bar , but you can still do it .
Create a menu, say main_menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_item" android:icon="@drawable/your_app_icon" android:title="@string/menu_item" app:showAsAction="always"/> //set showAsAction always //and this should be the only menu item with show as action always </menu>
Now just override onCreateOptionsMenu in your activity class.
add this to MainActivity.java
@Override public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater().inflate(R.menu.main_menu, menu); return super.onCreateOptionsMenu(menu); }
Done! Your application icon will now be visible on the right side of the ActionBar.
If you have several items in the menu , then override onPrepareOptionsMenu in the activity class and set setEnabled(false) for the menu item that has the application icon, this will prevent the icon from being clicked.
@Override public boolean onPrepareOptionsMenu(Menu menu){ menu.findItem(R.id.menu_item).setEnabled(false); return super.onPrepareOptionsMenu(menu); }
Now your MainActivity.java file will look like
@Override public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater().inflate(R.menu.main_menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item){ switch(item.getItemId()){ case R.id.menu_item: //this item has your app icon return true; case R.id.menu_item2: //other menu items if you have any //add any action here return true; case ... //do for all other menu items default: return super.onOptionsItemSelected(item); } } @Override public boolean onPrepareOptionsMenu(Menu menu){ menu.findItem(R.id.menu_item).setEnabled(false); return super.onPrepareOptionsMenu(menu); }
This is the only trick you can use to set the application icon on the right side.
source share