Skip to content

Instantly share code, notes, and snippets.

@Arjun-sna
Last active June 23, 2018 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arjun-sna/7be4fcbcb38bc32650906f364c0869f5 to your computer and use it in GitHub Desktop.
Save Arjun-sna/7be4fcbcb38bc32650906f364c0869f5 to your computer and use it in GitHub Desktop.
Switch with TextView using merge layout
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/item_switch"
android:layout_toStartOf="@+id/item_switch"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true">
<TextView
app:fontName="Font-Medium.ttf"
android:textSize="16.0sp"
android:textColor="?attr/colorPrimary"
android:id="@+id/setting_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
app:fontName="Font-Normal.ttf"
android:textSize="14.0sp"
android:textColor="@color/grey13"
android:id="@+id/setting_item_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.SwitchCompat
android:id="@+id/setting_item_switch"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true" />
</merge>
public class SettingWithSwitch extends RelativeLayout {
TextView settingTitle;
TextView settingState;
SwitchCompat toggleSwitch;
public SettingWithSwitch(Context context) {
super(context);
init(context, null, 0, 0);
}
public SettingWithSwitch(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0, 0);
}
public SettingWithSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr, 0);
}
@TargetApi(21)
public SettingWithSwitch(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr, defStyleRes);
}
private void init(Context context, AttributeSet attributeSet, int defStyleAttr, int defStyleRes){
LayoutInflater.from(context).inflate(R.layout.settings_item_with_switch, this, true);
TypedArray values = context.getTheme().obtainStyledAttributes(attributeSet
, R.styleable.SettingWithSwitch
, defStyleAttr
, defStyleRes);
settingTitle = (TextView) findViewById(R.id.setting_item_title);
settingState = (TextView) findViewById(R.id.setting_item_detail);
toggleSwitch = (SwitchCompat) findViewById(R.id.setting_item_switch);
String title;
String detail;
boolean value;
boolean switchVisible;
Integer background;
try {
title = values.getString(R.styleable.SettingWithSwitch_settingTitle);
detail = values.getString(R.styleable.SettingWithSwitch_settingDetail);
value = values.getBoolean(R.styleable.SettingWithSwitch_value, true);
switchVisible = values.getBoolean(R.styleable.SettingWithSwitch_switchVisible, true);
background = values.getResourceId(R.styleable.SettingWithSwitch_bg, -1);
} finally {
values.recycle();
}
settingTitle.setText(title);
toggleSwitch.setChecked(value);
if(switchVisible){
toggleSwitch.setVisibility(VISIBLE);
}
if(detail == null){
settingState.setVisibility(GONE);
} else {
settingState.setText(detail);
}
setBackgroundResource(background);
}
public boolean isChecked(){
return this.toggleSwitch.isChecked();
}
public void setChecked(boolean value){
this.toggleSwitch.setChecked(value);
}
public void setTitle(String title){
this.settingTitle.setText(title);
}
public void setTitle(int resId){
this.settingTitle.setText(resId);
}
public void setDetail(String detail){
this.settingState.setText(detail);
}
public void setDetail(int resId){
this.settingState.setText(resId);
}
public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener onCheckedChangeListener){
this.toggleSwitch.setOnCheckedChangeListener(onCheckedChangeListener);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment