Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to show title and custom view on action bar at same time?

Tags:

android

To put a right-aligned spinner on action bar, I have to use a custom view with relative layout. But after I put custom view on the action bar, I find that app title disappear, it must be impacted by putting custom view on action bar.

Is it possible to display both title and custom view on action bar? Otherwise I have to put title in customer view as well, but I don't want do go that way.

like image 246
TieDad Avatar asked Sep 22 '13 03:09

TieDad


2 Answers

The custom view will replace the Title bar. I suggest you to include a textview for the app title in your custom view so that you have more ground to play.

like image 175
prijupaul Avatar answered Nov 12 '22 01:11

prijupaul


Actually it doesn't "replace". This can be shown by the hierarchy viewer. it's just that if you set your custom view too big, it will be ON TOP OF the title. however, if you only put something like a spinner, you can set it to be right aligned and the title will be shown.

Here's XML I used to put an image view on the right:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="68dp"
android:layout_height="31dp"
android:padding="4dp"
android:layout_gravity="center_vertical|right"
android:layout_marginRight="4dp"
android:onClick="onImageClicked"
android:background="@drawable/mypic_bg"
android:src="@drawable/logo_mypic" />

and in code:

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
            | ActionBar.DISPLAY_HOME_AS_UP 
            | ActionBar.DISPLAY_SHOW_HOME 
            | ActionBar.DISPLAY_SHOW_TITLE);
    actionBar.setCustomView(R.layout.my_title_bar);
like image 30
Shawn Avatar answered Nov 12 '22 02:11

Shawn