Search code examples
androidandroid-actionbar

Android Profile image in actionbar


How can I put the profile image inside the default ActionBar?

My app, for while, is like the image bellow:

Actions bar

In the image above, the first Actionbar is the default and the second bar (with image and Profile's name) is the one I have created.

I would like to use the default ActionBar with image inside it, as shown in image bellow:

ActionBar with image

Maybe I need to create a custom Actionbar by myself, but if have a way to do with the default ActionBar, I will try.


Solution

  • You can use custom action bar

    actionbar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/profile_icon" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="APP_Name" />
    </LinearLayout>
    

    Add following lines in Activity.java

    Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.actionbar);
    

    OR

    You can use following code to set icon on action bar

    setTitle("title");
    getActionBar().setIcon(R.drawable.profile_icon);
    

    OR

    If you have constant logo then add following code in manifest file

    For single activity

    <activity android:name=".MyActivity" 
           android:icon="@drawable/profile_icon" 
           android:label="title" />  
    

    For all activities

    <application
        android:logo="@drawable/profile_icon">
    
        ...
    
    </application>