Set app icon to the right in the activity toolbar

I work with the AppCompact library and run into some layout / positioning issues. I want to put the application icon on the right side of the "ActionBar". One way is to define a button on the toolbar, but is there a standard method for setting the application icon and the Up button on the right side of the ActionBar?

enter image description here

As you can see in the above image, the icon is located on the left, I want it to be on the right. Any help would be appreciated.

Ps: For people who may run into my problem, you can easily install this code Add this code to the manifest:

<application android:supportsRtl="true"> 

and then write this code in Oncreate:

 getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 
+6
source share
2 answers

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.

+13
source

There is no other way than to install the RTL layout or implement it yourself. Even if you subclass Toolbar and try to change onLayout , you will need to recalculate the position of all kinds.

+1
source

Source: https://habr.com/ru/post/982203/


All Articles