Toolbar does not work with pre-lollipops devices using appcompat v7

I am making simple code for a toolbar and menu drawer using the appcompatv7 stuff. Everything works fine on a Nexus 5 with a lollipop lollipop , but if the device crashes before the lollipop (4.1 or 4.4). The problem is style definition. I put my code if someone tells me where the error is.

This is my main activity:

import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.content.res.Configuration; import android.os.Bundle; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView; public class Hello extends ActionBarActivity { private ActionBarDrawerToggle toggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); toggle = new ActionBarDrawerToggle( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close); toggle.setDrawerIndicatorEnabled(true); drawerLayout.setDrawerListener(toggle); ListView lv_navigation_drawer = (ListView) findViewById(R.id.lv_navigation_drawer); lv_navigation_drawer.setAdapter(new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, new String[] {"Screen 1", "Screen 2", "Screen 3"})); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) return true; return super.onOptionsItemSelected(item); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); toggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); toggle.onConfigurationChanged(newConfig); } } 

The main layout:

 <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" android:orientation="vertical" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" theme="@style/ThemeOverlay.AppCompat.ActionBar" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" /> <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> <ListView android:id="@+id/lv_navigation_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@android:color/white" /> </android.support.v4.widget.DrawerLayout> </LinearLayout> 

Style by simply extending Theme.AppCompat.Light.NoActionBar (I don't have a style defined for values-v21)

 <resources> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> </style> </resources> 

All this works correctly on lollipop and pre-lollipop devices. The problem occurs when you want to customize the colors of the toolbar and status bar

I make this change in values โ€‹โ€‹/ styles

 <resources> <style name="AppTheme" parent="Base.Theme.AppCompat"/> <style name="AppTheme.Base" parent="Theme.AppCompat"> <item name="colorPrimary">#2ecc71</item> <item name="colorPrimaryDark">#27ae60</item> <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> </style> <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle"> <item name="spinBars">true</item> <item name="color">@android:color/white</item> </style> <color name="primary">#457C50</color> <color name="primaryDarker">#580C0C</color> </resources> 

and I add style to the values-v21

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppTheme" parent="AppTheme.Base"> <item name="android:windowContentTransitions">true</item> <item name="android:windowAllowEnterTransitionOverlap">true</item> <item name="android:windowAllowReturnTransitionOverlap">true</item> <item name="android:windowSharedElementEnterTransition">@android:transition/move</item> <item name="android:windowSharedElementExitTransition">@android:transition/move</item> </style> </resources> 

As mentioned earlier, this works well on Nexus 5, but although the user interface comes with the right colors, I donโ€™t see the ListView inside the menu box (I donโ€™t understand why) ... but the pre-lollipop devices fail. The error calls me:

... java.lang.IllegalStateException: this activity already has an action panel provided by the window decor. Do not request Window.FEATURE_ACTION_BAR and set the windowActionBar parameter to false in your toolbar to use.

I searched a lot of information about this error and tried many options: the falseA windowActionBar element in the main action turned into a "Toolbar" on a "toolbar android.support.v7.widget.Toolbar", but to no avail ... I also I was looking for the examples already given, but it didnโ€™t work for me. I use Eclipse, the target of development is 21, and the minimum is 16, I also updated sdk and adt ...

Can someone help to work correctly on devices prior to Lellipop?

+5
source share
1 answer

Use Theme.AppCompat.Light.NoActionBar instead.

You cannot have another action bar to call setSupportActionBar . That's why you get

This operation already has an action bar provided by the window decor.

Make sure your theme does not have an action bar to use the toolbar.

+14
source

Source: https://habr.com/ru/post/1207023/


All Articles