Skip to content

Makes it easy to create beautiful preference screens for your apps

Notifications You must be signed in to change notification settings

filol/material-preference-library

Repository files navigation

material-preference-library : DEPRECATED PLEASE LOOK HERE FOR THE SAME THING material-about-library from daniel-stoneuk

Makes it easy to create a beautiful preference screen for your app. Generates an Activity or Fragment.

Fork from here: material-about-library from daniel-stoneuk

If you use this library in your app, please let me know and I'll add it to the list.

Screenshots

Light with Light Action Bar Light with Dark Action Bar Dark with Light Action Bar Dark with Dark Action Bar Custom Cardview Background Custom card & action item layout
Demo App Demo App Demo App Demo App Demo App Demo App

Features

  • Material design
  • Modular backend
  • Easy to implement
  • Simple but intuitive API
  • Dynamic item support
  • Support Switch and Checkbox (more after)

Dependency

material-preference-library is available on jitpack.io and has support for different support library versions. See the full list here.

Gradle dependency:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    implementation 'com.github.filol:material-preference-library:1.0'
}

Setup

Activity

Your Activity must extend MaterialPreferenceActivity and be in your AndroidManifest.xml:

public class ExampleMaterialPreferenceActivity extends MaterialPreferenceActivity {

    @Override
    @NonNull
    protected MaterialPreferenceList getMaterialPreferenceList(@NonNull Context context) {
        return new MaterialPreferenceList.Builder()
                .build(); // This creates an empty screen, add cards with .addCard()
    }

    @Override
    protected CharSequence getActivityTitle() {
        return getString(R.string.mp_title_preference);
    }

}

Ensure that the theme extends either of these themes, and apply primary & accent colours:

  • Theme.mp.Light (light theme with light toolbar)
  • Theme.mp.Light.DarkActionBar (light theme with dark toolbar)
  • Theme.mp.Dark (dark theme with dark toolbar)
  • Theme.mp.Dark.LightActionBar (dark theme with light toolbar)
<manifest ...>
    <application ...>
        <activity android:name=".ExampleMaterialPreferenceActivity"
            android:theme="@style/AppTheme.MaterialPreferenceActivity"/>
    </application>
</manifest>
<style name="AppTheme.MaterialPreferenceActivity" parent="Theme.mp.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Fragment

Your Fragment must extend MaterialPreferenceFragment.

public class ExampleMaterialPreferenceFragment extends MaterialPreferenceFragment {

    @Override
    protected MaterialPreferenceList getMaterialPreferenceList(final Context activityContext) {
        return new MaterialPreferenceList.Builder()
                .build(); // This creates an empty screen, add cards with .addCard()
    }

    @Override
    protected int getTheme() {
        return R.style.AppTheme_MaterialPreferenceActivity_Fragment;
    }

}

Pass in a theme that extends one of the styles above

<style name="AppTheme.MaterialPreferenceActivity.Fragment" parent="Theme.mp.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Add Cards:

Start building a "card" using MaterialPreferenceCard.Builder()

public class ExampleMaterialPreferenceActivity extends MaterialPreferenceActivity {

    @Override
    @NonNull
    protected MaterialPreferenceList getMaterialPreferenceList(@NonNull Context context) {
        MaterialPreferenceCard card = new MaterialPreferenceCard.Builder()
                // Configure card here
                .build();

        return new MaterialPreferenceList.Builder()
                    .addCard(card)
                    .build();
    }
}

Give the card a title by calling .title() on the MaterialPreferenceCard.Builder

MaterialPreferenceCard card = new MaterialPreferenceCard.Builder()
    .title("Author")
    .build();

Add Items to a card:

There are currently 4 types of items you can add to a card - MaterialPreferenceTitleItem, MaterialPreferenceActionItem, MaterialPreferenceCheckboxItem and MaterialPreferenceSwitchItem. Other types of items are planned, for example "person" items which feature buttons to showcase a single person. Feel free to submit a PR or Issue for more item ideas.

  • MaterialPreferenceActionItem: Standard item with text, icon and optional subtext.
  • MaterialPreferenceTitleItem: Larger item with large icon (e.g. app icon) and larger text.

MaterialPreferenceTitleItem is created with MaterialPreferenceTitleItem.Builder() and lets you specify text and an icon.

MaterialPreferenceCard.Builder cardBuilder = new MaterialPreferenceCard.Builder();
cardBuilder.addItem(new MaterialPreferenceTitleItem.Builder()
        .text("Material Preference Library")
        .icon(R.mipmap.ic_launcher)
        .build());

