Wednesday, 6 June 2012

Android Action Bar Demo



Hello Friends,

Today , I am going to explore a sample application in android ,which gives you
an idea , How to create Action Bar in Android.

Hope this will helps you.......
Enjoy Coding.. )


The Screen shot for these application is as below 

1. Home Screen:
android action bar

2. Information and Searh screen , on click of Search | Info  Icon:


action bar example




















Code for Android Action Bar:

1. AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mukesh.actionbar"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/Theme.D1" >
        <activity
            android:name=".HomeActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.D1" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.mukesh.actionbar.SearchActivity"
            android:label="@string/title_search"
            android:theme="@style/Theme.D1" />
    </application>
</manifest>

2. HomeActivity.java



package com.mukesh.actionbar;


import android.os.Bundle;

public class HomeActivity extends ActionBarAppActivity
{
public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


3.  SearchActivity.java



package com.mukesh.actionbar;


import android.os.Bundle;

public class SearchActivity extends ActionBarAppActivity
{
public  void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
     }
}

4.ActioBarAppActivity.java



package com.mukesh.actionbar;


import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Toast;




public abstract class ActionBarAppActivity extends Activity 

{

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) 

    {

        super.onCreate(savedInstanceState);

        //setContentView(R.layout.main);

    }

    

    public void onAbout(View v)

    {

     Toast.makeText (getApplicationContext(),"This is a Sample App that displays use of Action Bar" , Toast.LENGTH_LONG).show ();

    }

    

    public void onSearch(View v)

    {

     startActivity (new Intent(getApplicationContext(), SearchActivity.class));

    }

    

    public void onHome (View v)

    {

     return2Home(this);

    }

    

    public void return2Home(Context context)

    {

        final Intent intent = new Intent(context, HomeActivity.class);

        intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);

        context.startActivity (intent);

    }

}



Download full Source code: ActionBar.zip

Mukesh Yadav

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developer Blog.

Blog Widget by LinkWithin