2

I've run into serious problem with theme after updating to appcompat-v7:22. Issue described below does not occur with appcompat-v7:21

I have activity with one statically added fragment. Fragment view is created programmatically only (no layout is inflated). Final activity view contains 2 buttons: one created directly in Activity layout, second added programmatically via Fragment. Second button should look similar to the first one as no custom styles or attribute values are assigned:

enter image description here

App theme extends Theme.AppCompat.NoActionBar

Activity layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:text="Super Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/myfragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.jskierbi.appcompat22test.MainFragment" />

</LinearLayout>

Fragment class:

public class MainFragment extends Fragment {

    @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout layout = new LinearLayout(getActivity());
        layout.setOrientation(LinearLayout.VERTICAL);
        Button btn = new Button(getActivity());
        btn.setText("Click me!");
        layout.addView(btn);
        return layout;
    }
}

I've separated this issue to new project here: https://github.com/jskierbi/appcompat-v7-22-bug

Is this a bug or am I missing something? Is it possible to make a workaround for this?

Edit

This is not a bug. <Button> widget defined in layout inflates to TintButton object in view hierarchy. Possible fix is to create TintButton instead of Button in code. Warning TintButton is inside internal package so it is not supposed to be used in production code.

jskierbi
  • 1,635
  • 1
  • 14
  • 22
  • is it working in production code? – marilion91 Apr 20 '15 at 13:16
  • No we have not this into production. It is working as long as TintButton is exposed, but this is not guaranteed to stick around with new SDK version. – jskierbi Apr 22 '15 at 13:47
  • Sorry, but i don't understand your solution: i have the same problem, what i must do if i want to change the button color (for Android KitKat and Lollipop), maintaining ripple effect? – JJ86 Jun 03 '15 at 09:58
  • This is not in scope of this question. Please see: http://stackoverflow.com/questions/26686250/material-effect-on-button-with-background-color – jskierbi Jun 03 '15 at 10:02
  • @jskierbi ty, the link is helpful! – JJ86 Jun 03 '15 at 11:25

2 Answers2

1

As far as I know v21 had no support for automatically styling buttons with material guidelines. I assume now the button created at runtime is not styled, which yes, might be considered a bug.

Possible workaround that comes to mind (can't test it right now) is calling, rather than new Button(), new TintButton(), where TintButton is the class defined in the support library, assumed to be a material styled version of Button.

I think it should be android.support.v7.internal.widget.TintButton.

natario
  • 24,954
  • 17
  • 88
  • 158
  • What is interesting, widget – jskierbi Mar 29 '15 at 11:00
  • So, this is not a bug, its a feature :) – jskierbi Mar 29 '15 at 11:00
  • I don't know if `internal` stuff is really supposed to be used. You could try adding at runtime, rather than `Button`, an `EditText` or `CheckBox` or some other widget that was already tinted in v21. They also have proper tinted classes. – natario Mar 29 '15 at 11:12
  • More than using this in production code I've been into investigating this feature. Now I understand more, thanks! – jskierbi Mar 29 '15 at 11:14
1

The Problem was fixed with appcompat rev 22.1 on April 22. Now you can use the normal Button or the new AppCompatButton See here

marilion91
  • 2,094
  • 1
  • 19
  • 28