Reputation:
I have been looking for options on how to place Navigation bar common to all activities. Still can't figure out the best way to do it. The Navigation bar should have a title for screen and a back button. Or may be two in some activities.
What is the best practice I should follow?
Thanks
Upvotes: 5
Views: 10174
Reputation: 17557
You can define the navigation bar in separate header.xml layout like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/header_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 1px border top -->
<ImageView
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="@color/header_border_top" />
<!-- header with text and buttons -->
<RelativeLayout
android:layout_height="43dp"
android:layout_width="fill_parent"
android:background="@color/header_background">
<!-- left side -->
<LinearLayout
android:id="@+id/header_home_button_layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_alignParentLeft="true">
<!-- home button -->
<ImageButton
android:id="@+id/header_home_button"
android:src="@drawable/header_btn_home"
android:onClick="onHomeClick" />
</LinearLayout>
<!-- header text -->
<TextView
android:id="@+id/header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/app_name"
android:textSize="20dp"
android:textStyle="bold"/>
<!-- right side -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<!-- back button -->
<ImageButton
android:id="@+id/header_back_button"
android:src="@drawable/header_btn_back"
android:onClick="onBackClick"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
<!-- 1px border bottom -->
<ImageView
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="@color/header_border_bottom" />
</LinearLayout>
Then include this header in the layout of all your activities:
<include
layout="@layout/header" />
And make sure that all your classes extend parent class that has the methods onHomeClick and onBackClick...
Upvotes: 3
Reputation: 10949
A friend of mine has created an open source project named Android Actionbar, sounds like you want to do what that project does. It's a library project, so you can use it in your application. There are also examples at the site.
This is what it looks like:
Upvotes: 2