Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
Add applyStyle() method so view can change style dynamically.
Browse files Browse the repository at this point in the history
  • Loading branch information
rey5137 committed Apr 2, 2015
1 parent 2971664 commit 8b1f673
Show file tree
Hide file tree
Showing 14 changed files with 365 additions and 245 deletions.
10 changes: 10 additions & 0 deletions lib/src/main/java/com/rey/material/util/ThemeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.v7.internal.widget.TintTypedArray;
import android.util.TypedValue;

import com.rey.material.R;
Expand Down Expand Up @@ -121,4 +122,13 @@ public static int getType(TypedArray array, int index){
}
}

public static CharSequence getString(TypedArray array, int index, CharSequence defaultValue){
String result = array.getString(index);
return result == null ? defaultValue : result;
}

public static CharSequence getString(TintTypedArray array, int index, CharSequence defaultValue){
String result = array.getString(index);
return result == null ? defaultValue : result;
}
}
16 changes: 12 additions & 4 deletions lib/src/main/java/com/rey/material/widget/Button.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@ public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyl
}

private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
mRippleManager.onCreate(this, context, attrs, defStyleAttr, defStyleRes);
}

@Override
applyStyle(context, attrs, defStyleAttr, defStyleRes);
}

public void applyStyle(int resId){
applyStyle(getContext(), null, 0, resId);
}

private void applyStyle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
mRippleManager.onCreate(this, context, attrs, defStyleAttr, defStyleRes);
}

@Override
public void setOnClickListener(OnClickListener l) {
if(l == mRippleManager)
super.setOnClickListener(l);
Expand Down
12 changes: 10 additions & 2 deletions lib/src/main/java/com/rey/material/widget/CheckBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ public CheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defSt
}

private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
CheckBoxDrawable drawable = new CheckBoxDrawable.Builder(context, attrs, defStyleAttr, defStyleRes).build();
drawable.setInEditMode(isInEditMode());
applyStyle(context, attrs, defStyleAttr, defStyleRes);
}

public void applyStyle(int resId){
applyStyle(getContext(), null, 0, resId);
}

private void applyStyle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
CheckBoxDrawable drawable = new CheckBoxDrawable.Builder(context, attrs, defStyleAttr, defStyleRes).build();
drawable.setInEditMode(isInEditMode());
drawable.setAnimEnable(false);
setButtonDrawable(drawable);
drawable.setAnimEnable(true);
Expand Down
12 changes: 10 additions & 2 deletions lib/src/main/java/com/rey/material/widget/CheckedTextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,16 @@ public CheckedTextView(Context context, AttributeSet attrs, int defStyleAttr, in
}

private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
mRippleManager.onCreate(this, context, attrs, defStyleAttr, defStyleRes);
}
applyStyle(context, attrs, defStyleAttr, defStyleRes);
}

public void applyStyle(int resId){
applyStyle(getContext(), null, 0, resId);
}

private void applyStyle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
mRippleManager.onCreate(this, context, attrs, defStyleAttr, defStyleRes);
}

@Override
public void setOnClickListener(OnClickListener l) {
Expand Down
11 changes: 9 additions & 2 deletions lib/src/main/java/com/rey/material/widget/CompoundButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
mRippleManager.onCreate(this, context, attrs, defStyleAttr, defStyleRes);

//a fix to reset paddingLeft attribute
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1){
TypedArray a = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.padding, android.R.attr.paddingLeft}, defStyleAttr, defStyleRes);
Expand All @@ -53,7 +51,16 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr, int def
}

setClickable(true);
applyStyle(context, attrs, defStyleAttr, defStyleRes);
}

public void applyStyle(int resId){
applyStyle(getContext(), null, 0, resId);
}

private void applyStyle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
mRippleManager.onCreate(this, context, attrs, defStyleAttr, defStyleRes);
}

@Override
public void setOnClickListener(OnClickListener l) {
Expand Down
Loading

0 comments on commit 8b1f673

Please sign in to comment.