Skip to content

Commit

Permalink
Add custom themes via XML for light / dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Nov 16, 2017
1 parent 4c16901 commit d999369
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public NavigationView(Context context, @Nullable AttributeSet attrs) {
public NavigationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
ThemeSwitcher.setTheme(getContext());
ThemeSwitcher.setTheme(getContext(), attrs);
init();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.res.TypedArray;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;

import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.IconFactory;
Expand All @@ -23,16 +24,23 @@ public class ThemeSwitcher {
* and set the theme colors accordingly.
*
* @param context {@link NavigationView} where the theme will be set
* @param attrs holding custom styles if any are set
*/
static void setTheme(Context context) {
static void setTheme(Context context, AttributeSet attrs) {
int uiMode = context.getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
boolean darkThemeEnabled = uiMode == Configuration.UI_MODE_NIGHT_YES;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(context.getString(R.string.dark_theme_enabled), darkThemeEnabled);
editor.apply();
context.setTheme(darkThemeEnabled ? R.style.NavigationViewDark : R.style.NavigationViewLight);

TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.NavigationView);
int lightTheme = array.getResourceId(R.styleable.NavigationView_navigationLightTheme, R.style.NavigationViewLight);
int darkTheme = array.getResourceId(R.styleable.NavigationView_navigationDarkTheme, R.style.NavigationViewDark);
array.recycle();

context.setTheme(darkThemeEnabled ? darkTheme : lightTheme);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
android:id="@+id/navigationView"
android:layout_width="0dp"
android:layout_height="0dp"
app:navigationLightTheme="@style/NavigationViewLight"
app:navigationDarkTheme="@style/NavigationViewDark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
8 changes: 8 additions & 0 deletions libandroid-navigation-ui/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,16 @@
<attr name="navigationViewBannerManeuverPrimary" format="color"/>
<attr name="navigationViewBannerManeuverSecondary" format="color"/>

<!-- Progress Bars -->
<attr name="navigationViewProgress" format="color"/>
<attr name="navigationViewProgressBackground" format="color"/>

<!-- Map style -->
<attr name="navigationViewMapStyle" format="string"/>

<!-- For setting the styles in XML -->
<attr name="navigationLightTheme" format="reference"/>
<attr name="navigationDarkTheme" format="reference"/>

</declare-styleable>
</resources>
4 changes: 4 additions & 0 deletions libandroid-navigation-ui/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

<item name="navigationViewProgress">@color/mapbox_navigation_view_color_progress</item>
<item name="navigationViewProgressBackground">@color/mapbox_navigation_view_color_progress_background</item>

<item name="navigationViewMapStyle">@string/navigation_guidance_day_v2</item>
</style>

<style name="NavigationViewDark" parent="Theme.AppCompat.Light.NoActionBar">
Expand All @@ -48,6 +50,8 @@

<item name="navigationViewProgress">@color/mapbox_navigation_view_color_progress_dark</item>
<item name="navigationViewProgressBackground">@color/mapbox_navigation_view_color_progress_background_dark</item>

<item name="navigationViewMapStyle">@string/navigation_guidance_night_v2</item>
</style>

</resources>

0 comments on commit d999369

Please sign in to comment.