Not long ago did Android developers came up with appcompat_v7 library that added Android Action-bar backward compatibility to devices that were running an older versions of Android OS (below Gingerbread). ActionBar has now be replaced by Toolbar since API level 21 (yeah Lollipop 5.0). The toolbar is a part of the Material Design UI that was introduced in L Preview, using appcompat-v7 version 21 you can make use of it to support older devices.

Toolbar is a general version of ActionBars. It can be used at any location of the Android View Layout (Activity), but in this tutorial, we will only see how we can use it as an Actionbar.
Implementing Toolbar using appcompat_v7 version 21Just create a new Android Project in Android Studio IDE as we usually do. Now go to build.gradle and check under dependencies that the compile version of appcompat-v7 is 21.0.0 or higher, this will not work if you do not have support to API level 21 or higher. Once this is done, now go to style.xml file which you would find under res->values->style.xml and add the the following two item tags within AppTheme,
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>
The above tags will remove the TitleBar, but the better way is to change the ActionBar parent theme Theme.AppCompat.Light.DarkActionBar to Theme.AppCompat.Light.NoActionBar
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
</resources>
Now we need to add Toolbar View in our layout.xml file, Toolbar is a part of android.support.v7.widget package of appcompact, Let's create a parent LinearLayout View and add Toolbar under it,
File : layout_android.xmlLanguage : xml<LinearLayout 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"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:background="#637382"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
</LinearLayout>
The last part is to set the Toolbar as our ActionBar we will have to create an object of Toolbar and map its layout element, just as we do for any View element, and within onCreate method pass this object to setSupportActionBar(); function
File : MainActivity.javapackage com.code2care.tools.toolbarexample;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflatfded the menu; this adds items to the action bar if it is present. dsfsadfs
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
- Android : Execute some code after back button is pressed
- Android is starting optimizing... app 1 of 1
- How to change Android Button Color using xml attribute and programatically using java
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- How to make Android EditText not editable
- Make Android TextView Clickable like Buttons
- Unable to establish connection to adb : Android Studio Error
- Android Constant and Resource Type Mismatches Lint
- Android Shared Preferences API tutorial
- Fixing Android unknown error 961 while downloading app
- DDMS files not found hprof-conv.exe
- How to reset eclipse layout
- Android Developers Bluetooth Tutorial
- Android Studio Native typeface cannot be made error
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Multiline EditText in Android Example
- How to screenshot on Android?
- JavaScript : redirect page to other url
- Unable to load VM from snapshot. The snapshot has been saved for a different hardware configuration
- How To Disable Landscape Mode in Android Application
- Change Title text for Android Activity using java code
- Android : Class file collision: A resource exists with a different case
- Android Emulator Soft Back button action using Computer keyboard
- Device not compatible error Android Google Play Store
- ERROR x86 emulation currently requires hardware acceleration. Intel HAXM is not installed on this machine
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- Disable Fading Edges Scroll Effect Android Views
- Android : Unable to load VM from snapshot : Mac OS X Error
- Change Android EditText Cursor Height
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- How to add border to Android TextView
- Remove ActionBar from Activity that extends appcompat-v7
- How to send SMS on Android Emulator
- Android RatingBar Example
- This Toast was not created with Toast.makeText() : Android RuntimeException
- Android Studio Ctrl Shift o auto import not working
- Android : Duplicate registration for activity com.example.abc
- ADT Installation Error: requires plug-in org.eclipse.wst.sse.ui
- Running Android Lint has encountered a problem NullPointerException Error
- Android Emulator] ##KBD: Full queue, lose event Error Logs
- Export aborted because fatal lint error were found
- Android Parsing Data for android-L failed Unsupported major.minor version 51.0 Error
- Android : Accidental Octal Lint Warning
- Android ListView turns Black or Flickers while Scrolling
- How to make a dummy phone call from Android Emulator device
- See actual SharePoint error exception modify web.config - SharePoint
- Rounded Images in Bootstrap framework - Bootstrap
- Java location in Mac OS X - Mac-OS-X
- SharePoint Designer Workflow error - Coercion Failed: Input cannot be null for this coercion - SharePoint
- How to enable line numbers in IntelliJ Android Studio for all files - Android-Studio
- incorrect line ending: found carriage return (\r) without corresponding newline (\n) - Android
- Failed to sync Gradle project Error:failed to find target android-23 - Android
- How to Add a horizontal line in Android Layout - Android
- No CPU ABI system image available for this target Error Android Virtual Device - Android
- Convert text to random case using Notepad++ - NotepadPlusPlus
- Java Code to check if Twitter app is installed on Android device - Android
- Android : Accidental Octal Lint Warning - Android
- Android activity main xml stuck loading - Android-Studio
- Android Eclipse This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in - Android
- Google Search Hot Trends Screensaver for Mac OS X - Mac-OS-X