Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppCompat v22.1.0 not theming all xml widgets correctly for fragments

When using xml based layouts using AppCompat 22.1.0 not all supported widgets are tinted or material themed for my Fragments using Android 4.4.

I see this behavior with the following widgets (others not tested):

  • RadioButton (No tint color)
  • CheckBox (No tint color)
  • Spinner (Device default theme is applied)
  • EditText (Device default theme is applied)
  • RatingBar (Device default theme is applied)
  • Button (Device default theme is applied)

It used to work in AppCompat v22.0.0.

Screenshot (left 4.4, right 5.0):

Example screenshot

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}

fragment_main.xml

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

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="RadioButton test"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="CheckBox test"/>

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/someStrings"/>
</LinearLayout>

Themes.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></style>
</resources>
like image 297
Rolf ツ Avatar asked Apr 22 '15 18:04

Rolf ツ


2 Answers

This is currently reported as bug: https://code.google.com/p/android/issues/detail?id=169760

A temporary workaround is to use the Fragment parent Activity LayoutInflater: getActivity().getLayoutInflater() instead of the supplied LayoutInflater in the onCreateView method.

Example:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = getActivity().getLayoutInflater().inflate(R.layout.fragment_main, container, false);
    return rootView;
}

Note: Another solution is to use the special AppCompat widgets in your xml layout:

  • android.support.v7.widget.AppCompatRadioButton
  • android.support.v7.widget.AppCompatCheckBox
  • android.support.v7.widget.AppCompatSpinner

But this would basically mean you need to replace every single widget with the AppCompat one.

like image 81
Rolf ツ Avatar answered Sep 27 '22 21:09

Rolf ツ


You can force the theme applied to views, only need to add these lines on the parent view:

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

Then all views inside the linear layout take the accent color declared to the respective activity in the manifest (Via theme).

like image 39
Heraldo Avatar answered Sep 27 '22 21:09

Heraldo