t7bdh3hdhb
t7bdh3hdhb

Reputation: 438

AndroidX MaterialButton does not show style

I have a LinearLayout where I put programmatically buttons in it. At the beginning there is one button inside which is there to add new filter buttons. The XML is as follows:

        <LinearLayout
            android:id="@+id/filterContainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:orientation="horizontal">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/addFilterButton"
                style="@style/iconButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:icon="@drawable/ic_filter_list_icon_color" />
        </LinearLayout>

If the filter button is pressed then new filters can be added. There are added to the ViewGroup which is the LinearLayout like this:

private void addButtonForFilter(ResultFilter filter) {
    MaterialButton newFilter = new MaterialButton(mFilterBoxViewGroup.getContext(),
            null, R.attr.materialButtonStyle);
     newFilter.setText(FilterStringProvider.getLabel(mFilterBoxViewGroup.getContext(), filter));
    newFilter.setLayoutParams(mParameters);
    newFilter.setOnClickListener(this);
    newFilter.setTag(filter.getFilterType());
    mFilterBoxViewGroup.addView(newFilter);
}

Here the buttons are shown in the standard material design. Now I would like to change it to a specific style.

<style name="textButton" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="backgroundTint">@color/colorActionbarTabs</item>
    <item name="android:textColor">@color/itemColor</item>
    <item name="android:textSize">@dimen/item_standard_text_size</item>
    <item name="android:clipToPadding">false</item>
</style>

If I change the styling in the constructor of the code above then the button is not showing up as expected:

    MaterialButton newFilter = new MaterialButton(mFilterBoxViewGroup.getContext(),
            null, R.style.textButton);

The button is now showing with no padding even if the new style has the MaterialButton as parent and I was not able to find out what is causing this behavior. Any Ideas?

Upvotes: 1

Views: 716

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363747

The constructor of the MaterialButton is:

MaterialButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)

The 3rd parameter is defStyleAttr and it is an attribute defined in your app theme, it is not a style.

For example R.attr.materialButtonStyle:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
   <item name="materialButtonStyle">@style/Widget.MaterialComponents.Button</item>
</style>

With the 1.1.0 and 1.2.0-beta01 there are 3 built-in attrs defined:

<item name="materialButtonStyle">@style/Widget.MaterialComponents.Button</item>
<item name="materialButtonOutlinedStyle">@style/Widget.MaterialComponents.Button.OutlinedButton</item>
<item name="borderlessButtonStyle">@style/Widget.MaterialComponents.Button.TextButton</item>

The 1.3.0-alpha01 introduced a new attr:

<item name="buttonBarButtonStyle">@style/Widget.MaterialComponents.Button.TextButton</item>

You can use one of these attrs overriding them in your app theme:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
   <item name="buttonBarButtonStyle">@style/textButton</item>
</style>

and then use:

MaterialButton newFilter = new MaterialButton(mFilterBoxViewGroup.getContext(),
            null, R.attr.buttonBarButtonStyle);

Upvotes: 2

Related Questions