Sobes.tech
Junior

Kako sačuvati i koristiti podešavanja teme u Android aplikaciji?

sobes.tech АИ

Одговор од АИ

Efikasni pristup uključuje korišćenje SharedPreferences za čuvanje odabrane teme i AppCompatDelegate.setDefaultNightMode() za njenu primenu.

  1. Čuvanje teme: Koristite SharedPreferences za skladištenje identifikatora odabrane teme (npr., "light", "dark", "system").

    // Dobijanje SharedPreferences
    val sharedPrefs = getSharedPreferences("AppTheme", Context.MODE_PRIVATE)
    // Dobijanje urednika
    val editor = sharedPrefs.edit()
    // Primena teme sa ID-jem themeId
    editor.putString("current_theme", themeId)
    // Čuvanje promena
    editor.apply()
    
  2. Primena teme pri pokretanju: U glavnoj Activity ili Application klasi, pročitajte sačuvanu vrednost i primenite temu.

    // Čitanje sačuvane vrednosti teme
    val themeId = getSharedPreferences("AppTheme", Context.MODE_PRIVATE)
        .getString("current_theme", "system") // Podrazumevano 'system'
    
    // Primena teme
    when (themeId) {
        "light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
        "dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
        "system" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
        else -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
    }
    
  3. Promena teme od strane korisnika: Omogućite korisnički interfejs (npr., RadioButton u podešavanjima), gde korisnik može da izabere temu. Pri izboru, sačuvajte novi identifikator u SharedPreferences i odmah primenite temu pozivom AppCompatDelegate.setDefaultNightMode() i, ako je potrebno, restartujte Activity za ispravno primenjivanje.

    // Pretpostavimo da je korisnik izabrao tamnu temu
    val sharedPrefs = getSharedPreferences("AppTheme", Context.MODE_PRIVATE)
    val editor = sharedPrefs.edit()
    editor.putString("current_theme", "dark")
    editor.apply()
    
    // Primena teme odmah
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
    
    // Ili recreate() za potpuno primenjivanje teme
    // recreate()
    

    Restartovanje Activity pomoću recreate() može biti potrebno da bi se svi View-ovi u trenutnoj Activity ispravno osvežili sa novom temom.

  4. Definisanje tema u styles.xml: Definišite stilove za svetlu i tamnu temu u res/values/styles.xml i res/values-night/styles.xml, nasleđujući od Theme.AppCompat.

    <!-- res/values/styles.xml -->
    <style name="AppTheme.Light" parent="Theme.AppCompat.Light">
        <!-- Definicije stilova za svetlu temu -->
    </style>
    
    <!-- res/values-night/styles.xml -->
    <style name="AppTheme.Dark" parent="Theme.AppCompat">
        <!-- Definicije stilova za tamnu temu -->
    </style>
    

    U AndroidManifest.xml navedite osnovnu temu aplikacije:

    <!-- AndroidManifest.xml -->
    <application
        android:theme="@style/AppTheme.Light"
        ...>
        <!-- ... Activities -->
    </application>
    
  5. Praćenje promena teme: U slučaju da se tema menja sistemski (npr., po rasporedu ili ručno od strane korisnika putem podešavanja telefona), možete koristiti Configuration.uiMode u onConfigurationChanged() za reagovanje, ali AppCompatDelegate obično to automatski obrađuje pri korišćenju MODE_NIGHT_FOLLOW_SYSTEM.

    @Override
    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        // Možete proveriti novu temu, ako je potrebno
        // val currentNightMode = newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK
        // ...
    }
    

Korišćenje AppCompatDelegate je preporučeni pristup za podršku tema na Androidu, jer obezbeđuje kompatibilnost unazad i pravilno funkcionisanje sa različitim komponentama biblioteke za podršku.