user3718908x100
user3718908x100

Reputation: 8509

Add Image To Navigation Drawer Toolbar and Set Custom Background

I need to add an image to the navigation drawer toolbar/actionbar. A small round image that appears on the right of the menu icon and left of the activity title. I also need to change the background of the toolbar/actionbar to my own custom image.

Is this possible and if yes how do I accomplish this in my android app?

Upvotes: 0

Views: 3745

Answers (1)

MikeOscarEcho
MikeOscarEcho

Reputation: 548

For anyone confused, OP is using Android Studio's NavigationView Activity and not an Empty Activity.

Image to the right of the Hamburger menu icon and to the left of the Title is achieved like this:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

if (getSupportActionBar() != null) {
    getSupportActionBar().setDisplayShowTitleEnabled(false); // hide built-in Title

    // Setting background using a drawable
    Drawable toolbarBackground = getResources().getDrawable(R.drawable.ic_menu_gallery);
    getSupportActionBar().setBackgroundDrawable(toolbarBackground);
}

Now that we've hidden the title, we're going to add our own title TextView and icon ImageView to the toolbar, this would be your app_bar_main.xml layout (res/layout).

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/my_custom_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_menu_share"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="My Custom Title"
            android:id="@+id/my_custom_title"
            android:layout_toRightOf="@id/menu_icon"/>
    </RelativeLayout>
</android.support.v7.widget.Toolbar>

Now, you can reference my_custom_title and my_custom_icon and add whatever title and icon you want (you may have to tinker with the layout, I'm just pointing you in the right direction).

TextView myCustomTitleTV = (TextView) findViewById(R.id.my_custom_title);
myCustomTitleTV.setText("My Custom Title");

Upvotes: 1

Related Questions