Skip to content

Commit

Permalink
Fix errors in preference layout preview.
Browse files Browse the repository at this point in the history
  • Loading branch information
kizitonwose committed Feb 21, 2019
1 parent f62c765 commit a9ad04b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 53 deletions.
36 changes: 0 additions & 36 deletions CHANGELOG.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View rootView = layoutInflater.inflate(R.layout.dialog_colors, null);

colorGrid = (GridLayout) rootView.findViewById(R.id.color_grid);
colorGrid = rootView.findViewById(R.id.color_grid);
colorGrid.setColumnCount(numColumns);
repopulateItems();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private void initAttrs(AttributeSet attrs, int defStyle) {
TypedArray a = getContext().getTheme().obtainStyledAttributes(
attrs, R.styleable.ColorPreference, defStyle, defStyle);

PreviewSize previewSize = PreviewSize.NORMAL;
PreviewSize previewSize;
try {
numColumns = a.getInteger(R.styleable.ColorPreference_numColumns, numColumns);
colorShape = ColorShape.getShape(a.getInteger(R.styleable.ColorPreference_colorShape, 1));
Expand All @@ -56,8 +56,10 @@ private void initAttrs(AttributeSet attrs, int defStyle) {
@Override
protected void onBindView(View view) {
super.onBindView(view);
ImageView previewView = (ImageView) view.findViewById(R.id.color_view);
ColorUtils.setColorViewValue(previewView, value, false, colorShape);
ImageView colorView = view.findViewById(R.id.color_view);
if (colorView != null) {
ColorUtils.setColorViewValue(colorView, value, false, colorShape);
}
}

public void setValue(int value) {
Expand Down
20 changes: 12 additions & 8 deletions core/src/main/java/com/kizitonwose/colorpreference/ColorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,22 @@ public static void showDialog(Context context, ColorDialog.OnColorSelectedListen
fragment.setOnColorSelectedListener(listener);

Activity activity = Utils.resolveContext(context);
activity.getFragmentManager().beginTransaction()
.add(fragment, tag)
.commit();
if (activity != null) {
activity.getFragmentManager()
.beginTransaction()
.add(fragment, tag)
.commit();
}
}

public static void attach(Context context, ColorDialog.OnColorSelectedListener listener, String tag) {
Activity activity = Utils.resolveContext(context);
ColorDialog fragment = (ColorDialog) activity
.getFragmentManager().findFragmentByTag(tag);
if (fragment != null) {
// re-bind preference to fragment
fragment.setOnColorSelectedListener(listener);
if (activity != null) {
ColorDialog fragment = (ColorDialog) activity.getFragmentManager().findFragmentByTag(tag);
if (fragment != null) {
// re-bind preference to fragment
fragment.setOnColorSelectedListener(listener);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/kizitonwose/colorpreference/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* @author Kizito Nwose
*/

public class Utils {
class Utils {
@Nullable
public static Activity resolveContext(Context context) {
static Activity resolveContext(Context context) {
if (context instanceof Activity) {
return (Activity) context;
} else if (context instanceof ContextWrapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private void initAttrs(AttributeSet attrs, int defStyle) {
TypedArray a = getContext().getTheme().obtainStyledAttributes(
attrs, R.styleable.ColorPreferenceCompat, defStyle, defStyle);

PreviewSize previewSize = PreviewSize.NORMAL;
PreviewSize previewSize;
try {
numColumns = a.getInteger(R.styleable.ColorPreferenceCompat_numColumns, numColumns);
colorShape = ColorShape.getShape(a.getInteger(R.styleable.ColorPreferenceCompat_colorShape, 1));
Expand All @@ -63,8 +63,10 @@ private void initAttrs(AttributeSet attrs, int defStyle) {
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
ImageView previewView = (ImageView) holder.findViewById(R.id.color_view);
ColorUtils.setColorViewValue(previewView, value, false, colorShape);
ImageView colorView = (ImageView) holder.findViewById(R.id.color_view);
if (colorView != null) {
ColorUtils.setColorViewValue(colorView, value, false, colorShape);
}
}

public void setValue(int value) {
Expand Down

0 comments on commit a9ad04b

Please sign in to comment.