0

First, I want to change the color Menu Item color, I used this:

<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/search_toolbar"
    style="@style/Widget.CustomActionBar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/light_background"
    app:theme="@style/SearchToolbarTheme" />

<style name="SearchToolbarTheme" parent="Widget.AppCompat.Toolbar">
        <item name="colorControlNormal">@color/slate_grey</item> <!-- Arrow color -->
        <item name="colorControlHighlight">@color/grey</item> <!-- Ripple effect color -->
        <item name="colorPrimaryDark">@color/dark_blue</item> <!-- Status bar color -->
        <item name="colorPrimary">@color/dark_blue</item> <!-- Status bar color -->
    </style>

And it works well!

Then, I want to custom Toolbar:

public class SearchToolbar extends Toolbar {


public SearchToolbar(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
}

public SearchToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {}

Change the xml file to

<com.abc.searchtoolbar.SearchToolbar xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/search_toolbar"
style="@style/Widget.CustomActionBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/light_background"

app:theme="@style/SearchToolbarTheme" />

After that, the app:theme doesn't work anymore.

How can I keep the app:theme attribute in my custom view when I extends Toolbar?

Frank Nguyen
  • 6,493
  • 3
  • 38
  • 37

3 Answers3

0

If you are extending a custom view from Toolbar you have to use that View in the xml file as well, so in your case:

<your.package.name.SearchToolbar xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/search_toolbar"
style="@style/Widget.CustomActionBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/light_background"
app:theme="@style/SearchToolbarTheme" />
0

For getting a custom Toolbar you don't need to create a new class and extending with Toolbar.you can use the following code in your Activity class.

// Always cast your custom Toolbar here
 Toolbar tb = (Toolbar) findViewById(R.id.yourCustomToolbarId);
 setSupportActionBar(tb);

Everything you want to change make it at xml file or style file.Then cast it your Activity class.

Real73
  • 490
  • 4
  • 13
  • I know that. But that's only work around, and the code doesn't look good. the question is how can I re-use the app:theme attribute? – Frank Nguyen Nov 01 '16 at 12:14
0

I found a work around solution using inflate

public class SearchToolbar extends FrameLayout {


public SearchToolbar(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public SearchToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    inflate(getContext(), R.layout.layout_search_toolbar, this);}}

And inside layout_search_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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/search_toolbar"
style="@style/Widget.CustomActionBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/light_background"
app:theme="@style/SearchToolbarTheme"
tools:showIn="@layout/layout_laz_appbar">

This solution is not bad, accept for we will have 1 more parent layout of Toolbar.

Frank Nguyen
  • 6,493
  • 3
  • 38
  • 37
  • Hmm I was hoping to see the optimal solution of getting the theme attribute to work. Otherwise you are just wrapping the toolbar in a frame layout. – user1325843 May 04 '18 at 19:54
  • @user1325843 You should try ContextThemeWrapper, check https://stackoverflow.com/questions/26553772/adding-two-appcompat-toolbars-with-different-themes – Frank Nguyen May 08 '18 at 02:58