edittextpreference - ImageButton in EditText -
what code make image button stay @ top right of edittext when scrolled instead of following data input causing messy appearance?
i create custom component, extending relativelayout
, apply custom layout.
have @ article, can desired result few edit of code: https://arunbadole1209.wordpress.com/2011/12/16/how-to-create-edittext-with-crossx-button-at-end-of-it/
clearable_edit_text.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <edittext android:id="@+id/clearable_edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingright="35dip" /> <button android:id="@+id/clearable_button_clear" android:layout_width="30dip" android:layout_height="30dip" android:layout_alignparentright="true" android:background="@drawable/image_clear" android:layout_centervertical="true" android:layout_marginright="5dip"/> </relativelayout>
clearableedittext.java
public class clearableedittext extends relativelayout { layoutinflater inflater = null; edittext edit_text; imagebutton btn_clear; public clearableedittext(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); // todo auto-generated constructor stub initviews(); } public clearableedittext(context context, attributeset attrs) { super(context, attrs); // todo auto-generated constructor stub initviews(); } public clearableedittext(context context) { super(context); // todo auto-generated constructor stub initviews(); } void initviews() { inflater = (layoutinflater) getcontext().getsystemservice(context.layout_inflater_service); inflater.inflate(r.layout.clearable_edit_text, this, true); edit_text = (edittext) findviewbyid(r.id.clearable_edit); btn_clear = (imagebutton) 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) { // todo auto-generated method stub edit_text.settext(""); } }); } void showhideclearbutton() { edit_text.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { // todo auto-generated method stub 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) { // todo auto-generated method stub } @override public void aftertextchanged(editable s) { // todo auto-generated method stub } }); } public editable gettext() { editable text = edit_text.gettext(); return text; } }
to use it, declaration:
<com.yourpackage.clearableedittext android:layout_width="fill_parent" android:layout_height="wrap_content" />
Comments
Post a Comment