Created
June 10, 2019 12:23
-
-
Save kujyp/328b8dfb15fe66b20bf415863c27d136 to your computer and use it in GitHub Desktop.
Android Switch with TextView(Left-side)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="TextSwitch"> | |
<attr name="switchText" format="string" /> | |
</declare-styleable> | |
</resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
> | |
<android.support.v7.widget.SwitchCompat | |
android:id="@+id/textswitch_switch" | |
android:layout_height="match_parent" | |
android:layout_width="wrap_content" | |
/> | |
<TextView | |
android:id="@+id/textswitch_textview" | |
android:layout_height="match_parent" | |
android:layout_width="0dp" | |
android:gravity="center" | |
android:layout_weight="1" | |
/> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.support.v7.widget.SwitchCompat; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.Checkable; | |
import android.widget.CompoundButton; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
public class TextSwitch extends LinearLayout implements Checkable { | |
private static final String TAG = "TextSwitch"; | |
private String mSwitchText; | |
private TextView mTextView = null; | |
private SwitchCompat mSwitch = null; | |
public TextSwitch(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
Log.d(TAG, "TextSwitch: "); | |
TypedArray typedArray = context.getTheme().obtainStyledAttributes( | |
attrs, | |
R.styleable.TextSwitch, | |
0, 0 | |
); | |
try { | |
mSwitchText = typedArray.getString(R.styleable.TextSwitch_switchText); | |
} finally { | |
typedArray.recycle(); | |
} | |
initViewWith(context); | |
} | |
private void initViewWith(Context context) { | |
Log.d(TAG, "initViewWith: "); | |
LayoutInflater layoutInflater = LayoutInflater.from(context); | |
View inflated = layoutInflater.inflate(R.layout.text_switch, this, true); | |
mTextView = inflated.findViewById(R.id.textswitch_textview); | |
mSwitch = inflated.findViewById(R.id.textswitch_switch); | |
setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
toggle(); | |
} | |
}); | |
Log.d(TAG, "initViewWith() called with: mSwitchText = [" + mSwitchText + "]"); | |
mTextView.setText(mSwitchText); | |
} | |
@Override | |
public void setChecked(boolean checked) { | |
Log.d(TAG, "setChecked() called with: checked = [" + checked + "]"); | |
if (mSwitch == null) { | |
Log.d(TAG, "setChecked() called with: mSwitch = [" + mSwitch + "]"); | |
return; | |
} | |
mSwitch.setChecked(checked); | |
} | |
@Override | |
public boolean isChecked() { | |
Log.d(TAG, "isChecked: "); | |
if (mSwitch == null) { | |
Log.d(TAG, "isChecked() called with: mSwitch = [" + mSwitch + "]"); | |
return false; | |
} | |
return mSwitch.isChecked(); | |
} | |
@Override | |
public void toggle() { | |
Log.d(TAG, "toggle: "); | |
if (mSwitch == null) { | |
Log.d(TAG, "toggle() called with: mSwitch = [" + mSwitch + "]"); | |
return; | |
} | |
mSwitch.toggle(); | |
} | |
public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) { | |
Log.d(TAG, "setOnCheckedChangeListener: "); | |
if (mSwitch == null) { | |
Log.d(TAG, "toggle() called with: mSwitch = [" + mSwitch + "]"); | |
return; | |
} | |
mSwitch.setOnCheckedChangeListener(listener); | |
} | |
public void setSwitchText(String text) { | |
mSwitchText = text; | |
invalidate(); | |
requestLayout(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment