ActionBar/Toolbar not showing on Lollipop version of app
Solution 1
Change you're activity_main.xml as follows
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<RelativeLayout
android:id="@+id/drawerView"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="start" >
<ListView
android:id="@+id/drawerList"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@drawable/nav_border"
android:divider="@null" />
</RelativeLayout>
<!--
<fragment
android:id="@+id/fragment1"
android:name="com.shamu11.madlibsportable.MadlibsSelect"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
-->
</android.support.v4.widget.DrawerLayout>
and If you're using setSupportActionBar then toolbar.inflateMenu won't work.
For more details check this post Android Usages Of Toolbar. It may help you.
Solution 2
Your activity has to extend from ActionBarActivity when using the new AppCompat library.
Solution 3
Yes this happen , I have wasted over an hour to fix this.
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
This will automatically sync and finally show the toogle icon.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
That works for me, Hope that helps.
sanic
Updated on June 07, 2022Comments
-
sanic almost 2 years
I'm currently trying to convert my app to lollipop material design and have run into some problems with the actionbar/toolbar. Implementing everything the way I did, the actionbar/toolbar won't show on either a lollipop or kitkat device. Perhaps someone could look at my themes, styles, activity_main (where I hold a bunch of fragments and is the only place I put the toolbar xml code), and mainactivity.
Thanks.
values/styles.xml
<resources> <style name="Theme.MyTheme" parent="Theme.AppCompat.Light"> <!-- Set AppCompat’s actionBarStyle --> <item name="actionBarStyle">@menu/action_menu</item> <!-- The rest of your attributes --> </style> </resources>
values/themes.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppTheme" parent="AppTheme.Base"/> <style name="AppTheme.Base" parent="Theme.AppCompat"> <item name="colorPrimary">@color/material_blue_grey_800</item> <item name="colorPrimaryDark">@color/material_blue_grey_950</item> <item name="android:textColor">@color/black</item> <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> </style> </resources>
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawerLayout" android:layout_width="match_parent" android:layout_height="match_parent" > <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> <RelativeLayout android:id="@+id/drawerView" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_gravity="start" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" /> <ListView android:id="@+id/drawerList" android:layout_width="250dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="@drawable/nav_border" android:divider="@null" /> </RelativeLayout> <!-- <fragment android:id="@+id/fragment1" android:name="com.shamu11.madlibsportable.MadlibsSelect" android:layout_width="fill_parent" android:layout_height="fill_parent" /> --> </android.support.v4.widget.DrawerLayout>
mainactivity
public class MainActivity extends ActionBarActivity implements OnItemClickListener { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Fragment fragment = new MadlibsSelect(); fragment = new MadlibsSelect(); FragmentTransaction fm = getSupportFragmentManager().beginTransaction(); fragment.setArguments(getIntent().getExtras()); fm.add(R.id.fragment_container, fragment); fm.commit(); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); toolbar.inflateMenu(R.menu.action_menu); //ActionBar bar = getActionBar(); //bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#428bca"))); ...more stuff happen down here including adding nav drawer.
-
sanic over 9 yearsAlready doing that, also implementing onitemclicklistener I added that code for clarification.
-
Roberto Betancourt over 9 years@shamu11 Have you tried changing your parent theme to "Theme.AppCompat.Light.NoActionBar" You're already declaring the toolbar in your layout so you should be fine.
-
sanic over 9 yearsActually I just changed <item name="android:windowNoTitle">true</item> from true to false and now I have an action bar! Can you recommend a way to go a tinker with this action bar and add the toggle to it? Thanks!
-
sanic over 9 yearsit seems on kitkat, it doesn't go back to an actionbar, it just displays a little header saying the name of my app. So it seems not to be the fully functioning way to go, but this is something.
-
Roberto Betancourt over 9 yearsDo you mean the Navigation Drawer toggle? The toolbar should behave exactly as the old ActionBar, Just add the toggle like you would normally do with an ActionBar.
-
sanic over 9 yearsokay, figure all that out. Do you have any theory for why the action bar isn't appearing in the pre-lollipop devices? I appreciate the help!
-
Roberto Betancourt over 9 yearsTry removing these elements: <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item>
-
sanic over 9 yearsthis coupled with the answer above solved my problem, thank you!
-
Eugene Pankov over 7 yearsWorst kind of answer. Are the rest of the people supposed to do a diff on those two files now?