How to apply theme programmatically?

1.1k Views Asked by At

In XML, my action bar is created like this:

<android.support.v7.widget.Toolbar
    android:id="@+id/myactivity_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

Now I'm trying to create the action bar programmatically but I'm having difficulties to find out how to apply the style. I've tried the following:

android.support.v7.widget.Toolbar actionBar = new android.support.v7.widget.Toolbar(getContext());  
TypedValue typedValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.style.ThemeOverlay_AppCompat_ActionBar, typedValue, true);
actionBar.setTheme(typedValue.data);    

Unfortunately, this results in a "cannot find symbol" error on ThemeOverlay_AppCompat_ActionBar.

Any ideas how I can solve this?

0

There are 0 best solutions below