Single line EditText in Android - 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" />

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

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"

AutoCompleteTextView with keyboard done button and maxLines

I am trying to implement AutoCompleteTextView with multiline text and with keyboard done button. But done button does not show on keyboard (shows enter button). Same thing i have tried with android:singleLine="true" and text comes in single line with done button.
<AutoCompleteTextView
android:id="#+id/txtVillageName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edt_border"
android:cursorVisible="true"
android:maxLines="4"
android:hint="Enter your location"
android:imeOptions="actionDone"
android:textColor="#color/text_color"
android:textColorHint="#color/lblColor"
android:textSize="#dimen/text_size_large" />
Add android:imeActionLabel="Done" into your xml code
<AutoCompleteTextView
android:id="#+id/txtVillageName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:hint="Enter your location"
android:imeOptions="actionDone"
android:imeActionLabel="Done"
android:singleLine="true" />
android:inputType="text" works.
<AutoCompleteTextView
android:id="#+id/txtVillageName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:hint="#string/location_hint"
android:imeOptions="actionDone"
android:inputType="text"
android:textColor="#color/text_color"
android:textColorHint="#color/lblColor"
android:textSize="#dimen/text_size_large" />
Set HorizontalScrolling and MaxLines
mAutocompleteTextView.setHorizontallyScrolling(false);
mAutocompleteTextView.setMaxLines(Integer.MAX_VALUE);
It works

android:nextFocusDown doesn't work

I designed a layout with four EditText and i want to facilitate the navigation of them. I understand the use of android:nextFocusDown but i'm not able to get it to work. I also setted the ´ImeOptions´ to actionNext, but on the softpad I neither see the right icon.
My XML snippet:
<EditText
android:id="#+id/reservation_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#drawable/edit_text_reservation"
android:ems="10"
android:hint="#string/yourName"
android:imeOptions="actionNext"
android:nextFocusDown="#+id/reservation_surname" >
</EditText>
<EditText
android:id="#+id/reservation_surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#drawable/edit_text_reservation"
android:ems="10"
android:hint="#string/yourSurname"
android:imeOptions="actionNext"
android:nextFocusDown="#+id/reservation_email"
android:nextFocusUp="#+id/reservation_surname" />
<EditText
android:id="#+id/reservation_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#drawable/edit_text_reservation"
android:ems="10"
android:hint="#string/yourEmail"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:nextFocusDown="#+id/reservation_notes"
android:nextFocusUp="#+id/reservation_email" />
<EditText
android:id="#+id/reservation_notes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_text_reservation"
android:ems="10"
android:imeOptions="actionDone"
android:inputType="textMultiLine"
android:nextFocusUp="#id/reservation_notes" />
I had a similar issue, where nextFocus* simply did not work. In your case..
going from
<EditText android:id="#+id/reservation_name"...
android:nextFocusDown="#+id/reservation_notes"
to
change surname's id to NOT have a "+" i.e.
<EditText android:id="#id/reservation_surname"
this way you are
no creating a new reference to that tag.
This will now work.

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