Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change AppCompat v21 toolbar theme programmatically?

Tags:

This is my toolbar xml

<?xml version="1.0" encoding="utf-8"?> <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"     app:contentInsetEnd="0dp"     app:contentInsetStart="0dp"     android:layout_height="@dimen/toolbar_height"     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"     android:background="@color/primary_color">    </android.support.v7.widget.Toolbar> 

I want to change app:theme programmatically. How do i do this?

like image 387
Shane Ekanayake Avatar asked Jul 04 '15 21:07

Shane Ekanayake


People also ask

What is the difference between Toolbar and Action bar in android?

What is the difference between the toolbar and the action bar? The most obvious difference between the two is the updated visual design of the toolbar. The toolbar no longer includes an icon on the left side and decreases some of the spacing between the action items on the right side.

How to set Action bar in android?

To code the elements of ActionBar, create a new directory in the resource folder of the application project files. Right-click on the res folder and selects New -> Directory. Give the name “menu” to the new directory. Further, create a new Menu Resource File by right click on the menu directory.

What is Androidx Appcompat widget toolbar?

androidx.appcompat.widget.Toolbar. A standard toolbar for use within application content. A Toolbar is a generalization of action bars for use within application layouts.


2 Answers

Use the following code snippet to add a theme:

Toolbar toolbar; toolbar.getContext().setTheme(R.style.ThemeOverlay_AppCompat_Dark_ActionBar); 
like image 77
John Ruban Singh Avatar answered Apr 05 '23 10:04

John Ruban Singh


You can do this programmatically or with style:

Toolbar toolbar; // your toolbar toolbar.setBackgroundColor(newColor); // i don't tested this method. Write if it's not working toolbar.setTitleTextColor(titleColor); // if toolbar is white set title to black, if toolbar is black set title to white 

Or you can do it with style:

Add attrs.xml:

<?xml version="1.0" encoding="utf-8"?> <resources>     <attr name="toolbarStyle" format="reference"/> </resources> 

And now change toolbar.xml:

<?xml version="1.0" encoding="utf-8"?> <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"     app:contentInsetEnd="0dp"     app:contentInsetStart="0dp"     android:layout_height="@dimen/toolbar_height"     app:theme="?attr/toolbarStyle"     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"     android:background="@color/primary_color">    </android.support.v7.widget.Toolbar> 

And in styles.xml (if you don't have this create it):

<?xml version="1.0" encoding="utf-8"?> <resources>     <style name="MyStyle.Dark" parent="AppCompat.Theme">         <item name="toolbarStyle">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>     </style>     <style name="MyStyle.Light" parent="AppCompat.Theme.Light">         <item name="toolbarStyle">@style/ThemeOverlay.AppCompat.Light.ActionBar</item>     </style> </resources> 

If you select second method (with styles) you must restart activity and use setTheme method before super.onCreate()

I hope I helped you.

like image 31
krystian71115 Avatar answered Apr 05 '23 11:04

krystian71115