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?
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.
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.
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.
Use the following code snippet to add a theme:
Toolbar toolbar; toolbar.getContext().setTheme(R.style.ThemeOverlay_AppCompat_Dark_ActionBar);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With