Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set drawables on actionbar of my android application

I'm developing an application in which I have to put drawables on top of the actionbar and have to update then from time to time. Here is a layout what I want it to look like.

ActionBar Drawable on top

I want to put a drawable as shown in the picture atttached(The green color drawable not the notification thing). Is that possible to achieve this kind of screen in android? If, so please guide me how to achieve this.

Any kind of help will be appreciated.

like image 699
Anupam Avatar asked Oct 21 '22 14:10

Anupam


1 Answers

This is easy using a custom view. Let me explain you the steps briefly:

  1. Create your custom view in the layout folder. For example, an ImageButton.

  2. Set this view as the custom view of the action bar in your activity. You can do this by calling

    getSupportActionBar().setCustomView(R.layout.your_custom_view);
    

    (or getActionBar() instead of getSupportActionBar())

  3. Call setDisplayOptions() with the ActionBar.DISPLAY_SHOW_HOME and ActionBar.DISPLAY_SHOW_CUSTOM options.

  4. If you have other standard buttons in the action bar, inflate the menu.

  5. You can finally set the OnClickListener for your custom view.

This worked for me, hope it helps you!

like image 137
Dhanesh Budhrani Avatar answered Oct 24 '22 09:10

Dhanesh Budhrani