1

I have a few really simple PreferenceActivitys that I'd like to material designed but I'm not quite sure how.

My activity doesn't use any custom styles or themes so I was under the impression that it would automatically adopt the material-design look and feel, but it didn't.

How can I achieve this? Do I need to add a custom styles.xml?

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
  • 2
    Try out my library here: https://github.com/AndroidDeveloperLB/MaterialPreferenceLibrary . It's not a full solution, but it could be useful. – android developer May 26 '15 at 09:43

2 Answers2

1

You should create an Activity and then add a Fragment that extend from PreferenceFragment

public class PreferenceActivity extends ActionBarActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preference_layout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getFragmentManager().beginTransaction().replace(R.id.content, new CustomPreferenceFragment()).commit();
    }
}

And the Fragment

public static class CustomPreferenceFragment extends PreferenceFragment 
{
    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
Bae
  • 892
  • 3
  • 12
  • 26
0

In your Manifest file, you can define the theme for your Activity:

<activity
    android:name="YourActivity"
    ...
    android:theme="@android:style/Theme.Material" >
</activity>

Please, take a look at the documentation

Also, take into account that

@android:style/Theme.Material requires API level 21

antonio
  • 18,044
  • 4
  • 45
  • 61