android:maxLength not working on EditText - android

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"/>

Related

Android Why EditText ellipsize doesn't work?

I have added an EditText to my layout, but the ellipsize doesn't work. Can anybody help me? Thanks.
<EditText
android:layout_width="match_parent"
android:layout_margin="10dp"
android:singleLine="true"
android:ellipsize="end"
android:textSize="24dp"
android:layout_height="wrap_content" />
Ellipsize="end" wont work for editable EditText so you need to make the EditText as non editable to make it work. Use the following code
<EditText
android:layout_width="match_parent"
android:layout_margin="10dp"
android:textSize="24dp"
android:text="A long texttttttttttttttttttttttttttttttttttttttttttttt"
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
android:singleLine="true"
android:layout_height="wrap_content"
tools:ignore="Deprecated" />
ellipsize maybe works only with
android:ellipsize="end"
android:singleLine="true"
and sometimes we add one more
android:maxLines="n"

EditText and AutoCompleteTextView text starts not on same position

I've a EditText and a AutoCompleteTextView and the text starts not on the same position which looks a bit strange.
I'm using appcompat v7 and I don't know if it's possible to fix this.
The line in the bottom is also different (so so bad as the text begin)
I also tried to style it like appcompat's EditText, but that doesn't look like that.
Code
<EditText
android:id="#+id/input_ipAdresse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignEnd="#+id/button_berechnen"
android:layout_alignRight="#+id/button_berechnen"
android:ellipsize="end"
android:gravity="start"
android:hint="#string/input_ipAdresse"
android:imeOptions="flagNoExtractUi"
android:inputType="phone"
android:paddingStart="#dimen/padding_small"
android:paddingEnd="#dimen/padding_small"
android:textSize="#dimen/fontsize_medium" >
<requestFocus android:layout_width="match_parent" />
</EditText>
<AutoCompleteTextView
android:id="#+id/input_snm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/input_ipAdresse"
android:ellipsize="end"
android:gravity="start"
android:imeActionLabel="#string/Button_berechnen"
android:imeOptions="actionDone|flagNoExtractUi"
android:inputType="phone"
android:paddingStart="#dimen/padding_small"
android:paddingEnd="#dimen/padding_small"
android:textSize="#dimen/fontsize_medium" />

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" />

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

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).

Categories

Resources