When I try to use getActionBar()
it says it is deprecated, so that means by going forward we are not allowed to use getActionBar()
.
Which
If you are using appcompat
, the correct method is getSupportActionBar()
The reason you should create this, because Fragment Tabs using ActionBar is now deprecated. This is new way to create your actionbar with Material Design. to use this you need to add dependencies in your build.gradle file.
compile 'com.android.support:appcompat-v7:21.0.0'
create toolbar app_bar.xml file, you can give the name you want.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Make sure, you selected theme in styles.xml with
"Theme.AppCompat.NoActionBar"
after that, you need to initialize this toolbar in your mainActivity.xml layout file, for that include this,
<include
android:id="@+id/app_toolbar"
layout="@layout/app_toolbar"/>
then in the MainActivity, you need to make instance of Toolbar toolbar. then, after setContentView() add following with your
toolbar = (Toolbar) findViewById(R.id.app_toolbar);
setSupportActionBar(toolbar);