Skip to content

Instantly share code, notes, and snippets.

@andhikayuana
Last active October 8, 2017 00:35
Show Gist options
  • Save andhikayuana/950d1dc5a75b792eb25344f584accb0e to your computer and use it in GitHub Desktop.
Save andhikayuana/950d1dc5a75b792eb25344f584accb0e to your computer and use it in GitHub Desktop.
ItemWidgetSettingView.java

How to use

you can use like this

<com.anu.ItemWidgetSettingView
  android:id="@+id/settingNotification"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="?attr/selectableItemBackground"
  app:settingActionIcon="@drawable/ic_setting_arrow_right"
  app:settingDescription="@string/txt_setting_notification_description"
  app:settingDescriptionColor="@color/black"
  app:settingIcon="@drawable/ic_setting_notification"
  app:settingLabel="@string/txt_setting_notification"
  app:settingLabelColor="@color/colorAccent" />
ItemWidgetSettingView itemSettingView = (ItemWidgetSettingView) findViewById(R.id.settingNotification);

/**
 * available method
 */
itemSettingView.setLabel(R.string.your_label);
itemSettingView.setLabel("string label");
itemSettingView.setDescription(R.string.your_descsription);
itemSettingView.setDescription("string description");
itemSettingView.setIcon(ContextCompat.getDrawable(this, R.drawable.your_icon));
itemSettingView.setActionIcon(ContextCompat.getDrawable(this, R.drawable.your_action_icon));
itemSettingView.setLabelColor(R.color.your_color);
itemSettingView.setDescriptionColor(R.color.your_color);

/**
 * custom action here
 * you can pass all extends view like this
 */
Switch switchNotification = new Switch(this);
switchNotification.setChecked(true);
switchNotification.setOnCheckedChangeListener((buttonView, isChecked) -> {
  //your implementation
});

itemSettingView.setCustomViewAction(switchNotification);

// or you can get custom view like this
Switch customViewAction = (Switch) settingNotification.getCustomViewAction();
customViewAction.setChecked(false);
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ItemWidgetSettingView">
<attr name="settingLabel" format="string" />
<attr name="settingIcon" format="integer" />
<attr name="settingDescription" format="string" />
<attr name="settingActionIcon" format="integer" />
<attr name="settingLabelColor" format="color" />
<attr name="settingDescriptionColor" format="color" />
</declare-styleable>
</resources>
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();
}
}
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="@+id/llHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/ivIcon"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_gravity="center"
android:layout_weight="0.1"
android:src="@drawable/ic_edit" />
<TextView
android:id="@+id/tvLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:ellipsize="end"
android:text="Notifikasi"
android:textSize="14sp" />
<ImageView
android:id="@+id/ivAction"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_gravity="center"
android:layout_weight="0.1"
android:src="@drawable/ic_setting_arrow_right" />
</LinearLayout>
<TextView
android:id="@+id/tvDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dapatkan notifikasi ketika mendapatkan pesan"
android:textSize="12sp" />
</LinearLayout>
</merge>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment