Android tutorial

Android how to change application theme color programmatically

Android how to change application theme color programmatically
Android how to change application theme color programmatically

Hi everyone in this android tutorials, I am Sharing Android how to change the application theme color programmatically. In these android tutorials, you can learn an easy way to create an android app where you can change the android application theme color programmatically according to your choice.

Hare, I create a simple code for the android change application theme color programmatical. I show a color dialog to show color and you can select a color according to your interest.

Step 1: Open your Android Studio and Create an App.

let’s start for the app to start your android studio and create an app with name and create an activity.

Step 2: Open project base Build.gradle and add this dependance.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile files('libs/glide-3.6.0.jar')
    compile('com.mikepenz:materialdrawer:5.8.2@aar') {
        transitive = true
    }
    //noinspection GradleCompatible
    compile 'com.android.support:appcompat-v7:27.0.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:support-v4:27.0.1'
    compile 'com.android.support:design:27.0.1'
    compile 'com.geniusforapp.fancydialog:FancyDialog:0.1.0'
    compile 'com.turki-alkhateeb:materialcolorpicker:1.0.7'
    compile 'org.apache.commons:commons-lang3:3.5'
    compile 'com.github.apl-devs:appintro:v4.2.2'
    compile 'com.daimajia.numberprogressbar:library:1.4@aar'
    testCompile 'junit:junit:4.12'
}

Android how to change application theme color programmatically

Step 3: Open Your XML file  activity.xml and add these code.

open your XML file and create a layout with text and button also hare you can use on click listener and open dialog for selected color.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:id="@+id/llselectalarm"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:background="@drawable/click_background">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="7dp"
        android:text="Theme" />

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="15dp"
        android:id="@+id/button_color"/>

</LinearLayout>

Step 4: Open Your java class main.java and add this code.

in your java class, you can implement your XML UI. and create a canvas UI with multiple colors and after select color set them your color to your app theme. there is an easy way to learn how to change application theme programmatically.

public class SettingsActivity extends AppCompatActivity {
    SharedPreferences sharedPreferences, app_preferences;
    SharedPreferences.Editor editor;
    Button button;
    Methods methods;

    int appTheme;
    int themeColor;
    int appColor;
    Constant constant;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
        appColor = app_preferences.getInt("color", 0);
        appTheme = app_preferences.getInt("theme", 0);
        themeColor = appColor;
        constant.color = appColor;

        if (themeColor == 0){
            setTheme(Constant.theme);
        }else if (appTheme == 0){
            setTheme(Constant.theme);
        }else{
            setTheme(appTheme);
        }
        setContentView(R.layout.activity_settings);
       

        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_setting);
        toolbar.setTitle("Settings");
        toolbar.setBackgroundColor(Constant.color);
        toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        methods = new Methods();
        button = (Button) findViewById(R.id.button_color);
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        editor = sharedPreferences.edit();

        colorize();

        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                ColorChooserDialog dialog = new ColorChooserDialog(SettingsActivity.this);
                dialog.setTitle("Select");
                dialog.setColorListener(new ColorListener() {
                    @Override
                    public void OnColorClick(View v, int color) {
                        colorize();
                        Constant.color = color;

                        methods.setColorTheme();
                        editor.putInt("color", color);
                        editor.putInt("theme",Constant.theme);
                        editor.commit();

                        Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                    }
                });

                dialog.show();
            }
        });  
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void colorize(){
        ShapeDrawable d = new ShapeDrawable(new OvalShape());
        d.setBounds(58, 58, 58, 58);

        d.getPaint().setStyle(Paint.Style.FILL);
        d.getPaint().setColor(Constant.color);

        button.setBackground(d);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

Step 5: Create a Java Class with the name  Constant.java  and add this code.

in these constant java class add a boolean value to check and set a mutipal theme for your android app

class Constant {
    public static int nav_clicked = 0;
    public static Boolean isNavClicked = false;

    public static Boolean isToggle = true;
    public static int color = 0xff3b5998;
    public static int theme = R.style.AppTheme;
}

 

Read More Tutorial