Build a material Designed Preference/Settings Screen on Android : The easy way :

Recently I have open-sourced an android library that can be used to create material designed Settings/Preference screen easily. Best part is that preference widgets of android’s v7 support library is used to create this library (i.e. it will support all devices upto android 2.2 (api 8) ). Also one color chooser dialogue widget is included, that can be used to select the secondary color for the application. Let me show you how it works,

lollipop :

Kitkat :

Light Theme :

GitHub Project link : https://github.com/codevscolor/MaterialPreference

So, following are the steps to use it :

include in your dependencies (check the latest release version on GitHub ) :

dependencies {
compile ‘com.codevscolor.materialpreference:mp:0.1’
}

Sync your project and :

  1. Create one preference xml file inside res/xml folder
  2. Add android.support.v7.preference widgets.
  3. For Preference category, use com.codevscolor.materialpreference.widget.MaterialPreferenceCategory with a title , like :
<com.codevscolor.materialpreference.widget.MaterialPreferenceCategory android:title="Category one">
.........
.........
</com.codevscolor.materialpreference.widget.MaterialPreferenceCategory>

4. If you want to use color chooser widget,use it with a key,summary and title :

<com.codevscolor.materialpreference.widget.ColorChooserPreference
android:key="secondary_color_position"
android:summary="select accent color"
android:title="secondary color" />

5. Now create your Preference Activity

import com.codevscolor.materialpreference.util.MaterialPrefUtil;
import com.codevscolor.materialpreference.activity.MaterialPreferenceActivity;
import com.codevscolor.materialpreference.callback.MaterialPreferenceCallback;
public class SettingsActivity extends MaterialPreferenceActivity implements MaterialPreferenceCallback {
    //initialization of the sdk should be done here
@Override
public void init(@Nullable Bundle savedInstanceState) {
//register this class as listener for preference change
setPreferenceChangedListener(this);
        //use dark theme or not . Default is light theme
useDarkTheme(true);
        //set toolbar title
setToolbarTitle("My Toolbar");
        //set primary color
setPrimaryColor(MaterialPrefUtil.COLOR_BLUE_GREY);
        //default secondary color for tinting widgets, if no secondary color is used yet
setDefaultSecondaryColor(this, MaterialPrefUtil.COLOR_BLUE);
        //set application package name and xml resource name of preference
setAppPackageName("com.exampletest.sampleapplicationsettings");
//set xml resource name
setXmlResourceName("settingspreference");
        //optional
//if you are using color picker, set the key used for color picker in the xml preference
setColorPickerKey("secondary_color_position");
    }

    /**
* callback for preference changes
*
* @param sharedPreferences
* @param name
*/
@Override
public void onPreferenceSettingsChanged(SharedPreferences sharedPreferences, String name) {
Toast.makeText(this, "preference with key " + name + " changed", Toast.LENGTH_LONG).show();
}
}

That’s it. 
You can check one example application here.
Also, Please like/fork this project to support development.

(Check out My Personal Blog for more : codevscolor )

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.