|
import android.content.Context; |
|
import android.content.res.TypedArray; |
|
import android.graphics.drawable.Drawable; |
|
import android.os.Build; |
|
import android.support.annotation.Nullable; |
|
import android.support.annotation.RequiresApi; |
|
import android.support.v4.content.ContextCompat; |
|
import android.util.AttributeSet; |
|
import android.view.LayoutInflater; |
|
import android.view.View; |
|
import android.view.ViewGroup; |
|
import android.widget.ImageView; |
|
import android.widget.LinearLayout; |
|
import android.widget.TextView; |
|
|
|
/** |
|
* @author yuana <andhikayuana@gmail.com> |
|
* @since 10/6/17 |
|
*/ |
|
|
|
public class ItemWidgetSettingView extends LinearLayout { |
|
|
|
private View mView; |
|
|
|
private TextView tvLabel; |
|
private TextView tvDescription; |
|
private ImageView ivIcon; |
|
private ImageView ivAction; |
|
private View viewCustomAction; |
|
private LinearLayout llHeader; |
|
|
|
private String strLabel; |
|
private String strDescription; |
|
private Drawable drawableIcon; |
|
private Drawable drawableActionIcon; |
|
private int colorLabel; |
|
private int colorDescription; |
|
|
|
public ItemWidgetSettingView(Context context) { |
|
this(context, null); |
|
} |
|
|
|
public ItemWidgetSettingView(Context context, @Nullable AttributeSet attrs) { |
|
this(context, attrs, 0); |
|
} |
|
|
|
public ItemWidgetSettingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
|
super(context, attrs, defStyleAttr); |
|
init(context, attrs); |
|
} |
|
|
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) |
|
public ItemWidgetSettingView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
|
super(context, attrs, defStyleAttr, defStyleRes); |
|
init(context, attrs); |
|
} |
|
|
|
@Override |
|
protected void onFinishInflate() { |
|
super.onFinishInflate(); |
|
initView(mView); |
|
|
|
displayAttrs(); |
|
} |
|
|
|
private void displayAttrs() { |
|
setLabel(strLabel); |
|
setDescription(strDescription); |
|
setIcon(drawableIcon); |
|
setActionIcon(drawableActionIcon); |
|
setLabelColor(colorLabel); |
|
setDescriptionColor(colorDescription); |
|
} |
|
|
|
private void init(Context context, AttributeSet attrs) { |
|
|
|
inflateView(); |
|
|
|
TypedArray a = context.getTheme().obtainStyledAttributes( |
|
attrs, |
|
R.styleable.ItemWidgetSettingView, |
|
0, 0); |
|
|
|
try { |
|
strLabel = a.getString(R.styleable.ItemWidgetSettingView_settingLabel); |
|
strDescription = a.getString(R.styleable.ItemWidgetSettingView_settingDescription); |
|
drawableActionIcon = a.getDrawable(R.styleable.ItemWidgetSettingView_settingActionIcon); |
|
drawableIcon = a.getDrawable(R.styleable.ItemWidgetSettingView_settingIcon); |
|
colorLabel = a.getColor(R.styleable.ItemWidgetSettingView_settingLabelColor, |
|
getColorDefault(context)); |
|
colorDescription = a.getColor(R.styleable.ItemWidgetSettingView_settingDescriptionColor, |
|
getColorDefault(context)); |
|
} finally { |
|
a.recycle(); |
|
} |
|
} |
|
|
|
private int getColorDefault(Context context) { |
|
return ContextCompat.getColor(context, android.R.color.darker_gray); |
|
} |
|
|
|
private void inflateView() { |
|
mView = getInflater().inflate(R.layout.view_item_widget_setting, this); |
|
} |
|
|
|
private LayoutInflater getInflater() { |
|
return (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
|
} |
|
|
|
private void initView(View view) { |
|
tvLabel = (TextView) findViewById(R.id.tvLabel); |
|
ivIcon = (ImageView) findViewById(R.id.ivIcon); |
|
ivAction = (ImageView) findViewById(R.id.ivAction); |
|
tvDescription = (TextView) findViewById(R.id.tvDescription); |
|
llHeader = (LinearLayout) findViewById(R.id.llHeader); |
|
} |
|
|
|
private void removeDefaultAction() { |
|
((ViewGroup) ivAction.getParent()).removeView(ivAction); |
|
} |
|
|
|
public View getCustomViewAction() { |
|
if (viewCustomAction == null) |
|
throw new IllegalStateException("Please setCustomViewAction first!"); |
|
return viewCustomAction; |
|
} |
|
|
|
public void setCustomViewAction(View customView) { |
|
removeDefaultAction(); |
|
llHeader.addView(customView); |
|
viewCustomAction = customView; |
|
} |
|
|
|
public void setLabel(String label) { |
|
tvLabel.setText(label); |
|
invalidateAndRequestLayout(); |
|
} |
|
|
|
public void setLabel(int label) { |
|
tvLabel.setText(label); |
|
invalidateAndRequestLayout(); |
|
} |
|
|
|
public void setDescription(String description) { |
|
tvDescription.setText(description); |
|
invalidateAndRequestLayout(); |
|
} |
|
|
|
public void setDescription(int description) { |
|
tvDescription.setText(description); |
|
invalidateAndRequestLayout(); |
|
} |
|
|
|
public void setActionIcon(Drawable drawableActionIcon) { |
|
ivAction.setImageDrawable(drawableActionIcon); |
|
invalidateAndRequestLayout(); |
|
} |
|
|
|
public void setIcon(Drawable drawableIcon) { |
|
ivIcon.setImageDrawable(drawableIcon); |
|
invalidateAndRequestLayout(); |
|
} |
|
|
|
public void setDescriptionColor(int colorDescription) { |
|
tvDescription.setTextColor(colorDescription); |
|
invalidateAndRequestLayout(); |
|
} |
|
|
|
public void setLabelColor(int colorLabel) { |
|
tvLabel.setTextColor(colorLabel); |
|
invalidateAndRequestLayout(); |
|
} |
|
|
|
private void invalidateAndRequestLayout() { |
|
invalidate(); |
|
requestLayout(); |
|
} |
|
|
|
} |