EditText´s android:nextFocusDown attibute stops working when onClick is set - android

Does anybody knows why the android:nextFocusDown attibute stops working when we set the onClick on it?
On the example below, we have some EditText with this attribute defined:
<TableRow
android:weightSum="1"
android:gravity="center">
<EditText
android:id="#+id/txtChave1"
android:maxLength="4"
android:gravity="center"
android:textSize="16dp"
android:layout_height="wrap_content"
android:layout_weight=".23"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:selectAllOnFocus="true"
android:inputType="textCapCharacters"
android:nextFocusDown="#+id/txtChave2"></EditText>
<EditText
android:id="#+id/txtChave2"
android:maxLength="4"
android:gravity="center"
android:textSize="16dp"
android:layout_height="wrap_content"
android:layout_weight=".23"
android:layout_width="0dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:selectAllOnFocus="true"
android:inputType="textCapCharacters"
android:nextFocusDown="#+id/txtChave3"></EditText>
<EditText
android:id="#+id/txtChave3"
android:maxLength="4"
android:gravity="center"
android:textSize="16dp"
android:layout_height="wrap_content"
android:layout_weight=".23"
android:layout_width="0dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:selectAllOnFocus="true"
android:inputType="textCapCharacters"
android:nextFocusDown="#+id/txtChave4"></EditText>
<EditText
android:id="#+id/txtChave4"
android:maxLength="4"
android:gravity="center"
android:textSize="16dp"
android:layout_height="wrap_content"
android:layout_weight=".23"
android:layout_width="0dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:selectAllOnFocus="true"
android:inputType="textCapCharacters"></EditText>
</TableRow>
As defined above, when user clicks on the "next button" on virtual keyboard, the focus changes as expected... But, if we set the onClick attribute, the android:nextFocusDown doen´t work anymore.
<EditText
android:id="#+id/txtChave1"
android:maxLength="4"
android:gravity="center"
android:textSize="16dp"
android:layout_height="wrap_content"
android:layout_weight=".23"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:selectAllOnFocus="true"
android:inputType="textCapCharacters"
android:nextFocusDown="#+id/txtChave2"
android:onClick="onClickHandler">
</EditText>
Any advice?
Thanks a lot!

Do it programmatically because when you call android:nextFocusDown in XML may be the object that you want to call in the nextFocus will not create it till.
private void setUpView(){
editText1=(EditText)findViewById(R.id.editText1);
editText2=(EditText)findViewById(R.id.editText2);
editText3=(EditText)findViewById(R.id.editText3);
}
private void setUpFocus(){
editText1.setNextFocusDownId(R.id.editText2);
editText2.setNextFocusDownId(R.id.editText8);// you can give focus to any id
editText3.setNextFocusDownId(R.id.editText9);
}
Call setUpView() before setUpFocus() in onCreate().

Had the same problem recently and found out that it will work if you use setOnTouchListener instead of setOnClickListener. In the onTouch method use MotionEvent.ACTION_DOWN or MotionEvent.ACTION_UP, according to what you need. Also don't forget to return FALSE for events that also have to be processed by the system (if you want to be safe, always return false).

Related

android:maxLength not working on EditText

Siduation
I was trying to limit the amount of characters in my EditText using the flag android:maxLength.
Code
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/someComponent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/someComponent2"
android:background="#null"
android:fontFamily="sans-serif-medium"
android:inputType="textCapWords|textPersonName"
android:lineSpacingExtra="8sp"
android:maxLength="10"
android:maxLines="1"
android:scrollHorizontally="true"
android:textSize="16sp"
android:textStyle="normal"
/>
Problem
For some reason it doesn't seem to have any effect. I can enter as many Characters as i wish
Question
Can anyone explain why the EditText doesn't behave according to its flags? Are there any flags which interfere with android:maxLength?
EDIT
Problem was found. Had nothing to do with the EditText. Was related to some wrongly committed changes.
try this code
<android.support.v7.widget.AppCompatEditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/someComponent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/someComponent2"
android:background="#null"
android:fontFamily="sans-serif-medium"
android:inputType="textCapWords|textPersonName"
android:lineSpacingExtra="8sp"
android:maxLength="10"
android:maxLines="1"
android:scrollHorizontally="true"
android:textSize="16sp"
android:textStyle="normal"
android:maxEms="10"/>

How to make cursor stay in the center of edittext

