java - Android: EditText in CustomView not behaving as expected when touched -
i have customview contains linearlayout holds edittext & custom view element. however, when run app , touch edittext not behave expected (doesn't appear receive focus & soft keyboard doesn't open). i've tried setting duplicateparentstate="true" on both edittext & linearlayout. attached ontoucheventlistener edittext calls simple toast & toast did show up.
here's code customview.
form_field.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_marginbottom="15dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/grey_rounded_borders"> <edittext android:id="@+id/edit_text" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:inputtype="text" android:padding="10dp" android:textcolor="#2d6169" android:textsize="18sp" android:background="@color/transparent" /> <relativelayout android:layout_marginstart="5dp" android:layout_marginend="10dp" android:layout_width="wrap_content" android:layout_height="match_parent"> <com.example.app.validationicon android:id="@+id/validation_icon" android:layout_width="wrap_content" android:layout_height="match_parent" tools:ignore="contentdescription" /> </relativelayout> </linearlayout>
formfield.java
package com.example.app; import android.content.context; import android.content.res.typedarray; import android.text.inputtype; import android.util.attributeset; import android.view.motionevent; import android.view.view; import android.widget.edittext; import android.widget.linearlayout; import java.util.arraylist; public class formfield extends linearlayout { private edittext edittext; private validationicon validationicon; private integer fieldinputtype; private string fieldinputhint; public formfield(context context) { super(context); } public formfield(context context, attributeset attributeset) { super(context, attributeset); typedarray attrs = context.obtainstyledattributes(attributeset, r.styleable.formfield, 0, 0); fieldinputtype = attrs.getint( r.styleable.formfield_fieldinputtype, inputtype.type_text_variation_normal ); fieldinputhint = attrs.getstring( r.styleable.formfield_fieldinputhint ); attrs.recycle(); inflate(getcontext(), r.layout.form_field, this); this.setfocusable(true); this.setfocusableintouchmode(true); edittext = (edittext)findviewbyid(r.id.edit_text); edittext.setinputtype(fieldinputtype); edittext.sethint(fieldinputhint); edittext.setfocusableintouchmode(true); validationicon = (validationicon)findviewbyid(r.id.validation_icon); validationicon.setvalid(true); arraylist<view> touchables = new arraylist<view>(); touchables.add(this); touchables.add(edittext); addtouchables(touchables); } public formfield(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } }
registration_layout.xml
<relativelayout android:id="@+id/container" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="20dp"> <relativelayout android:id="@+id/account_details" android:layout_marginbottom="15dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.example.app.formfield android:id="@+id/name_field" android:layout_width="match_parent" android:layout_height="wrap_content" custom:fieldinputhint="@string/name_field_hint" /> <com.example.app.formfield android:id="@+id/email_field" android:layout_width="match_parent" android:layout_height="wrap_content" custom:fieldinputhint="@string/email_field_hint" /> <com.example.app.formfield android:id="@+id/password_field" android:layout_width="match_parent" android:layout_height="wrap_content" custom:fieldinputhint="@string/password_field_hint" /> <com.example.app.formfield android:id="@+id/re_password_field" android:layout_width="match_parent" android:layout_height="wrap_content" custom:fieldinputhint="@string/re_password_field_hint" /> </relativelayout> </relativelayout>
min sdk version set @ 14, target set @ 20. appreciated!
edit:
i got logcat message today while testing long presses on formfields.
w/textview﹕ textview not support text selection. action mode cancelled.
i double checked code , edittext element ever cast edittext. if edittext being cast textview explain original issues i'm confused why or how cast textview.
it looks extending linearlayout never using in layout. you've posted, aren't using formfield @ , custom view see validationicon view. hooking touch events edittext , validationicon aren't occurring because aren't using formfield in layout.xml.
rather extending linearlayout, why not use predefined methods , attributes handle behaviour trying achieve. example, displaying keyboard, requesting focus, , displaying hint:
<edittext android:id="@+id/edit_text" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" **android:inputtype="text"** **android:hint="@styleable/formfield_fieldinputhint"** android:padding="10dp" android:textcolor="#2d6169" android:textsize="18sp" android:background="@color/transparent" />
the keyboard should display when edit text receives focus, if not programmatically requesting focus , handling ime manager call. example, activity/fragment:
//call onviewcreated grab focus , begin flow edittext.requestfocus(); //set focus listener handle keyboard display edittext.setonfocuschangelistener(new onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if (!hasfocus) { inputmethodmanager imm = (inputmethodmanager)getsystemservice( context.input_method_service); imm.hidesoftinputfromwindow(myedittext.getwindowtoken(), 0); } else { inputmethodmanager imm = (inputmethodmanager)getsystemservice( context.input_method_service); imm.showsoftinputfromwindow(myedittext.getwindowtoken(), 0); } });
Comments
Post a Comment