Skip to content

Commit

Permalink
Refactoring to properly support dark theme
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvavra committed Mar 5, 2015
1 parent a0aeeab commit 3626b50
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@ public abstract class BaseDialogFragment extends DialogFragment implements Dialo
private static boolean darkTheme;
protected int mRequestCode;

/**
* @return True if dark theme is to be used
*/
public static boolean isDarkTheme() {
return darkTheme;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Expand Down Expand Up @@ -240,7 +233,6 @@ private boolean isScrollable(ViewGroup listView) {
* This method resolves the current theme declared in the manifest
*/
private void resolveTheme() {
//Try-catch block is used to overcome resource not found exception
try {
TypedValue val = new TypedValue();

Expand Down Expand Up @@ -406,13 +398,6 @@ public View create() {
View vButtonsStacked = content.findViewById(R.id.sdl_buttons_stacked);
ListView vList = (ListView) content.findViewById(R.id.sdl_list);


//Dark theme is enabled
if (isDarkTheme()) {
vTitle.setTextAppearance(mContext, R.style.SDL_TextView_Title_Dark);
vMessage.setTextAppearance(mContext, R.style.SDL_TextView_Message_Dark);
}

Typeface regularFont = TypefaceHelper.get(mContext, "Roboto-Regular");
Typeface mediumFont = TypefaceHelper.get(mContext, "Roboto-Medium");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.avast.android.dialogs.fragment;

import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
Expand All @@ -8,14 +11,10 @@
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.*;

import com.avast.android.dialogs.R;
import com.avast.android.dialogs.core.BaseDialogBuilder;
Expand All @@ -25,9 +24,6 @@
import com.avast.android.dialogs.iface.ISimpleDialogCancelListener;
import com.avast.android.dialogs.util.SparseBooleanArrayParcelable;

import java.util.Arrays;
import java.util.List;

/**
* Dialog with a list of options.
* <p/>
Expand Down Expand Up @@ -76,77 +72,71 @@ public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (getArguments() == null) {
throw new IllegalArgumentException(
"use SimpleListDialogBuilder to construct this dialog");
"use SimpleListDialogBuilder to construct this dialog");
}
}

private ListAdapter prepareAdapter(int itemLayoutId) {
private ListAdapter prepareAdapter(final int itemLayoutId) {
return new ArrayAdapter<Object>(getActivity(),
itemLayoutId,
R.id.sdl_text,
getItems()) {
itemLayoutId,
R.id.sdl_text,
getItems()) {

/**
* This function of list adapter class is overriden to set the dark style attrributes
* to the list view dynamically
*
* @param position
* @param convertView
* @param parent
* @return
* Overriding default implementation because it ignores current light/dark theme.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Dark theme is enabled
if (isDarkTheme()) {
View v = super.getView(position, convertView, parent);
TextView t = (TextView) v.findViewById(R.id.sdl_text);
t.setTextColor(getResources().getColor(R.color.sdl_textColor_primary_dark));
return v;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(itemLayoutId, parent, false);
}
TextView t = (TextView)convertView.findViewById(R.id.sdl_text);
if (t != null) {
t.setText((CharSequence)getItem(position));
}
return super.getView(position, convertView, parent);
return convertView;
}
};
}

private void buildMultiChoice(Builder builder) {
builder.setItems(
prepareAdapter(R.layout.sdl_list_item_multichoice),
asIntArray(getCheckedItems()), AbsListView.CHOICE_MODE_MULTIPLE,
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SparseBooleanArray checkedPositions = ((ListView) parent).getCheckedItemPositions();
setCheckedItems(new SparseBooleanArrayParcelable(checkedPositions));
}
});
prepareAdapter(R.layout.sdl_list_item_multichoice),
asIntArray(getCheckedItems()), AbsListView.CHOICE_MODE_MULTIPLE,
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SparseBooleanArray checkedPositions = ((ListView)parent).getCheckedItemPositions();
setCheckedItems(new SparseBooleanArrayParcelable(checkedPositions));
}
});
}

private void buildSingleChoice(Builder builder) {
builder.setItems(
prepareAdapter(R.layout.sdl_list_item_singlechoice),
asIntArray(getCheckedItems()),
AbsListView.CHOICE_MODE_SINGLE, new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SparseBooleanArray checkedPositions = ((ListView) parent).getCheckedItemPositions();
setCheckedItems(new SparseBooleanArrayParcelable(checkedPositions));
}
});
prepareAdapter(R.layout.sdl_list_item_singlechoice),
asIntArray(getCheckedItems()),
AbsListView.CHOICE_MODE_SINGLE, new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SparseBooleanArray checkedPositions = ((ListView)parent).getCheckedItemPositions();
setCheckedItems(new SparseBooleanArrayParcelable(checkedPositions));
}
});
}

private void buildNormalChoice(Builder builder) {
builder.setItems(
prepareAdapter(R.layout.sdl_list_item), -1,
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (IListDialogListener listener : getSingleDialogListeners()) {
listener.onListItemSelected(getItems()[position], position, mRequestCode);
}
dismiss();
prepareAdapter(R.layout.sdl_list_item), -1,
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (IListDialogListener listener : getSingleDialogListeners()) {
listener.onListItemSelected(getItems()[position], position, mRequestCode);
}
});
dismiss();
}
});
}

@Override
Expand Down Expand Up @@ -415,7 +405,7 @@ public SimpleListDialogBuilder setCancelButtonText(int cancelBttTextResID) {

@Override
public ListDialogFragment show() {
return (ListDialogFragment) super.show();
return (ListDialogFragment)super.show();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ protected Builder build(Builder builder) {
final View view = inflater.inflate(R.layout.sdl_progress, null, false);
final TextView tvMessage = (TextView) view.findViewById(R.id.sdl_message);

//Changing the textview style to dark dynamically if dark theme is in use
if (isDarkTheme()) {
tvMessage.setTextAppearance(getActivity(), R.style.SDL_TextView_Message_Progress_Dark);
}

tvMessage.setText(getArguments().getCharSequence(ARG_MESSAGE));

builder.setView(view);
Expand Down
5 changes: 5 additions & 0 deletions library/src/main/res/drawable-v21/sdl_button_dark.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?colorControlHighlight">
<item android:drawable="@drawable/sdl_button_selector_dark" />
</ripple>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime">
<item android:drawable="@drawable/sdl_button_pressed_dark" android:state_pressed="true" />
<item android:drawable="@drawable/sdl_button_normal" />
</selector>
5 changes: 5 additions & 0 deletions library/src/main/res/drawable-v21/sdl_list_dark.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?colorControlHighlight">
<item android:drawable="@drawable/sdl_list_selector_dark" />
</ripple>
5 changes: 5 additions & 0 deletions library/src/main/res/drawable-v21/sdl_list_selector_dark.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime">
<item android:drawable="@drawable/sdl_list_pressed_dark" android:state_pressed="true" />
<item android:drawable="@drawable/sdl_list_normal" />
</selector>
5 changes: 5 additions & 0 deletions library/src/main/res/drawable/sdl_button_dark.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:exitFadeDuration="@android:integer/config_shortAnimTime" tools:ignore="UnusedAttribute">
<item android:drawable="@drawable/sdl_button_pressed_dark" android:state_pressed="true" />
<item android:drawable="@drawable/sdl_button_normal" />
</selector>
2 changes: 1 addition & 1 deletion library/src/main/res/drawable/sdl_button_pressed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="@color/sdl_pressed" />
<solid android:color="@color/sdl_pressed_light" />
</shape>
6 changes: 6 additions & 0 deletions library/src/main/res/drawable/sdl_button_pressed_dark.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="@color/sdl_pressed_dark" />
</shape>
5 changes: 5 additions & 0 deletions library/src/main/res/drawable/sdl_list_dark.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:exitFadeDuration="@android:integer/config_shortAnimTime" tools:ignore="UnusedAttribute">
<item android:drawable="@drawable/sdl_list_pressed_dark" android:state_pressed="true" />
<item android:drawable="@drawable/sdl_list_normal" />
</selector>
2 changes: 1 addition & 1 deletion library/src/main/res/drawable/sdl_list_pressed.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/sdl_pressed" />
<solid android:color="@color/sdl_pressed_light" />
</shape>
5 changes: 5 additions & 0 deletions library/src/main/res/drawable/sdl_list_pressed_dark.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/sdl_pressed_dark" />
</shape>
42 changes: 42 additions & 0 deletions library/src/main/res/values-v21/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="SDL.Dialog" parent="android:Theme.Material.Light.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:colorAccent">?colorAccent</item>
<item name="android:colorPrimary">?colorPrimary</item>

<item name="sdlTextPrimaryColor">@color/sdl_text_primary_light</item>
<item name="sdlTextSecondaryColor">@color/sdl_text_secondary_light</item>
<item name="sdlDividerColor">@color/sdl_divider_light</item>
<item name="sdlPressedColor">@color/sdl_pressed_light</item>
<item name="sdlButtonBackground">@drawable/sdl_button</item>
<item name="sdlListSelector">@drawable/sdl_list</item>
</style>

<style name="SDL.Dialog.Dark" parent="android:Theme.Material.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:colorAccent">?colorAccent</item>
<item name="android:colorPrimary">?colorPrimary</item>

<item name="sdlTextPrimaryColor">@color/sdl_text_primary_dark</item>
<item name="sdlTextSecondaryColor">@color/sdl_text_secondary_dark</item>
<item name="sdlDividerColor">@color/sdl_divider_dark</item>
<item name="sdlPressedColor">@color/sdl_pressed_dark</item>
<item name="sdlButtonBackground">@drawable/sdl_button_dark</item>
<item name="sdlListSelector">@drawable/sdl_list_dark</item>
</style>
</resources>
11 changes: 11 additions & 0 deletions library/src/main/res/values/sdl_attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="StyledDialogs">
<attr name="sdlTextPrimaryColor" format="color|reference" />
<attr name="sdlTextSecondaryColor" format="color|reference" />
<attr name="sdlDividerColor" format="color|reference" />
<attr name="sdlPressedColor" format="color|reference" />
<attr name="sdlButtonBackground" format="reference" />
<attr name="sdlListSelector" format="reference" />
</declare-styleable>
</resources>
26 changes: 17 additions & 9 deletions library/src/main/res/values/sdl_colors.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Material colors -->
<color name="sdl_text_primary">#DE000000</color>
<!-- 87% -->
<color name="sdl_text_secondary">#8A000000</color>
<!-- 54% -->
<color name="sdl_divider">#1F000000</color>
<!-- 12% -->
<color name="sdl_pressed">#D5D5D5</color>
<color name="sdl_textColor_primary_dark">#FFFFFFFF</color>
<!-- colors taken from: http://www.google.com/design/spec/style/color.html#color-ui-color-application -->
<!-- 87% black -->
<color name="sdl_text_primary_light">#DE000000</color>
<!-- 100% white -->
<color name="sdl_text_primary_dark">#FFFFFFFF</color>
<!-- 54% black-->
<color name="sdl_text_secondary_light">#8A000000</color>
<!-- 70% white -->
<color name="sdl_text_secondary_dark">#B3FFFFFF</color>
<!-- 12 % black -->
<color name="sdl_divider_light">#1F000000</color>
<!-- 12 % white -->
<color name="sdl_divider_dark">#1FFFFFFF</color>
<!-- 16 % black -->
<color name="sdl_pressed_light">#29000000</color>
<!-- 16 % white -->
<color name="sdl_pressed_dark">#29FFFFFF</color>
</resources>
Loading

0 comments on commit 3626b50

Please sign in to comment.