---
title: "Action Bar in Android"  
description: "In this article, we will learn how to work with and add items in Action bar."  
author: "Manoj Pandey"  
published: 2014-03-26  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1386/action-bar-in-android  
category: "android"  
tags: ["android"]  
reading_time: 7 minutes  

---

# Action Bar in Android

In this article, we will learn how to work with and [add items](https://www.mindstick.com/forum/160381/how-to-add-items-to-a-list-collection-in-c-sharp) in Action bar.The action bar is a window feature that identifies the [user location](https://www.mindstick.com/forum/886/launching-jquery-plugins-depending-on-the-user-location), and provides user actions and [navigation](https://www.mindstick.com/blog/62/different-navigation-in-asp-dot-net) modes.

Please follow the steps which are given below and you can use this example for [learning](https://yourviews.mindstick.com/view/88279/my-journey-learning-content-writing-at-mindstick-training-from-experienced-content-writers) the [concepts](https://www.mindstick.com/articles/13127/macro-and-microeconomics-concepts-in-relation-to-global-organization-management) of Action bar.

· Open Eclipse

· Select New [Android Application](https://www.mindstick.com/articles/1653/creating-libraries-for-android-applications) Project

· Enter Application Name as ActionBarSample

· Click Next

· Click Finish.

**Now go to res/menu folder and main xml and create [xml file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp) and user (as shown below)**

![Action Bar in Android](https://www.mindstick.com/mindstickarticle/6b870543-372e-4ca5-9ae3-a103ff82e7c0/images/d887f66d-4e04-4b14-a220-0605b55f706e.png)

Add Items in user.xml file

![Action Bar in Android](https://www.mindstick.com/mindstickarticle/6b870543-372e-4ca5-9ae3-a103ff82e7c0/images/8cfe3a40-b135-42df-b565-5ec33cd41ed5.png)

```
<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](https://www.mindstick.com/forum/23119/how-to-save-activity-state-in-android) **user_acivity.**

![Action Bar in Android](https://www.mindstick.com/mindstickarticle/6b870543-372e-4ca5-9ae3-a103ff82e7c0/images/6936b4de-cd38-4a58-936c-598c8389c93e.png)

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](https://www.mindstick.com/mindstickarticle/6b870543-372e-4ca5-9ae3-a103ff82e7c0/images/eb4a1477-c8d2-42d3-a090-bfd74ba32682.png)

On **OK** Click call the Next Activity.

![Action Bar in Android](https://www.mindstick.com/mindstickarticle/6b870543-372e-4ca5-9ae3-a103ff82e7c0/images/45933e8f-50c0-48cf-917d-eb2fead5c3c8.png)

Click in Action bar back icon

Back in main activity.

![Action Bar in Android](https://www.mindstick.com/mindstickarticle/6b870543-372e-4ca5-9ae3-a103ff82e7c0/images/1e04ba80-d212-49cd-8027-5f99c7250ff3.png)

Thanks for reading this article. You can enter [your valuable](https://www.mindstick.com/articles/23598/best-wedding-photographer-inspire-the-best-to-protect-your-valuable-minutes) comments and suggestion to improve this article in the comment box.

---

Original Source: https://www.mindstick.com/articles/1386/action-bar-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
