How to add material palette to Android App Android 16.01.2018

First of all, download color resource from here and save it to res/values/.

Let's define MaterialPalette class with two methods for color acquisition.

public class MaterialPalette {
    private static int DEFAULT_COLOR = Color.BLACK;

    private static String[] colors = {"md_red", "md_pink", "md_purple", "md_deep_purple", "md_indigo",
            "md_blue", "md_light_blue", "md_cyan", "md_teal", "md_green", "md_light", "md_lime",
            "md_yellow", "md_amber", "md_orange", "md_deep_orange", "md_brown", "md_grey",
            "md_blue_grey"};

    private static String[] depths = {"50", "100", "200", "300", "400", "500", "600", "700", "800",
            "900", "A100", "A200", "A400", "A700"};

    public static int getColor(Context ctx, String color, String depth) {
        if (!Arrays.asList(colors).contains(color)) return DEFAULT_COLOR;
        if (!Arrays.asList(depths).contains(depth)) return DEFAULT_COLOR;
        int colorId =  ctx.getResources().getIdentifier(color + "_" + depth, "color", ctx.getPackageName());
        return ContextCompat.getColor(ctx, colorId);
    }

    public static int getRandomColor(Context ctx, String depth) {
        if (!Arrays.asList(depths).contains(depth)) return DEFAULT_COLOR;
        int index = (int) (Math.random() * colors.length);
        String color = colors[index];
        return getColor(ctx, color, depth);
    }
}

I'm going to show simple example of MaterialPalette class and TextView with rounded background. I use following drawable for rounded background

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="#F00"/>
    <stroke android:width="2dp" android:color="#FFF"/>
    <padding android:top="10dp" android:right="10dp" android:left="10dp" android:bottom="10dp"/>
</shape>

Below you can see simple layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:paddingTop="10dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:text="K"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:textSize="30sp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:background="@drawable/circle"/>

</LinearLayout>

And MainActivity

public class MainActivity extends AppCompatActivity {
    private Activity activity = MainActivity.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fonts);

        TextView tv = findViewById(R.id.tv1);

        Drawable bg = tv.getBackground();
        bg.setColorFilter(MaterialPalette.getRandomColor(activity,"600"), PorterDuff.Mode.SRC_ATOP);
        tv.setBackground(bg);

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bg.setColorFilter(MaterialPalette.getRandomColor(activity,"600"), PorterDuff.Mode.SRC_ATOP);
                tv.setBackground(bg);
            }
        });
    }
}

Result

android_material_palette.png