Giter Site home page Giter Site logo

parhamahmadi / materialcolors Goto Github PK

View Code? Open in Web Editor NEW

This project forked from servingo/materialcolors

0.0 2.0 0.0 932 KB

An Android lib to generate Material colors and change Material themes at runtime.

License: Apache License 2.0

Java 100.00%

materialcolors's Introduction

Release

MaterialColors

MaterialColors is a small library to help with styling Android apps based on the Material theme at runtime. It is able to work around the limitation of fixed styles defined via hardcoded XML files. Furthermore it is able to adjust any color to become a MaterialColor as defined by Google.

Preview

Working with MaterialColors

MaterialColor primary = MaterialColor.from(MaterialColorHue.BLUE, MaterialColorShade.P500);
MaterialColor primaryDark = MaterialColor.from(MaterialColorHue.BLUE, MaterialColorShade.P700);
MaterialColor accent = MaterialColor.from(MaterialColorHue.RED, MaterialColorShade.A400)

MaterialColorHue.values() // Holds all hues
MaterialColorHue.primaries() // Holds only hues which can be used as primary colors
MaterialColorHue.accents() // Holds only hues which can be used as accent colors

MaterialColorShade.values() // Holds all shades
MaterialColorShade.primaries() // Holds only primary shades
MaterialColorShade.accents() // Holds only accent shades

Changing a Material theme

It is necessary to change the theme of an activity right after the creation of the activity.

public class BaseActivity extends AppCompatActivity {

    public static MaterialColor primary = MaterialColor.from(RGB.from(33, 33, 33));
    public static MaterialColor primaryDark = MaterialColor.from(RGB.from(0, 0, 0));
    public static MaterialColor accent = MaterialColor.from(RGB.from(177, 33, 33));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        MaterialThemeStyler.applyPrimaryColor(this, primary);
        MaterialThemeStyler.applyPrimaryDarkColor(this, primaryDark);
        MaterialThemeStyler.applyAccentColor(this, accent);

        super.onCreate(savedInstanceState);
    }
    
}

The following snippet shows how to change the colors at runtime. It is necessary to recreate the activity to apply the changes, in case the activity is already running.

public class MainActivity extends BaseActivity {

    public void randomizeTheme() {
        Random random = new Random();
    
        MaterialColorHue primaryHue = MaterialColorHue.primaries()[random.nextInt(MaterialColorHue.primaries().length)];
        BaseActivity.primary = MaterialColor.from(primaryHue, MaterialColorShade.P500);
        BaseActivity.primaryDark = MaterialColor.from(primaryHue, MaterialColorShade.P700);
    
        MaterialColorHue accentHue = MaterialColorHue.accents()[random.nextInt(MaterialColorHue.accents().length)];
        MaterialColorShade accentShade = MaterialColorShade.accents()[random.nextInt(MaterialColorShade.accents().length)];
        BaseActivity.accent = MaterialColor.from(accentHue, accentShade);
    
        recreate();
    }

}

How to add

####Step 1. Add the JitPack repository in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		...
		maven { url "https://jitpack.io" }
	}
}

####Step 2. Add the dependency to your app dependencies

dependencies {
	compile 'com.github.servingo:materialcolors:1.0.0@aar'
}

How it works

You don't want to know. Just assume it's magic!

materialcolors's People

Watchers

James Cloos avatar S.Parham Ahmadi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.