Wednesday, October 9, 2013

Add custom title on ActionBar in android

title_latout.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">

    <Button android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:textSize="11sp"
        android:id="@+id/logout_btn"
        android:text="Logout" />

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title_text"
        android:layout_toRightOf="@+id/logout_btn"
        android:layout_centerInParent="true"
        android:layout_toLeftOf="@+id/setting_btn"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:text="DoMyExpenses"/>

    <ImageButton android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:background="@android:drawable/ic_menu_compass"
        android:id="@+id/setting_btn"/>


</RelativeLayout>



        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setIcon(android.R.color.transparent);
        actionBar.setDisplayShowCustomEnabled(true);
        View customTitle = getLayoutInflater().inflate(R.layout.title_layout, null);
        title_text = (TextView)customTitle.findViewById(R.id.title_text);
        title_text.setText("Saved Expenses");
        actionBar.setCustomView(customTitle);