Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set app icon to right side in activity tool bar

Tags:

android

I am working with the 'AppCompact' library and faced some problems with layout/positioning. I want to position the App icon on the right hand side of the 'ActionBar'. One method is to define a button in the toolbar, but is there a standard method to set the App icon and Up button on the right hand side of the ActionBar?

enter image description here

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

P.s:For people who may face my problem it can be fixed easily using this code Add this code to manifest:

<application android:supportsRtl="true">

and then write this code on Oncreate:

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
like image 468
Majid Hojati Avatar asked Feb 09 '15 11:02

Majid Hojati


People also ask

How to set icons to the right of the toolbar?

Toolbar with menus works fine, it sets icons to the right of toolbar automatically If there's anyone that still confused (including me before), somehow android studio doesn't provide autocomplete for the android:layout_gravity under the toolbar tag. So to make it works, just copy and paste android:layout_gravity="end" to the ImageView /layout.

How to move app icons in the middle of the taskbar?

Using the native taskbar settings, one can easily move the app icons in the middle. You need to make sure that the taskbar is not locked in your system. Follow the steps below. Step 1: Hover the mouse pointer on the taskbar and right-click on it. Step 2: Disable Lock the taskbar option.

Where can I place the toolbar in an activity?

Developers can place it anywhere in the activity according to the need just like any other View in android. Toolbar use material design theme features of Android and thus it provides backward compatibility up to API 7 (Android 2.1). One can use the Toolbar in the following two ways:

How do I change the toolbar ID of an activity?

To make the desired change, the ID of the Toolbar must be passed as an argument in the setSupportActionBar () method written inside the MainActivity file. Below is the code to do the same. Each and every component/element that will be displayed on the Toolbar is customizable.


1 Answers

There is no way android provides to set app icon at right side of actionbar but you can still do that.

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 in MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu){

    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

It's done! Your app icon will be visible on right side of ActionBar now.

If you have more than one item in menu then override onPrepareOptionsMenu in activity class and set setEnabled(false) for menu item having app icon, doing this prevents your icon to be clickable.

@Override
public boolean onPrepareOptionsMenu(Menu menu){
    menu.findItem(R.id.menu_item).setEnabled(false);

    return super.onPrepareOptionsMenu(menu);
}

Now your MainActivity.java file would 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 app icon on right side.

like image 130
Apurva Avatar answered Oct 31 '22 20:10

Apurva