Night Mode Kullanımı

Night Mode, uygulamanın varsayılan tema modunu, night mode temasına çevrilmesidir.

Mobil uygulamanıza gece modu özelliğini ekleyerek, son kullanıcıya daha iy bir uygulama deneyimi yaşatabilirsiniz.

Android uygulamanıza eklemeniz için:

  1. res’ in içinde bulunan values klasörünün içine ‘attrs.xml’ dosyasını oluşturun.

attrs ile night mode ile kullanacağınız renk paletlerini isimlendirmeniz için kullanmanız gerekmektedir. Burada vereceğiniz isimlerle, styles.xml içindeki oluşturulacak temanın isimleri aynı olması gerekmektedir.

<?xml version="1.0" encoding="utf-8"?>

<resources>
<declare-styleable name="ds">
<attr name="backgroundColor" format="color" />
<attr name="cardBackground" format="color" />
<attr name="colorPrimaryDark" format="color" />
<attr name="textcolor" format="color"></attr>
<attr name="buttoncolor" format="color"></attr>
<attr name="toolbarcolor" format="color"></attr>
<attr name="edittextcolor" format="color"></attr>
</declare-styleable>
</resources>

attrs’i böyle oluşturduk.

2. styles.xml dosyası içinde night mode temasını oluşturma.

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimary</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="backgroundColor">#fcfcfc</item>
<item name="cardBackground">#ffffffff</item>
<item name="textcolor">#8a000000</item>
<item name="buttoncolor">#FFFFFF</item>
<item name="toolbarcolor">#03a9f4</item>
<item name="edittextcolor">#9e9e9e</item>

</style>
<!-- A separate style pattern is created for the NightMode theme.
It is important that the colors used in the default theme correspond. -->
<style name="NightMode" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimaryDarkMode</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="backgroundColor">#303030</item>
<item name="cardBackground">#424242</item>
<item name="textcolor">#ffffffff</item>
<item name="buttoncolor">#ffffffff</item>
</style>

</resources>

3. colors.xml içinde renk paletlerini ayarlamak.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#03a9f4</color>
<color name="colorPrimaryDark">#03a9f4</color>
<color name="colorAccent">#64b5f6</color>
<color name="white">#FFFFFF</color>
<color name="colorPrimaryDarkMode">#212121</color>
<color name="colorPrimaryDarkNight">#000000</color>

</resources>

4. MainActivity layoutunu oluşturmak.

Basit olarak oluşturduğum layout.

Layout’ın içinde Switch’i oluşturmayı unutmayın.

Önemli!

Layout içinde oluşturacağınız, text, button, edittext gibi nesnelerin içinde “?attr/” diyerek tema içindeki isimlendirdiniz ilgili paletleri eklemeyi unutmayın.

5. MainActivity’de Night Mode’u Kodlama

onCreate() metodu içinde ilk olarak temaları ayarlamamız için, if koşuluyla programın varsayılan temasını kontrol etmemiz gerekir. Aksi halde Switch ile true döndürsek bile mode aktif hale gelmez.

// If the night mode is correct by default, it sets the Night Mode theme.
if(AppCompatDelegate.getDefaultNightMode() ==AppCompatDelegate.MODE_NIGHT_YES){

setTheme(R.style.NightMode);
}else{
setTheme(R.style.AppTheme);
}
setContentView(R.layout.activity_main);

Daha sonra Switch kontrolünü yapalım.

//If NightMode is active, the Switch value returns true. Enabled to be active
if(AppCompatDelegate.getDefaultNightMode() ==AppCompatDelegate.MODE_NIGHT_YES){
mySwitch.setChecked(true);
}

Switch ile night modu aktif hale getirelim. Burada switch onCheckedChanged varsayılan metodunu çağırmamız gerekir.

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
//restart App
//Called intent with the restartApp method to be active.
restartApp();
}else{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
});

Switch true döndüğünde, intent ile aktivity’in yeniden başlamasını sağlayarak night mode un aktif olmasını sağlarız.

restartApp metoduyla bu işlemi yapıyoruz.

private void restartApp(){
//Go to the same class as Intent

//With setFlags, the application is prevented from being a black screen.
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
}

Ve programın çıktısı:

Github linkine buradan oluşabilirsiniz.

İnternet sitesi https://www.merttoptas.com
Yazı oluşturuldu 18

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Benzer yazılar

Aramak istediğinizi üstte yazmaya başlayın ve aramak için enter tuşuna basın. İptal için ESC tuşuna basın.

Üste dön
%d blogcu bunu beğendi: