Last active
September 24, 2019 13:07
-
-
Save juniorcesarabreu/21e1c581de235215c5bb6e68e6efa000 to your computer and use it in GitHub Desktop.
This file contains 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
package br.com.revija.cadastroimobiliario.controller.components; | |
/** | |
* Created by Júnior César Abreu <juniorcesarabreu<em>live.com> on 12/10/2017. | |
*/ | |
import android.content.Context; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.util.AttributeSet; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.RelativeLayout; | |
import br.com.revija.cadastroimobiliario.R; | |
public class ClearableEditText extends RelativeLayout { | |
LayoutInflater inflater = null; | |
EditText edit_text; | |
Button btn_clear; | |
public ClearableEditText(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
initViews(); | |
} | |
public ClearableEditText(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
initViews(); | |
} | |
public ClearableEditText(Context context) { | |
super(context); | |
initViews(); | |
} | |
void initViews() { | |
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
inflater.inflate(R.layout.clearable_edit_text, this, true); | |
edit_text = findViewById(R.id.clearable_edit); | |
btn_clear = findViewById(R.id.clearable_button_clear); | |
btn_clear.setVisibility(RelativeLayout.INVISIBLE); | |
clearText(); | |
showHideClearButton(); | |
} | |
void clearText() { | |
btn_clear.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
edit_text.setText(""); | |
} | |
}); | |
} | |
void showHideClearButton() { | |
edit_text.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
if (s.length() > 0) | |
btn_clear.setVisibility(RelativeLayout.VISIBLE); | |
else | |
btn_clear.setVisibility(RelativeLayout.INVISIBLE); | |
} | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
} | |
}); | |
} | |
public Editable getText() { | |
Editable text = edit_text.getText(); | |
return text; | |
} | |
} | |
// Uso | |
//<?xml version="1.0" encoding="utf-8"?> | |
//<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
// android:layout_width="fill_parent" | |
// android:layout_height="fill_parent" > | |
// | |
//<com.and.ab1209.ClearableEditText | |
// android:id="@+id/edit_text_clearable" | |
// android:layout_width="fill_parent" | |
// android:layout_height="wrap_content" /> | |
//</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment