/material-setting

Primary LanguageJavaApache License 2.0Apache-2.0

material-setting-library

Release Apache License 2.0

This project inspiration from: material-about-library

Thanks!

中文文档

Screenshots

Screenshot Screenshot
Demo App Demo App

Features

  • Material design
  • Modular backend
  • Easy to implement
  • Simple but intuitive API

Dependency

material-setting-library is available on jitpack.io

Gradle dependency:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    compile 'com.github.zzyandzzy:materialsetting:0.3'
}

Setup

Activity&Fragment

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

public class MainActivity extends MaterialSettingActivity {
    private MaterialSettingCard materialSettingCard;
    private MaterialSettingCard.Builder builder;

    @Override
    protected MaterialSettingList getMaterialSettingList(Context context) {
        builder = new MaterialSettingCard.Builder()
                .title("title");
        materialSettingCard = builder.build();
        builder.addItem(new MaterialSettingTitleItem.Builder()
                .text(R.string.app_name)
                .icon(R.mipmap.ic_launcher)
                .build());
        return new MaterialSettingList(materialSettingCard);
    }

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

}

Your Fragment must extend MaterialSettingFragment

public class mainFragment extends MaterialSettingFragment {
    MaterialSettingCard.Builder settingCardBuilder;
    MaterialSettingActionItem materialSettingActionItem;

    @Override
    protected MaterialSettingList getMaterialSettingList(Context context) {
        settingCardBuilder = new MaterialSettingCard.Builder();
        settingCardBuilder.title("设置");
        materialSettingActionItem = new MaterialSettingActionItem.Builder()
                .text("标题")
                .subText("可点击的item")
                .icon(R.mipmap.ic_launcher)
                .setOnClickListener(new MaterialSettingActionItem.OnClickListener() {
                    @Override
                    public void onClick() {
                        Toast.makeText(getActivity(),"你点击了:"+ materialSettingActionItem.getText().toString(),
                                Toast.LENGTH_SHORT).show();
                    }
                }).build();
        settingCardBuilder.addItem(materialSettingActionItem);
        return new MaterialSettingList(settingCardBuilder.build());
    }
}

Ensure that the theme extends 'Theme.Mal', and apply primary & accent colours.

    <!-- Base application theme. -->
        <style name="AppTheme.MaterialSetting" parent="Theme.Mal">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>

AndroidManifest.xml

 <activity android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.MaterialSetting">

Add Cards:

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

public class MainActivity extends MaterialSettingActivity {
    private MaterialSettingCard.Builder builder;

    @Override
    protected MaterialSettingList getMaterialSettingList(Context context) {
        builder = new MaterialSettingCard.Builder()
                        .title("title");
    }
}

or

public class MainActivity extends MaterialSettingActivity {
    private MaterialSettingCard.Builder builder;

    @Override
    protected MaterialSettingList getMaterialSettingList(Context context) {
        builder = new MaterialSettingCard.Builder();
        builder.title("title");
    }
}

Add Items:

There are currently two types of item you can add to a card - MaterialSteeingTitleItem and MaterialSettingActionItem and MaterialSettingCompoundButtonItem. Planned items include "person" items which feature buttons to showcase a single person. Feel free to submit a PR or Issue for more item ideas.

  • MaterialSettingActionItem: Standard item with text, icon and optional subtext.
  • MaterialSteeingTitleItem: Larger item with large icon (eg app icon) and larger text.
  • MaterialSettingCompoundButtonItem: CheckBox Switch and RadioButton.

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

        builder.addItem(new MaterialSettingTitleItem.Builder()
                .text(R.string.app_name)
                .icon(R.mipmap.ic_launcher)
                .build());

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

        builder.addItem(new MaterialSettingActionItem.Builder()
                .text(R.string.app_name)
                .subText(R.string.mal_title)
                .icon(R.mipmap.ic_launcher)
                .setOnClickListener(new MaterialSettingActionItem.OnClickListener() {
                    @Override
                    public void onClick() {
                        Toast.makeText(MainActivity.this,"click",Toast.LENGTH_SHORT).show();
                    }
                }).build());

MaterialSettingCompoundButtonItem is created with MaterialSettingCompoundButtonItem.Builder() and lets you specify defText, defSubText,changeOnText, changeOnSubText,changeOffText, changeOffSubText, an CompoundButton and an OnClickListener.

ItemType:

CHECKBOX_ITEM

CheckBox

SWITCH_ITEM

SWITCH

SWITCH_RADIOBUTTON_ITEM

RadioButton

ButtonPosition:

LEFT

Position

RIGHT

Position

        builder.addItem(new MaterialSettingCompoundButtonItem.Builder()
                .setItemType(MaterialSettingItem.ItemType.CHECKBOX_ITEM)
                .setButtonPosition(MaterialSettingItem.ButtonPosition.LEFT)
                .key("title")
                .defValue(false)
                .defText("text")
                .defSubText("subText")
                .changeOnText("changeOnText")
                .changeOnSubText("changeOnSubText")
                .changeOffText("changeOffText")
                .changeOffSubText("changeOffText")
                .setOnCheckedChangeListener(new MaterialSettingCompoundButtonItem.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, String key, boolean isCheck) {
                        Toast.makeText(MainActivity.this,key + "change :" + isCheck,Toast.LENGTH_SHORT).show();
                    }
                }).build());

Return the list:

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

        return new MaterialSettingList.Builder()
                        .addCard(builder.build())
                        .build();

Check out a working example in MainActivity.java.

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

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

Contributors

License

Copyright 2016 zzyandzzy

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.