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);
}
}
- Increase Android Emulator Timeout time
- Android : Remove ListView Separator/divider programmatically or using xml property
- Error : Invalid key hash.The key hash does not match any stored key hashes
- How to Change Android Toast Position?
- Android Alert Dialog with Checkboxes example
- Android : No Launcher activity found! Error
- Android Development: Spinners with Example
- Failed to sync Gradle project Error:failed to find target android-23
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Disable Fading Edges Scroll Effect Android Views
- How to create Toast messages in Android?
- Channel 50 SMSes received every few minutes Android Phones
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Create Custom Android AlertDialog
- How To Disable Landscape Mode in Android Application
- Android Development - How to switch between two Activities
- incorrect line ending: found carriage return (\r) without corresponding newline (\n)
- Generate Facebook Android SDK keyhash using java code
- Android Error Generating Final Archive - Debug Certificate Expired
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Android RatingBar Example
- 11 Weeks of Android Online Sessions-15-Jun-to-28-Aug-2020
- Download interrupted: Unknown Host dl-ssl.google.com Error Android SDK Manager
- fill_parent vs match_parent vs wrap_content
- Android : Connection with adb was interrupted 0 attempts have been made to reconnect
- Docker - Incompatible CPU detected - M1/M2 Mac (macOS Sonoma) - Docker
- How to install npm on Ubuntu - Linux
- Command to check Last Login or Reboot History of Users and TTYs - Linux
- How to Copy all text to Clipboard in Vim - vi
- Program 38: Store two numbers in variables and print their sum - Python-Programs
- Fix - Microsoft Teams Operation Failed With Unexpected Error - Teams
- Bash Command to Find OS Version in Terminal - Bash
- How to check PowerShell version - Powershell