This library can be used to implement Material Designed Settings/Preference Screen on Pre-Lollipop devices. (Currently supporting devices upto api 8 )
- Material Preference Library uses
com.android.support:preference-v7:x.x.x
support library widgets. - Also it includes a color chooser dialog widget, that can be used to select accent color .
- Header is tinted with selected secondary color.
On Lollipop :
On KitKat :
Light Theme :
dependencies {
compile 'com.codevscolor.materialpreference:mp:0.1'
}
- Create one preference xml file inside
res/xml
folder - Add
android.support.v7.preference
widgets. - 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>
- If you want to use color chooser widget,use it with a
key
,summary
andtitle
like
<com.codevscolor.materialpreference.widget.ColorChooserPreference
android:key="secondary_color_position"
android:summary="select accent color"
android:title="secondary color" />
- Now create your Preference Activity like
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();
}
}
API level 8 and above
Nandan Kaushik Dutta - @codevscolor on GitHub, personal blog : codevscolor
Apache 2.0