Implement menu item to switch between themes

pull/84/head
Alinson S. Xavier 10 years ago
parent 35f778c376
commit 52c07660b1

@ -29,6 +29,7 @@ import android.view.View;
import android.widget.TextView;
import org.isoron.uhabits.helpers.ColorHelper;
import org.isoron.uhabits.helpers.UIHelper;
public class AboutActivity extends Activity implements View.OnClickListener
{
@ -37,6 +38,8 @@ public class AboutActivity extends Activity implements View.OnClickListener
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
UIHelper.applyCurrentTheme(this);
setContentView(R.layout.about);
if (android.os.Build.VERSION.SDK_INT >= 21)

@ -26,6 +26,7 @@ import android.os.Bundle;
import android.widget.Toast;
import org.isoron.uhabits.commands.Command;
import org.isoron.uhabits.helpers.UIHelper;
import java.util.LinkedList;
@ -44,6 +45,8 @@ abstract public class BaseActivity extends Activity implements Thread.UncaughtEx
{
super.onCreate(savedInstanceState);
UIHelper.applyCurrentTheme(this);
androidExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);

@ -29,17 +29,19 @@ import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import org.isoron.uhabits.helpers.DateHelper;
import org.isoron.uhabits.helpers.UIHelper;
import org.isoron.uhabits.fragments.ListHabitsFragment;
import org.isoron.uhabits.helpers.DateHelper;
import org.isoron.uhabits.helpers.ReminderHelper;
import org.isoron.uhabits.helpers.UIHelper;
import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.tasks.BaseTask;
import org.isoron.uhabits.widgets.CheckmarkWidgetProvider;
@ -70,6 +72,10 @@ public class MainActivity extends BaseActivity
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
setContentView(R.layout.list_habits_activity);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
@ -122,6 +128,10 @@ public class MainActivity extends BaseActivity
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.list_habits_menu, menu);
MenuItem nightModeItem = menu.findItem(R.id.action_night_mode);
nightModeItem.setChecked(UIHelper.isNightMode());
return true;
}
@ -130,6 +140,17 @@ public class MainActivity extends BaseActivity
{
switch (item.getItemId())
{
case R.id.action_night_mode:
{
if(UIHelper.isNightMode())
UIHelper.setCurrentTheme(UIHelper.THEME_LIGHT);
else
UIHelper.setCurrentTheme(UIHelper.THEME_DARK);
refreshTheme();
return true;
}
case R.id.action_settings:
{
Intent intent = new Intent(this, SettingsActivity.class);
@ -158,6 +179,20 @@ public class MainActivity extends BaseActivity
}
}
private void refreshTheme()
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
Intent intent = new Intent(MainActivity.this, MainActivity.class);
MainActivity.this.finish();
startActivity(intent);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{

@ -23,6 +23,7 @@ import android.app.Activity;
import android.os.Bundle;
import org.isoron.uhabits.fragments.SettingsFragment;
import org.isoron.uhabits.helpers.UIHelper;
public class SettingsActivity extends Activity
{
@ -30,6 +31,9 @@ public class SettingsActivity extends Activity
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
UIHelper.applyCurrentTheme(this);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();

@ -19,6 +19,7 @@
package org.isoron.uhabits.helpers;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
@ -36,14 +37,19 @@ import android.view.View;
import android.view.inputmethod.InputMethodManager;
import org.isoron.uhabits.BuildConfig;
import org.isoron.uhabits.HabitsApplication;
import org.isoron.uhabits.R;
import org.isoron.uhabits.commands.Command;
import java.util.Locale;
public abstract class UIHelper
{
public static final String ISORON_NAMESPACE = "http://isoron.org/android";
public static final int THEME_LIGHT = 0;
public static final int THEME_DARK = 1;
private static Typeface fontawesome;
public interface OnSavedListener
@ -190,4 +196,38 @@ public abstract class UIHelper
return bool;
}
public static void applyCurrentTheme(Activity activity)
{
switch(getCurrentTheme())
{
case THEME_DARK:
activity.setTheme(R.style.AppBaseThemeDark);
break;
case THEME_LIGHT:
default:
activity.setTheme(R.style.AppBaseTheme);
break;
}
}
private static int getCurrentTheme()
{
Context appContext = HabitsApplication.getContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
return prefs.getInt("pref_theme", THEME_LIGHT);
}
public static void setCurrentTheme(int theme)
{
Context appContext = HabitsApplication.getContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
prefs.edit().putInt("pref_theme", theme).apply();
}
public static boolean isNightMode()
{
return getCurrentTheme() == THEME_DARK;
}
}

@ -28,6 +28,12 @@
android:enabled="true"
android:title="@string/show_archived"/>
<item
android:id="@+id/action_night_mode"
android:checkable="true"
android:enabled="true"
android:title="@string/night_mode"/>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"

@ -156,4 +156,5 @@
<string name="generate_bug_report">Generate bug report</string>
<string name="troubleshooting">Troubleshooting</string>
<string name="help_translate">Help translate this app</string>
<string name="night_mode">Night mode</string>
</resources>
Loading…
Cancel
Save