Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an actionbar in android studio for beginners?

I want to build my own app and i want to add an action bar to it.In my main layout i want an action bar with my app name only.In my other layouts/pages i want my page name and a back navigating symbol which navigates back to the previous page.Can anybody tell me how to do this and also can somebody tell me about the default actionbar in android what it does and what do i do with it.

like image 370
chanaka Avatar asked Aug 20 '16 08:08

chanaka


People also ask

What is ActionBar in Android Studio?

The app bar, also known as the action bar, is one of the most important design elements in your app's activities, because it provides a visual structure and interactive elements that are familiar to users.


2 Answers

STEP 1 : build.gradle (Application Level)

dependencies {
    compile 'com.android.support:appcompat-v7:23.3.0'
}

STEP 2 : AndroidManifest.xml

<application
....
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"/>

STEP 3 : In your activity

public class main extends AppCompatActivity
{
    ....
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ...

        getSupportActionBar().setTitle("Your Activity Title"); // for set actionbar title
        getSupportActionBar().setDisplayHomeAsUpEnabled(true); // for add back arrow in action bar
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    int id = item.getItemId();
    if (id == android.R.id.home) {
        finish();
    }
    return super.onOptionsItemSelected(item);
}
like image 65
Kalpesh Tarsariya Avatar answered Oct 25 '22 07:10

Kalpesh Tarsariya


Change your layout theme to "Theme.DeviceDefault.Light.DarkActionBar"

Make action bar menu layout under menu folder

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/a"
      android:icon="@drawable/a"
      android:showAsAction="always"
      android:title=""/>

<item android:id="@+id/b"
      android:icon="@drawable/b"
      android:showAsAction="always"
      android:title=""/>

<item android:id="@+id/c"
      android:icon="@drawable/c"
      android:showAsAction="always"
      android:title=""/>

<item android:id="@+id/d"
      android:icon="@drawable/d"
      android:showAsAction="always"
      android:title=""/>

<item android:id="@+id/e"
      android:icon="@drawable/e"
      android:showAsAction="always"
      android:title=""/>

Then write the following script to your activity class

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.actionbar_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.a:
            //Write your code
            return true;
        case R.id.b:
            //Write your code
            return true;
        case R.id.c:
            //Write your code
            return true;
        case R.id.d:
            //Write your code
            return true;
        case R.id.e:
            //Write your code
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
like image 37
Sabish.M Avatar answered Oct 25 '22 06:10

Sabish.M