From ca9d56e59ed9644c4297b4688bf4e38998e53436 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Sun, 12 Jun 2016 18:22:04 -0400 Subject: [PATCH] Fix toolbars --- .../org/isoron/uhabits/ui/BaseScreen.java | 58 +++++++++++++++++-- .../uhabits/ui/about/AboutActivity.java | 21 ++++--- .../ui/habits/show/ShowHabitActivity.java | 24 ++++---- .../uhabits/ui/settings/SettingsActivity.java | 11 ++-- .../uhabits/ui/settings/SettingsFragment.java | 16 ++--- 5 files changed, 84 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/org/isoron/uhabits/ui/BaseScreen.java b/app/src/main/java/org/isoron/uhabits/ui/BaseScreen.java index f6df69818..bd803cc4a 100644 --- a/app/src/main/java/org/isoron/uhabits/ui/BaseScreen.java +++ b/app/src/main/java/org/isoron/uhabits/ui/BaseScreen.java @@ -31,6 +31,7 @@ import android.support.v7.widget.Toolbar; import android.view.*; import android.widget.*; +import org.isoron.uhabits.*; import org.isoron.uhabits.tasks.ProgressBar; import org.isoron.uhabits.utils.*; @@ -60,6 +61,39 @@ public abstract class BaseScreen this.activity = activity; } + @Deprecated + public static void setupActionBarColor(AppCompatActivity activity, + int color) + { + + Toolbar toolbar = (Toolbar) activity.findViewById(R.id.toolbar); + if (toolbar == null) return; + + activity.setSupportActionBar(toolbar); + + ActionBar actionBar = activity.getSupportActionBar(); + if (actionBar == null) return; + + actionBar.setDisplayHomeAsUpEnabled(true); + + ColorDrawable drawable = new ColorDrawable(color); + actionBar.setBackgroundDrawable(drawable); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) + { + int darkerColor = ColorUtils.mixColors(color, Color.BLACK, 0.75f); + activity.getWindow().setStatusBarColor(darkerColor); + + toolbar.setElevation(InterfaceUtils.dpToPixels(activity, 2)); + + View view = activity.findViewById(R.id.toolbarShadow); + if (view != null) view.setVisibility(View.GONE); + + view = activity.findViewById(R.id.headerShadow); + if (view != null) view.setVisibility(View.GONE); + } + } + /** * Ends the current selection operation. */ @@ -210,6 +244,7 @@ public abstract class BaseScreen int color = rootView.getToolbarColor(); setActionBarColor(actionBar, color); setStatusBarColor(color); + setupToolbarElevation(toolbar); } private void setActionBarColor(@NonNull ActionBar actionBar, int color) @@ -220,12 +255,23 @@ public abstract class BaseScreen private void setStatusBarColor(int baseColor) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) - { - int darkerColor = - ColorUtils.mixColors(baseColor, Color.BLACK, 0.75f); - activity.getWindow().setStatusBarColor(darkerColor); - } + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return; + + int darkerColor = ColorUtils.mixColors(baseColor, Color.BLACK, 0.75f); + activity.getWindow().setStatusBarColor(darkerColor); + } + + private void setupToolbarElevation(Toolbar toolbar) + { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return; + + toolbar.setElevation(InterfaceUtils.dpToPixels(activity, 2)); + + View view = activity.findViewById(R.id.toolbarShadow); + if (view != null) view.setVisibility(View.GONE); + + view = activity.findViewById(R.id.headerShadow); + if (view != null) view.setVisibility(View.GONE); } private class ActionModeWrapper implements ActionMode.Callback diff --git a/app/src/main/java/org/isoron/uhabits/ui/about/AboutActivity.java b/app/src/main/java/org/isoron/uhabits/ui/about/AboutActivity.java index e1c7600a0..8e1b49e14 100644 --- a/app/src/main/java/org/isoron/uhabits/ui/about/AboutActivity.java +++ b/app/src/main/java/org/isoron/uhabits/ui/about/AboutActivity.java @@ -19,16 +19,15 @@ package org.isoron.uhabits.ui.about; -import android.content.Intent; -import android.net.Uri; -import android.os.Bundle; -import android.view.View; -import android.widget.TextView; +import android.content.*; +import android.net.*; +import android.os.*; +import android.view.*; +import android.widget.*; -import org.isoron.uhabits.BuildConfig; -import org.isoron.uhabits.R; -import org.isoron.uhabits.ui.BaseActivity; -import org.isoron.uhabits.utils.InterfaceUtils; +import org.isoron.uhabits.*; +import org.isoron.uhabits.ui.*; +import org.isoron.uhabits.utils.*; /** * Activity that allows the user to see information about the app itself. @@ -76,11 +75,11 @@ public class AboutActivity extends BaseActivity implements View.OnClickListener super.onCreate(savedInstanceState); setContentView(R.layout.about); -// setupSupportActionBar(true); int color = InterfaceUtils.getStyledColor(this, R.attr.aboutScreenColor); -// setupActionBarColor(color); + + BaseScreen.setupActionBarColor(this, color); TextView tvVersion = (TextView) findViewById(R.id.tvVersion); TextView tvRate = (TextView) findViewById(R.id.tvRate); diff --git a/app/src/main/java/org/isoron/uhabits/ui/habits/show/ShowHabitActivity.java b/app/src/main/java/org/isoron/uhabits/ui/habits/show/ShowHabitActivity.java index 365495876..4825f1fa4 100644 --- a/app/src/main/java/org/isoron/uhabits/ui/habits/show/ShowHabitActivity.java +++ b/app/src/main/java/org/isoron/uhabits/ui/habits/show/ShowHabitActivity.java @@ -19,21 +19,21 @@ package org.isoron.uhabits.ui.habits.show; -import android.content.ContentUris; -import android.net.Uri; -import android.os.Bundle; -import android.support.v7.app.ActionBar; +import android.content.*; +import android.net.*; +import android.os.*; +import android.support.v7.app.*; -import org.isoron.uhabits.HabitsApplication; -import org.isoron.uhabits.R; -import org.isoron.uhabits.models.Habit; -import org.isoron.uhabits.models.HabitList; -import org.isoron.uhabits.ui.BaseActivity; +import org.isoron.uhabits.*; +import org.isoron.uhabits.models.*; +import org.isoron.uhabits.ui.*; +import org.isoron.uhabits.utils.*; -import javax.inject.Inject; +import javax.inject.*; /** * Activity that allows the user to see more information about a single habit. + * * Shows all the metadata for the habit, in addition to several charts. */ public class ShowHabitActivity extends BaseActivity @@ -53,7 +53,8 @@ public class ShowHabitActivity extends BaseActivity habit = habitList.getById(ContentUris.parseId(data)); setContentView(R.layout.show_habit_activity); -// setupSupportActionBar(true); + BaseScreen.setupActionBarColor(this, ColorUtils.getColor(this, habit.getColor())); + setupHabitActionBar(); } @@ -65,7 +66,6 @@ public class ShowHabitActivity extends BaseActivity if (actionBar == null) return; actionBar.setTitle(habit.getName()); -// setupActionBarColor(ColorUtils.getColor(this, habit.color)); } public Habit getHabit() diff --git a/app/src/main/java/org/isoron/uhabits/ui/settings/SettingsActivity.java b/app/src/main/java/org/isoron/uhabits/ui/settings/SettingsActivity.java index aebebc8aa..c281f23d8 100644 --- a/app/src/main/java/org/isoron/uhabits/ui/settings/SettingsActivity.java +++ b/app/src/main/java/org/isoron/uhabits/ui/settings/SettingsActivity.java @@ -19,11 +19,11 @@ package org.isoron.uhabits.ui.settings; -import android.os.Bundle; +import android.os.*; -import org.isoron.uhabits.R; -import org.isoron.uhabits.ui.BaseActivity; -import org.isoron.uhabits.utils.InterfaceUtils; +import org.isoron.uhabits.*; +import org.isoron.uhabits.ui.*; +import org.isoron.uhabits.utils.*; /** * Activity that allows the user to view and modify the app settings. @@ -35,10 +35,9 @@ public class SettingsActivity extends BaseActivity { super.onCreate(savedInstanceState); setContentView(R.layout.settings_activity); -// setupSupportActionBar(true); int color = InterfaceUtils.getStyledColor(this, R.attr.aboutScreenColor); -// setupActionBarColor(color); + BaseScreen.setupActionBarColor(this, color); } } diff --git a/app/src/main/java/org/isoron/uhabits/ui/settings/SettingsFragment.java b/app/src/main/java/org/isoron/uhabits/ui/settings/SettingsFragment.java index b5f9e0d72..40bcd8c96 100644 --- a/app/src/main/java/org/isoron/uhabits/ui/settings/SettingsFragment.java +++ b/app/src/main/java/org/isoron/uhabits/ui/settings/SettingsFragment.java @@ -75,17 +75,11 @@ public class SettingsFragment extends PreferenceFragmentCompat private void setResultOnPreferenceClick(String key, final int result) { Preference pref = findPreference(key); - pref.setOnPreferenceClickListener( - new Preference.OnPreferenceClickListener() - { - @Override - public boolean onPreferenceClick(Preference preference) - { - getActivity().setResult(result); - getActivity().finish(); - return true; - } - }); + pref.setOnPreferenceClickListener(preference -> { + getActivity().setResult(result); + getActivity().finish(); + return true; + }); } @Override