banner



How To Set Dark Mode On Android

How to implement Dark (Dark) mode in Android app

Light-on-dark colour scheme, also chosen night style, is a supplemental mode that uses a color scheme in which content of a webpage is displayed on a dark background. Such a color scheme reduces the lite emitted by screens and enhances readability. Switching to dark mode allows website users to move to an heart-friendly and resource-saving pattern whenever they desire.

Dark manner or dark manner has been getting a lot of fizz lately as Google has included it into their Latest Android version i.due east Android Q(API Level 29) and following that more and more apps started supporting night manner natively because information technology has many benefits:

  • Reduces battery/power usage.
  • Improves visibility for users with low vision and those who are sensitive to bright low-cal.
  • Makes it easier to use device in a low-light.

    How to add nighttime mode to your android app?

    1. Create a new Android Project
    2. Create a layout and add a button or switch to toggle On/Off Night Fashion
    3. Now Right click on values and Select Prove in Explorer Option
    4. Now copy the values folder and paste it into the same directory and rename it to "values-night"
    5. Now you'll see 2 colors.xml files, the normal one and 1 with (dark) written to it
    6. Now add these colors to colors.xml for Normal/Low-cal Mode
    7. And add these colors to colors.xml(nighttime)
    8. Add together backgroundColor to your primary Layout
    9. Add textColor to your TextView's
    10. Now simply use AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
      as shown below

      public class MainActivity

      extends AppCompatActivity {

      individual Button btnToggleDark;

      @Override

      protected void onCreate(

      Parcel savedInstanceState)

      {

      super .onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      btnToggleDark

      = findViewById(R.id.btnToggleDark);

      btnToggleDark.setOnClickListener(

      new View.OnClickListener() {

      @Override

      public void onClick(View view)

      {

      AppCompatDelegate

      .setDefaultNightMode(

      AppCompatDelegate

      .MODE_NIGHT_YES);

      }

      });

      }

      }

    11. Salvage the state of the app so that, when the user reopens the app after applying Dark/Light Mode that fashion retains. Nosotros will use SharedPreferences to salvage the land of the app

      public course MainActivity

      extends AppCompatActivity {

      private Button btnToggleDark;

      @SuppressLint ( "SetTextI18n" )

      @Override

      protected void onCreate(

      Packet savedInstanceState)

      {

      super .onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      btnToggleDark

      = findViewById(R.id.btnToggleDark);

      SharedPreferences sharedPreferences

      = getSharedPreferences(

      "sharedPrefs" , MODE_PRIVATE);

      final SharedPreferences.Editor editor

      = sharedPreferences.edit();

      terminal boolean isDarkModeOn

      = sharedPreferences

      .getBoolean(

      "isDarkModeOn" , false );

      if (isDarkModeOn) {

      AppCompatDelegate

      .setDefaultNightMode(

      AppCompatDelegate

      .MODE_NIGHT_YES);

      btnToggleDark.setText(

      "Disable Night Mode" );

      }

      else {

      AppCompatDelegate

      .setDefaultNightMode(

      AppCompatDelegate

      .MODE_NIGHT_NO);

      btnToggleDark

      .setText(

      "Enable Dark Mode" );

      }

      btnToggleDark.setOnClickListener(

      new View.OnClickListener() {

      @Override

      public void onClick(View view)

      {

      if (isDarkModeOn) {

      AppCompatDelegate

      .setDefaultNightMode(

      AppCompatDelegate

      .MODE_NIGHT_NO);

      editor.putBoolean(

      "isDarkModeOn" , false );

      editor.apply();

      btnToggleDark.setText(

      "Enable Night Style" );

      }

      else {

      AppCompatDelegate

      .setDefaultNightMode(

      AppCompatDelegate

      .MODE_NIGHT_YES);

      editor.putBoolean(

      "isDarkModeOn" , true );

      editor.employ();

      btnToggleDark.setText(

      "Disable Dark Fashion" );

      }

      }

      });

      }

      }

    Output:



  • Source: https://www.geeksforgeeks.org/how-to-implement-dark-night-mode-in-android-app/

    Posted by: whitewasell.blogspot.com

    0 Response to "How To Set Dark Mode On Android"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel