Reputation: 183
In my app I have a Main Activity with a drawer layout to navigate between two fragments. One of those fragments has a FAB which opens another activity that extends from AppCompatActivity. This activity shows no lower toolbar, and an upper toolbar in grey. I managed to add a lower toolbar in the xml code :
<android.support.v7.widget.Toolbar
android:id="@+id/editor_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="Add Manual Entry"/>
but the upper toolbar keeps showing in grey no matter what. Tried setting the upper toolbar to my colorPrimaryDark as a workaround but found no way to do it. I looked into my AppTheme and found this:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
Seeing as the parent says no action bar, I tried changing it to Theme.AppCompat.Light but I get this error on my main activity:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tobias.run/com.example.tobias.run.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
which looks like my MainActivity and its drawer layout require the NoActionBar theme, altough I'm not sure. Why does my secondary activity doesn't come with a toolbar? how can I add one?
PS: Both the secondary activity code and xml are just as how Android Studio created them with the empty activity template. the xml just had the toolbar added.
Upvotes: 0
Views: 1852
Reputation: 183
Ended up finding a workaround. The issue was the
<item name="android:statusBarColor">@android:color/transparent</item>
in the AppTheme. When setting it to transparent, the status bar fades to transparent when activating the drawer layout, which is the desired result. But when you create a new activity the status bar looks exactly as your background, making it look like there's no status bar. To make it work, I left the attr transparent in the App Theme for the Drawer Layout but changed during runtime, in my other activity, the color of my status bar following this post.
Upvotes: 0
Reputation: 2014
This means that you already have included a toolbar somewhere in your view hierarchy (you have not provided your complete layout, so hard to say).
In any case, just paste these two lines in your code (OnCreate
of your Actvity) and mostly this should fix your problem.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Oh yes, revert back the other changes that you did to supply the action bar.
The toolbar reference might be different if you have specifically changed it in your layout. The default is always given an id toolbar
Android sometime back revamped the whole action bar thing and made it more generic. So what you include in your layout is a generic toolbar which does not show up as a actionbar unless you set it in your code.
Upvotes: 2
Reputation: 13555
Try this
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
OR
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and in you Manifest
<activity android:name=".activity.MyActivity"
android:theme="@style/AppTheme.NoActionBar">
Upvotes: 2