0

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.

Jan Pfeifer
  • 2,854
  • 25
  • 35
rioleal
  • 33
  • 1
  • 9
  • see this [answer](https://stackoverflow.com/questions/33300978/how-to-add-image-in-toolbar) may this will help you. – Nilesh Panchal Jan 17 '20 at 11:54
  • As I thought... I have to create my own Action bar. The documentation of the Android Studio tells that, after 21th version, is not a best practice to insert name and icons on action bar, just one or another one, never both. Thanks for all and to all. – rioleal Jan 19 '20 at 10:30

2 Answers2

0

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>
VikaS GuttE
  • 1,636
  • 2
  • 14
  • 38
0

You can use the collapsing toolbar as it will collapse and appear as a single action bar when collapsed. And when dragged down you'll get all the contents inside it.