I have set Light and Dark mode in my app and its working.
But I also want to add an option to let the app follow the mode set by the system. So user can choose among three options.
I tried to do it, but was unsuccessful. I want to add Radio Buttons to switch to Light, Dark, Follow System Mode.
Here's what I have tried
public class setting extends AppCompatActivity {
private Switch darkMode;
Button autoButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
setTheme(R.style.darktheme);
}
else {
setTheme(R.style.AppTheme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Settings");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setTitleTextAppearance(this, R.style.Font);
darkMode = (Switch) findViewById(R.id.darkModeSwitch);
autoButton = (Button) findViewById(R.id.autoButton);
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
darkMode.setChecked(true);
}
darkMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
getDelegate().applyDayNight();
recreate();
//restartApp();
}
else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
recreate();
// restartApp();
}
}
});
}
}
There’s an option that does this: AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM.
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
Related
I implement dark mode with SwitchPreference in the second activity and in the first activity just a button take me to the second activity (Setting) the problem is when the app destroyed and i launched it again the dark mode not implemented until the second activity launched.
//First Activity
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences( this/* Activity context */);
Boolean theme = sharedPreferences.getBoolean("darkMode", false);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent2=new Intent(MainActivity.this,Settings.class);
startActivity(intent2);
}
});
}
}
i have some idea that i should use sharedpreferences but i don't know how can i set it
//Second Activity
public class MySettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(#Nullable Bundle savedInstanceState, #Nullable String rootKey) {
setPreferencesFromResource(R.xml.preferences,rootKey);
SwitchPreference switchPreference=findPreference("darkMode");
if (switchPreference.isChecked()){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
}
Overview:-
You can change dark mode or light mode via this function.
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.HERE_YOUR_MODE);
You can access the mode like this:
AppCompatDelegate.MODE_NIGHT_YES // for dark mode
AppCompatDelegate.MODE_NIGHT_NO // for light mode
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM // for set as your devices theme
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM // when you are in battery saver
Implementation:-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Boolean theme = sharedPreferences.getBoolean("darkMode", false);
if (theme){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); // implement here.
}else{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); // implement here.
}
}
Well as per your code , you are just one step away from achieving result
you just need to use below code in every activity's onCreate method
if (isDarkMode){ // isDarkMode is boolean which we can get from preference
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
```
I'm trying to make a navigation drawer switch which switches between a normal theme and a dark theme of an app, however I can't get the switch to work.
I already have a working switch on mainactivity but I can't get it to work in the navigation drawer.
This is the code to switch between light/dark mode.
#Override
protected void onCreate(Bundle savedInstanceState) {
if(AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_YES) {
setTheme(R.style.HROTheme);
}
else setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
modeswitch=(Switch)findViewById(R.id.switch2);
if (AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_YES) {
modeswitch.setChecked(true);
}
modeswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
recreate();
}
else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
recreate();
}
}
});
You can try to add an item in values/attrs like this: <attr name="bottomback" format="color" />
Then define "bottomback" in styles for your both dark and light styles like this:
<item name="bottomback">#000</item>
That must be different in both styles.
now you can set the backgroundTint of drawer as this: android:backgroundTint="?attr/bottomback"
how to get back hamburg icon when set back icon?
hen I am implementing change toggle icon when fragment on activity ,its set to back icon but need when its back the fragment then set it to again hamburg icon for open drawer of the activity,
this is my code
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
toolbar.setNavigationIcon(R.drawable.ic_action_navigation_arrow_back);
//drawerFragment.mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);// show back button
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
} else {
//show hamburger
///drawerFragment.mDrawerToggle.setDrawerIndicatorEnabled(true);
toolbar.setNavigationIcon(R.drawable.icon1);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
//drawerFragment.mDrawerToggle.syncState();
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.openDrawer(GravityCompat.START);
}
});
}
}
});
If you are using fragment to show content and have the single toolbar inside host activity, you could manage your toolbar.
#Override
public void onResume() {
super.onResume();
ActionBar actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();
actionBar.setTitle("First Fragment");
actionBar.setIcon(R.drawable.back_icon);
}
Do not forget to call
setSupportActionBar(toolbar)
I want to switch between layouts using the physical back button. I found this:
#Override
public void onBackPressed() {
RelativeLayout layout2 = (RelativeLayout)findViewById(R.id.layout2);// check if layout2 is open
if(layout2.getVisibility() == View.VISIBLE){
setContentView(R.layout.main);
return;
}else{
super.onBackPressed();
}
}
Is the checking part correct?
Make RelativeLayout decalre globally so you can access in OnBack() method
RelativeLayout layout2
this line you have to put in the OnCreate() method
layout2 = (RelativeLayout)findViewById(R.id.layout2);//
and finally check visibility of layout
#Override
public void onBackPressed() {
if(layout2.getVisibility() == View.VISIBLE){
setContentView(R.layout.main);
return;
}else{
super.onBackPressed();
}
}
enjoy coding............
Is possible remove the banners in a webview? In my webview i display a forum and there is an annoying banner i want to remove.. Is it possible? Is there a way like adBlock or something like that? My code:
public class MainActivity extends Activity
{
protected WebView webview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
WebSettings websettings = webview.getSettings();
websettings.setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient());
if (savedInstanceState == null)
{
webview.loadUrl("http://www.proboards.com/");
}
}
#Override
protected void onSavedInstanceState(Bundle outState) {
//super.onSavedInstanceState(outState);
webview.saveState(outState);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack() == true)
webview.goBack();
else finish();
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
}
Thanks.
If you want to hide a banner in a specific page, look at the page's source for the banner's id and inject some javascript code (example here) in order to set its visibility to hidden.