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.
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.
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);
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With