2

Please, let me know, how to add buttons to the title bar.

  • Remove the title completely and create a custom title with an ImageView for example.

or

  • Use another approach

My Goal is something similar to the Facebook app enter image description here

UPDATE: API Level >= 10

Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
Shlomi Schwartz
  • 8,693
  • 29
  • 109
  • 186

2 Answers2

0

My solution using custom ViewGroup (from RelativeLayout) so I can use it in different activities without copy paste xml. This is a simple example that update a text view with the title. You can add buttons to relative layout ang get them work in the way you need.

But if you've only an Activity it's better if you include it as a sub layout of the main layout of your activity

public class StatusBar extends RelativeLayout {

private TextView textViewTitle;

    public StatusBar(Context context, AttributeSet attrs) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(R.layout.ui_status_bar, this, true);
        super(context, attrs);
}

@Override
    public void onFinishInflate() {
        super.onFinishInflate();

        textViewTitle = (TextView) findViewById(R.id.textViewTitle);
}

private void updateStatusBar(String title) {
this.textViewTitle.setText(title)
}
}

And the ui_status_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/theme_color_darker"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

</RelativeLayout>

in your activity you can use it like this:

<com.uicomponents.statusbar.StatusBar
    android:id="@+id/statusBar"
    style="@style/status_bar" />

Seraphim's
  • 12,559
  • 20
  • 88
  • 129
  • Thanks for the reply. Just to make it clear:R.layout.ui_status_bar is a reference to the XML? can you elaborate a bit? – Shlomi Schwartz May 28 '13 at 12:48
  • It's hard to explain if you have no experience with custm View and viewGroups. layout.ui_status_bar.xml goes into layout/ folder in the Android project. when you compile, that layour can be loaded by its reference from the resources (i.e. R.layout.ui_status_bar) like you do ehn loading layout for the activity. – Seraphim's May 28 '13 at 12:50
0

I think, it's best practice to use Android's Actionbar. Prefer action menu instead of buttons in custom title bar.

Please see : There's given refresh and edit action menu

And for the further implementation of actionbar please refer :

  1. http://www.vogella.com/articles/AndroidActionBar/article.html
  2. developer.android.com/guide/topics/ui/actionbar.html‎
Prat
  • 143
  • 3
  • 17
  • Thanks for the reply, I've updated my question ... API level 10 is my minimum version. – Shlomi Schwartz May 28 '13 at 13:02
  • Yeah..! There's option to give support from API level 8, you can use SherlockActionbar library.. Please download library from : http://actionbarsherlock.com/ – Prat May 28 '13 at 13:11