I have a normal edittext like this :
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/et_word"
android:ems="12"
android:background="#android:color/transparent"/>
How can I make the cursor stay always in the center of edittext? No matter if user enters something or not. Thanks.
Try adding the following code
android:gravity="center"
This makes sure that your text will always be entered in the center of the EditText. By default its value is left.
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/et_word"
android:ems="12"
android:gravity="center"
android:background="#android:color/transparent"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="start"
android:maxLines="1"
android:gravity="center"
android:hint="Some hint text"
></EditText>
please Try adding the following code
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="start"
android:maxLines="1"
android:gravity="center"
android:id="#+id/et_word"
android:ems="12"
android:background="#android:color/transparent"/>
Here you can add to property in xml file to gravity center then you can get cursor center
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/et_word"
android:ems="12"
android:gravity="center"
android:background="#android:color/transparent"/>
That can be write from center also.

Android align drawable and text in EditText

i've an edittext :
<EditText android:id="#+id/chp"
android:layout_width="match_parent"
android:paddingLeft="10dp"
android:layout_height="340dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:gravity="top"
android:hint="#string/hintCommentaire"
android:textSize="25sp"
android:drawablePadding="10dp"
android:drawableLeft="#drawable/ic_comment"
android:layout_margin="5dp" />
The problem is, i've my hint at the top of the edittext, but the drawable stays in the center..
I tried with "drawableStart", but it's not working.
Someone to help me ?
EDIT : I want the text and drawable in the top(same line). (i want to keep the height of my edittext)
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/etMobile"
android:hint="Mobile"
android:fontFamily="sans-serif-normal"
android:singleLine="true"
android:textSize="15sp"
android:gravity="center|start"
android:textCursorDrawable="#null"
android:drawableStart="#drawable/ic_profile_bio"
android:drawablePadding="10dp"
app:met_floatingLabel="normal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
Set this Property android:gravity="center|start" in above EditText

android EditText does not expand to mutiple lines when set android:inputType

I used a EditText widget to contain contacts. The layout file is as below
<EditText
android:id="#+id/contact_name_editor"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/add_recipients"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dip"
android:layout_marginRight="3dip"
android:background="#drawable/search_bar_in_new_chat"
android:hint="Recipients"
android:imeOptions="actionDone"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:paddingTop="3dip"
android:paddingBottom="3dip"
android:scrollHorizontally="true"
android:maxLines="3"
android:textSize="15sp"
android:editable="true"
android:clickable="true"
android:visibility="visible" />
It works fine, the default line is 1 and when many contacts are filled in, it will expand to 3 lines and scroll.
Then I changed it to the following(just added a android:inputType="number|text" attribute):
<EditText
android:id="#+id/contact_name_editor"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/add_recipients"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dip"
android:layout_marginRight="3dip"
android:background="#drawable/search_bar_in_new_chat"
android:hint="Recipients"
android:imeOptions="actionDone"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:paddingTop="3dip"
android:paddingBottom="3dip"
android:scrollHorizontally="true"
android:maxLines="3"
android:inputType="number|text"
android:textSize="15sp"
android:editable="true"
android:clickable="true"
android:visibility="visible" />
Then it failed expanding, just keep single line.
Why setting the inputType blocks the EditText expanding?
Have you tried?
android:inputType="number|textMultiLine"
If above will not work then you can use:
android:numeric="integer"
This will work definitely but let me clear this is deprecated now. But for your problem this is the only solution available what I found.

Single line EditText in Android

I am trying to make the edittext single line. The edittext does comes up as single line but on pressing enter it the cursor comes to the second line and I need to stop this. The singleLine attribute is deprecated so I do not want to use that.
<EditText
android:id="#+id/txtSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:drawablePadding="5dp"
android:lines="1"
android:maxLines="1"
android:inputType="none"
android:scrollHorizontally="true"
android:paddingLeft="5dp"
android:textSize="12sp" />
Can someone let me know what am I goind wrong over here ?
Set android:inputType to something different than "none", use "text", "number" or whatever should be entered in the EditText. This way you can make the EditText single-lined. (Of course you shouldn't use "textMultiLine" then ;))
Use this
android:singleLine="true"
instead of,
android:lines="1"
android:maxLines="1"
Change,
android:inputType="none"
to
android:inputType="text" or android:inputType="number"
according to your requirement.
use this code :
<EditText
android:id="#+id/Et_loginEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/layout_bg"
android:hint="Email Address"
android:singleLine="true"
android:ellipsize="end"
/>
This alone is enough to make single line in EditText.
<EditText
android:id="#+id/fullname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Full Name"
android:inputType="text"/>
android:inputType="text" alone solves the single line in EditText.
You need to choose your desired inputType as per your requirement.
change the android:inputType="none"
<EditText
android:id="#+id/txtSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:drawablePadding="5dp"
android:maxLines="1"
android:inputType="none"
android:scrollHorizontally="true"
android:paddingLeft="5dp"
android:textSize="12sp" />
to
<EditText
android:id="#+id/txtSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:drawablePadding="5dp"
android:maxLines="1"
android:inputType="text"
android:scrollHorizontally="true"
android:paddingLeft="5dp"
android:textSize="12sp" />

Categories

Resources