Wednesday, September 12, 2012

How To Implement Action Bar In Your Application


The project describes How to implement Action Bar for your application. The Action Bar is a widget for activities that replaces the traditional title bar at the top of the screen. By default, the Action Bar includes the application logo on the left side, followed by the activity title, and any available items from the Options Menu on the right side.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project ActionBarExample
2.) Create and open the res/layout/main.xml file and insert the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:background="#81BEF7"

        android:scrollbars="vertical">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/buttonlayout" android:orientation="horizontal"
                android:layout_width="fill_parent"android:layout_height="wrap_content"
                android:height="32dp" android:gravity="left|top"android:background="#2B60DE"
                android:paddingTop="2dp" android:paddingBottom="2dp">
                <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/buttonlayout2"android:orientation="horizontal"
                        android:layout_height="wrap_content"android:gravity="left|center_vertical"
                        android:layout_gravity="left"android:layout_width="wrap_content">
                        <ImageButton android:id="@+id/imgbtn1"
                                android:layout_width="32dp"android:layout_height="32dp"
                                android:background="@drawable/icon" />
                        <TextView android:id="@+id/txtTest"android:layout_width="fill_parent"
                                android:layout_height="fill_parent"android:textStyle="bold"
                                android:text="SimpleActionBar"android:textColor="#FFFFFF"
                                android:textSize="15sp"android:gravity="center_vertical"
                                android:paddingLeft="5dp" />
                        <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
                                android:id="@+id/buttonlayout2"android:orientation="horizontal"
                                android:layout_height="wrap_content"android:gravity="right"
                                android:layout_gravity="right"android:layout_width="fill_parent">
                                <ImageButton android:id="@+id/imgbtn2"
                                        android:layout_width="32dp"android:layout_height="32dp"
                                        android:background="@drawable/ea" />
                                <ImageButton android:id="@+id/imgbtn3"
                                        android:layout_width="32dp"android:layout_height="32dp"
                                        android:background="@drawable/icon" />
                        </LinearLayout>
                </LinearLayout>
                <TableLayout android:id="@+id/TableLayoutTop"
                        android:layout_gravity="left"android:scrollbars="horizontal|vertical"
                        android:scrollbarAlwaysDrawVerticalTrack="true"
                        android:scrollbarAlwaysDrawHorizontalTrack="true"
                        android:layout_height="wrap_content"android:layout_width="fill_parent"
                        android:orientation="horizontal">
                        <TableRow android:id="@+id/TableRowTop"
                                android:layout_height="wrap_content"android:layout_width="match_parent" />
                </TableLayout>
        </LinearLayout>
</LinearLayout>
3.) Make sure to add following in your manifest file:
<application android:icon="@drawable/icon" android:label="@string/app_name"
                android:theme="@android:style/Theme.NoTitleBar">
                <activity android:name=".ActionBarExample"android:label="@string/app_name">
                        <intent-filter>
                                <actionandroid:name="android.intent.action.MAIN"/>
                                <categoryandroid:name="android.intent.category.LAUNCHER"/>
                        </intent-filter>
                </activity>
        </application>
“android:theme=”@android:style/Theme.NoTitleBar” will enable application to popup as action bar.
4.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. ActionBarExample Enter following information:
Project name: ActionBarExample
Build Target: Android 2.3.3
Application name: ActionBarExample
Package name: org.example. ActionBarExample
Create Activity: ActionBarExample
On Clicking Finish ActionBarExample code structure is generated with the necessary Android Packages being imported along with ActionBarExample.java. Following code must be added in ActionBarExample class to get the menu to work.
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
Output –The final output:

No comments:

Post a Comment