Home > DeveloperSection > Articles > Action Bar in Android

Action Bar in Android


Android Android 
Ratings:
0 Comment(s)
 2284  View(s)
Rate this:

  Action Bar in Android

                                                                                                                                           

In this article, we will learn how to work with and add items in Action bar.

The action bar is a window feature that identifies the user location, and provides user actions and navigation modes.

 

Please follow the steps which are given below and you can use this example for learning the concepts of Action bar.

 

·         Open Eclipse

·         Select New Android Application Project

·         Enter Application Name as ActionBarSample

·         Click Next

·         Click Finish.

 

Now go to res/menu folder and main xml and create xml file and user (as shown below)

 

Action Bar in Android

Add Items in user.xml file

 

Action Bar in Android

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

 

    <item

        android:id="@+id/action_settings"

        android:orderInCategory="100"

        android:showAsAction="never"

        android:title="@string/action_settings"/>

                <item

                  android:id="@+id/backicon"

        android:orderInCategory="1" 

        android:icon="@drawable/back_icon"

 

          <!-- back _icon is png file which store in res/drawable-hdpi

             /back_icon.png (It is use for make an icon for back) -->     

   

        android:showAsAction="always"/>

                  

    </item>

</menu>    

               

 

 

 

Create a new activity user_acivity.

 

Action Bar in Android

 

Now add the user activity in AndroidMainFest.xml

 

<activity

            android:name="com.example.actionbarsample.User_Activity">

              </activity>

 

Now in res/Layout/activity_main.xml add following controls

 

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <EditText

        android:id="@+id/txt_UserName"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignBaseline="@+id/textView1"

        android:layout_alignBottom="@+id/textView1"

        android:layout_alignParentRight="true"

        android:layout_toRightOf="@+id/textView1"

        android:ems="10" />

 

    <Button

        android:id="@+id/btn_ok"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/txt_UserName"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="16dp"

        android:onClick="Senddata"

        android:text="OK" />

 

 

 

 

    <TextView

        android:id="@+id/tv_EnterName"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginTop="104dp"

        android:text="Enter the Name-: "

        android:textSize="20dp" />

   

</RelativeLayout>

 

 

 

Now write the following code in MainActivity .class

 

package com.example.actionbarsample;

public class MainActivity extends Activity {

 

 

                EditText txt_User;

                Button btn_ok;

                @Override

                protected void onCreate(Bundle savedInstanceState) {

                                super.onCreate(savedInstanceState);

                                setContentView(R.layout.activity_main);

                                txt_User=(EditText)findViewById(R.id.txt_UserName);

                                btn_ok=(Button)findViewById(R.id.btn_ok);                                  

                }

 

                @Override

                public boolean onCreateOptionsMenu(Menu menu) {

                                // Inflate the menu; this adds items to the action bar if it is present.

                                getMenuInflater().inflate(R.menu.main, menu);

                                return true;

                }

                public void Senddata(View v)

                {

                     String _sUser=edUser.getText().toString();

//An intent is an abstract description of an operation to be performed.

Intenet use for to launch a activity and start the activity.

                     Intent intent=new Intent(MainActivity.this,User_Activity.class); 

                     intent.putExtra("User",_sUser);

 

//Intent also use for to transfer the value from one activity to another activity. putExtra method is Store the value in intent  which is specify by “User”.

                     startActivity(i);                         

                }

 

}

 

And in res/Layout/user_activity.xml add following controls

 

 

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content" >

    <TextView

            android:id="@+id/tv_ShowWelcome"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:textSize="20dp"

            android:text="Welcome -: " />

 

        <TextView

            android:id="@+id/tv_sShowName"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:textSize="20dp"

            android:text="TextView" />

 

    </LinearLayout>

 

</LinearLayout>

 

 

 

Now write the following code in user_activity.class  

 

package com.example.actionbarsample;

 

public class User_Activity  extends Activity {

                String _sUser;

                TextView tv_Username;

                @SuppressLint("NewApi")

                @Override

 

                protected void onCreate(Bundle savedInstanceState) {

                                super.onCreate(savedInstanceState);

                                setContentView(R.layout.user_activity);

                                tv_Username=(TextView)findViewById(R.id. tv_sShowName);

                                user=(String) getIntent().getExtras().get("User");// Get the data from Intent class which is specify by “User” .

                                edUsername.setText(_sUser);                                                 

                                }             

                @Override

                public boolean onCreateOptionsMenu(Menu menu) {

                                // Inflate the menu; this adds items to the action bar if it is present.

                                getMenuInflater().inflate(R.menu.user, menu);                            

                                return true;

}

                public boolean onOptionsItemSelected(MenuItem item) {

                               

                                Intent intent=new Intent(User_Activity.this,MainActivity.class);

                                startActivity(intent);                   

                                return true;

                }

               

 

}

 

Now Run As Android Application.

Enter the name and click OK.

 

Action Bar in Android

 

On OK Click call the Next Activity.

 

Action Bar in Android

 

Click in Action bar back icon 

Back in main activity.

 

Action Bar in Android

 

Thanks for reading this article. You can enter your valuable comments and suggestion to improve this article in the comment box.

 

 


Don't want to miss updates? Please click the below button!

Follow MindStick