Skip to content

Instantly share code, notes, and snippets.

@tolmachevroman
Last active April 26, 2024 11:01
Show Gist options
  • Save tolmachevroman/a3aea4dfe00cd9065fdf to your computer and use it in GitHub Desktop.
Save tolmachevroman/a3aea4dfe00cd9065fdf to your computer and use it in GitHub Desktop.
Custom View for Toggle Edit Text
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ToggleEditText">
<attr name="label" format="string" />
<attr name="hint" format="string" />
</declare-styleable>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toggle"
android:paddingRight="10dip"
android:checked="false"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view"
android:layout_gravity="center_vertical"
android:textColor="@color/login_grey_text"
android:textSize="18sp"
android:textStyle="bold"
android:paddingLeft="10dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/toggle" />
</RelativeLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="100dip"
android:id="@+id/edit_text"
android:visibility="gone"
android:gravity="left|top"
android:layout_margin="8dip"
android:inputType="textMultiLine"/>
</LinearLayout>
package com.test.app;
import com.test.app.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class ToggleEditText extends RelativeLayout {
private TextView textView;
private ToggleButton toggle;
private EditText editText;
private String label;
private String hint;
public ToggleEditText(Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.toggle_edit_text, this);
}
public ToggleEditText(Context context, AttributeSet attrs) {
super(context, attrs);
initViews(context, attrs);
}
public ToggleEditText(Context context, AttributeSet attrs, int defStyle) {
this(context, attrs);
initViews(context, attrs);
}
private void initViews(Context context, AttributeSet attrs) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ToggleEditText, 0, 0);
try {
// get the text and colors specified using the names in attrs.xml
label = a.getString(R.styleable.ToggleEditText_label);
hint = a.getString(R.styleable.ToggleEditText_hint);
} finally {
a.recycle();
}
LayoutInflater.from(context).inflate(R.layout.toggle_edit_text, this);
//text view
textView = (TextView) this.findViewById(R.id.text_view);
textView.setText(label);
//edit text
editText = (EditText) this.findViewById(R.id.edit_text);
editText.setHint(hint);
//toggle button
toggle = (ToggleButton) this.findViewById(R.id.toggle);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editText.setVisibility(isChecked? View.VISIBLE : View.GONE);
}
});
}
public boolean isChecked() {
return toggle.isChecked();
}
public String getText() {
return editText.getText().toString();
}
}
@ilacernenko12
Copy link

hh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment