Search This Blog

Sunday 1 July 2012

Simple Code for Action Bar using XML Layout

This post is explaining the example for how to create a top action bar for a application using the simple XML layout.

Create a android project in your eclipse editor and open the main.xml (layout file located in project res directory).

Add the following XML layout

<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">
                <LinearLayout xmlns: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/ic_launcher" />
                        <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" />
                        <LinearLayout xmlns: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/ic_launcher" />
                                <ImageButton android:id="@+id/imgbtn3"
                                        android:layout_width="32dp" android:layout_height="32dp"
                                        android:background="@drawable/ic_launcher" />
                        </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>
  <TextView
        android:text="CreativeAndroidApps - This is a GridView Example"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 <GridView
    android:id="@+id/grid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="2"
    android:columnWidth="100px"
    android:stretchMode="columnWidth"
    android:gravity="center"/>
</LinearLayout>


See the below code for your activity class

package com.example.creativeapps;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;

public class CreativelayoutsActivity extends Activity 
{
     GridView myGrid;
     String[] listContent = {
                "January",
                "February",
                 "March",
                 "April",
                 "May",
                 "June",
                 "July",
                 "August",
                 "September",
                 "October",
                 "November",
                 "December"
     };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        myGrid = (GridView)findViewById(R.id.grid);
        //create the array adapter for the grid view  
        ArrayAdapter<String> adapter
         = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1,
          listContent);
          myGrid.setAdapter(adapter);
     }
 
}

Now run the application and see the following output with action bar.

Action Bar Example
Simple Action Bar


No comments:

Post a Comment