Android Night Theme Oluşturmak

values > attrs.xml oluşturun içerisine;

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ds" >
        <attr name="backgroundcolor" format="color" />
        <attr name="cardbackground" format="color" />
        <attr name="textcolor" format="color" />
        <attr name="tintcolor" format="color" />
        <attr name="buttoncolor" format="color" />
    </declare-styleable>
</resources>

values > styles.xml içerisine;

<resources>

    <!-- Light/Day Mode theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#2196F3</item>
        <item name="colorPrimaryDark">#1976D2</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="backgroundcolor">#fcfcfc</item>
        <item name="cardbackground">#ffffff</item>
        <item name="textcolor">#808080</item>
        <item name="tintcolor">#000000</item>
        <item name="buttoncolor">#2196F3</item>
        <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
    </style>
    
    <!-- Night / dark  Mode theme. -->
    <style name="darktheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#212121</item>
        <item name="colorPrimaryDark">#000000</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="backgroundcolor">#303030</item>
        <item name="cardbackground">#424242</item>
        <item name="textcolor">#ffffff</item>
        <item name="tintcolor">#ffffff</item>
        <item name="buttoncolor">#2196F3</item>
        <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>

    </style>

    <style name="WindowAnimationTransition">
        <item name="android:windowEnterAnimation" >@android:anim/fade_in</item>
        <item name="android:windowExitAnimation">@android:anim/fade_out</item>

    </style>

</resources>

SharedPref.java oluşturun ve içerisine;

public class SharedPref {
    SharedPreferences mySharedPref ;
    public SharedPref(Context context) {
        mySharedPref = context.getSharedPreferences("filename",Context.MODE_PRIVATE);
    }
    // this method will save the nightMode State : True or False
    public void setNightModeState(Boolean state) {
        SharedPreferences.Editor editor = mySharedPref.edit();
        editor.putBoolean("NightMode",state);
        editor.commit();
    }
    // this method will load the Night Mode State
    public Boolean loadNightModeState (){
        Boolean state = mySharedPref.getBoolean("NightMode",false);
        return  state;
    }
}

Örnek MainActivity kodları (Not: xml tarafında Switch nesnesi üretiniz ve id olarak myswitch yazınız

public class MainActivity extends AppCompatActivity {
    SharedPref sharedpref;
    private Switch myswitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        sharedpref = new SharedPref(this);
        if(sharedpref.loadNightModeState()==true) {
            setTheme(R.style.darktheme);
        }
        else  setTheme(R.style.AppTheme);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        myswitch=(Switch)findViewById(R.id.myswitch);
        if (sharedpref.loadNightModeState()==true) {
            myswitch.setChecked(true);
        }
        myswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedpref.setNightModeState(true);
                    restartApp();
                }
                else {
                    sharedpref.setNightModeState(false);
                    restartApp();
                }
            }
        });

    }
    public void restartApp () {
        Intent i = new Intent(getApplicationContext(),MainActivity.class);
        startActivity(i);
        finish();
    }
}
Bu makaleyi paylaşın:

3 Comments

Bir yorum bırakın