android toolbar popupTheme vs theme - android

Often I see this declaration of Toolbar in layout files:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
Why are there two attributes relating theming: theme and popupTheme?
What are the purposes of each of them?

popupTheme
Specifies the theme to use when inflating popup menus. By default, uses the same theme as the Toolbar itself.
theme
It is simply the theme of Toolbar.

Related

How to change the background color of the overflow menu in android

I want to change the background color of the overflow popup menu to match the background of the main screen. Does anyone know how I could do that?
Thanks.
If you are using a Toolbar, first you need to add this line to your toolbar layout:
app:popupTheme="#style/ThemeOverlay.MyTheme"
It should look like this:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="#color/primary"
app:titleTextColor="#color/white"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.MyTheme"/>
You need to use that line to tell your Toolbar which theme to use. Then, you add the following theme to your styles.xml file:
<style name="ThemeOverlay.MyTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:colorBackground">#color/primary</item>
<item name="android:textColor">#color/white</item>
</style>
In the place I put "#color/primary" and "#color/white", you can use any color you want, and you can also put hex values like #000000.

Change AppBarLayout theme programmatically

First I set AppBarLayout theme via xml:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MyTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/MyTheme.PopupOverlay"
app:titleTextAppearance="#style/MyTheme.Toolbar.Title" />
where #style/MyTheme.AppBarOverlay contains:
<style name="MyTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="colorPrimary">#drawable/toolbar_background</item>
</style>
But then in some scenarios I want to change it programmatically without change the Theme of the activity (only the Theme of the AppBarLayout).
I have tried these two ways without success:
First:
ActionBar actionBar = getSupportActionBar();
actionBar.getThemedContext().setTheme(R.style.MyTheme_AppBarOverlay_2);
Second:
AppBarLayout mAppBar = (AppBarLayout) findViewById(R.id.appbar); mAppBar.getContext().setTheme(R.style.MyTheme_AppBarOverlay_2);
Changing themes after inflation is not possible with Android views. You'll have to change each property the theme defines individually. You may work around this by using Compose TopAppBar, since Compose widgets react to theme changes.

Remove shadow toolbar below actionbar in blank activity

I've created a new project with a new blank activity using Android Studio and I'm trying to remove the shadow below the toolbar in >=API21. I've tried many things.
This works for < API21
<item name="android:windowContentOverlay">#null</item>
This doesn't work for me in phone with >=API21:
getSupportActionBar().setElevation(0);
<item name="android:elevation">0dp</item>
I don't know what else I can try. Any help is appreciated.
EDIT: I've tried everything from other questions like this but nothing worked.
As #Vipul Asri said, I had to add app:elevation="0dp" but I was adding it to the wrong place. This works:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="android.teechart.steema.com.androiddemo.DashboardWebAnalytics">
<android.support.design.widget.AppBarLayout
app:elevation="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_dashboard_web_analytics" />
</android.support.design.widget.CoordinatorLayout>
I was adding it in android.support.v7.widget.Toolbar but the correct place was in android.support.design.widget.AppBarLayout.
This shadow is part of windowContentOverlay on APIs below LOLLIPOP (on LOLLIPOP it's #null).
When you work with Toolbar widget the toolbar isn't part of window decor anymore so the shadow starts at the top of the window over the toolbar instead of below it (so you want the windowContentOverlay to be #null). Additionally you need to add an extra empty View below the toolbar pre-LOLLIPOP with its background set to a vertical shadow drawable (8dp tall gradient from #20000000 to #00000000 works best). On LOLLIPOP you can set 8dp elevation on the toolbar instead.

customize actionbar with different background color - Android

I am trying to style the Actionbar of my app. Since i need to customize more i have replaced the actual ActionBar with ToolBar. I want to design the ActionBar as shown in the below image1.
Below is the toolbar code i am using to customize.
custom_actionbar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
android:id="#+id/actionbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</RelativeLayout
activity_toolbar.java
// Custom actionbar starts here
Toolbar action = (Toolbar) findViewById(R.id.actionbar);
action.inflateMenu(R.menu.menu_scrollable_tabs);
action.setNavigationIcon(R.drawable.ct_drawer);
I am getting the action bar like below image2,
1) The problem i am facing is i am not getting the Navigation icon and menu icon as same as requested by the client.
2) How to achieve this using toolbar. Please provide if any other alternate way to achieve this in android.
Please help.
Set the colorPrimary value of your theme for your style in styles.xml
<item name="colorPrimary">#color/colorPrimary</item>
Then define your color in colors.xml file like follows
<color name="colorPrimary">#3F51B5</color>
Please see this example of setting primary colors
http://www.dilan.me/articles/tech-notes/android-tips-2-how-to-change-the-main-colors-of-your-app/
you can override color in /res/values/colors.xmal
and change value of colorPrimary
<color name="colorPrimary">#3F51B5</color>
or
use this code
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setBackgroundColor(Color.BLACK);
or
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:background="#android:color/black"
android:background="?attr/colorPrimary"
android:elevation="4dp"
/>

Android Lollipop toolbar `setTitle`

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/purple"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
When I use the setTitle() method, it all works fine. But the problem is that when I change orientation (since in lollipop 5.1 I can do so) the title word is small compared to portrait version. Why such bug? and how do I solve this?
By default the Toolbar's fontsize is smaller in landscape.
If you want it to have the same size as in portrait you can set a custom titleTextAppearance:
<android.support.v7.widget.Toolbar
app:titleTextAppearance="#style/YourToolbarTitleStyle"
.../>
with YourToolbarTitleStyle being defined as a style where you specify a text size:
<style name="YourToolbarTitleStyle" parent="#style/TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">20sp</item>
</style>

Categories

Resources