MaterialPreferenceItem is created with MaterialPreferenceItem.Builder() and lets you specify text, sub-text, an icon and an OnClickAction.

cardBuilder.addItem(new MaterialPreferenceActionItem.Builder()
        .text("Version")
        .subText("1.0.0")
        .icon(R.drawable.ic_about_info)
        .setOnClickAction(new MaterialPreferenceActionItem.OnClickAction() {
            @Override
            public void onClick() {
                Toast.makeText(ExampleMaterialPreferenceActivity.this, "Version Tapped", Toast.LENGTH_SHORT)
                        .show();
            }
        })
        .build());

MaterialPreferenceCheckboxItem is created with MaterialPreferenceCheckboxItem.Builder() and lets you specify text, sub-text, an icon and an onCheckedChangedListener.

cardBuilder.addItem(new MaterialPreferenceCheckboxItem.Builder()
        .text("Activate something")
        .subText("desciption")
        .icon(R.drawable.ic_about_info)
        .setOnCheckedChanged(new MaterialPreferenceOnCheckedChangedListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        Toast.makeText(c,"Now : "+isChecked,Toast.LENGTH_SHORT).show();
                    }
                })
        .build());

MaterialPreferenceSwitchItem is created with MaterialPreferenceSwitchItem.Builder() and lets you specify text, sub-text, an icon and an onCheckedChangedListener.

cardBuilder.addItem(new MaterialPreferenceSwitchItem.Builder()
        .text("Version")
        .subText("1.0.0")
        .icon(R.drawable.ic_about_info)
        .setOnCheckedChanged(new MaterialPreferenceOnCheckedChangedListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        Toast.makeText(c,"Now : "+isChecked,Toast.LENGTH_SHORT).show();
                    }
                })
        .build());

Return the list:

Create a MaterialPreferenceList using MaterialPreferenceList.Builder(), passing in the cards you would like to display.

MaterialPreferenceCard card = new MaterialPreferenceCard.Builder()
        .title("Hey! I'm a card")
        .build();

return new MaterialPreferenceList.Builder()
        .addCard(card)
        .build();

Check out a working example in Demo.java.

Tip: You can either use Strings / Drawables or Resources when creating MaterialPreferenceItem's

Tip: Use Android-Iconics for icons. "Android-Iconics - Use any icon font, or vector (.svg) as drawable in your application."

Tip: Use ConvenienceBuilder to easily create items or OnClickActions.

Tip: Customise text colour and card colour in your styles. Example below:

<style name="AppTheme.MaterialPreferenceActivity.Light.CustomCardView" parent="Theme.mp.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="mp_card_background">@color/colorPrimaryDark</item>
    <item name="android:textColorPrimary">#eee</item>
    <item name="android:textColorSecondary">#ffe0e0e0</item>
    <item name="mp_color_primary">#eee</item>
    <item name="mp_color_secondary">#ffe0e0e0</item>
    <!-- You can specify custom theme for toolbar and toolbarPopup. -->
    <item name="mp_toolbarTheme">@style/Theme.mp.Toolbar.Dark</item>
    <item name="mp_toolbarPopupTheme">@style/Theme.mp.Toolbar.Dark</item>
</style>

Dynamic items:

It's possible to create dynamic items that either change on tap (or when any other event occurs). There are two examples in the sample application. Simply change the items in the list variable and then call refreshMaterialPreferenceList(). DiffUtil calculates the changes to animate in the RecyclerView.

final MaterialPreferenceActionItem item = new MaterialPreferenceActionItem.Builder()
                .text("Dynamic UI")
                .subText(subText)
                .icon(new IconicsDrawable(c)
                        .icon(CommunityMaterial.Icon.cmd_refresh)
                        .color(ContextCompat.getColor(c, R.color.mp_color_icon_dark_theme)
                        ).sizeDp(18))
                .build();
        item.setOnClickAction(new MaterialPreferenceItemOnClickAction() {
            @Override
            public void onClick() {
                item.setSubText("Random number: " + ((int) (Math.random() * 10)));
                refreshMaterialPreferenceList();
            }
        });

Custom card and Action layout

To get a layout that is similar to the 6th screenshot above simply override the files mp_material_preference_action_item and mp_material_preference_list_card by creating new layout resources with the same filename. See here.

Contributors

This Library

Original Library

License

Copyright 2016-2018 François Dexemple

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Makes it easy to create beautiful preference screens for your apps

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages