I have this stupid and seemingly trivial problem with the properties of an EditText.
The properties I am trying to achieve for the EditText, are the following:
The contents of the view should NOT contain newlines. It can be text, numbers, symbols, but no newlines.
The soft keyboard should NOT display the enter button because of the above. It should instead display something like "Send" or "Done".
The contents of the view should NOT continue horizontally when reaching the edge of the screen. Instead I want to wrap the text, displaying it on multiple lines.
I have tried many different combinations, but I can not achieve this combination.
What I currently have is this, which is inside a RelativeLayout:
<EditText
android:id="#+id/comment_box"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_below="#id/preparation_text"
android:hint="#string/comment_hint"
android:inputType="textCapSentences|textAutoCorrect|text"
android:scrollHorizontally="false"
android:maxLength="400"
android:imeOptions="actionSend"/>
It achieves 2 of 3. No newlines possible, keyboard displays "Send" rather than the enter-key for me, but the text continues on one line.
Changing inputType="text" to "textMultiLine" wraps text correctly on multiple lines, but also overrides the keyboard to always display the enter button.
I have tried different solutions around the Internet, including setting the properties maxLines="4", singleLine="true" and possible others that I have forgotten again.
I can not find a combination that works.
Output:
Exp:
The op is on right track. I did some research found that some options gets ignored which are specified in XML. but if the same options are set in code then it should do the trick. I used the same XML fragment specified in the question.
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:hint="hint"
android:inputType="textCapSentences|textAutoCorrect|text"
android:scrollHorizontally="false"
android:maxLength="400"
android:imeOptions="actionSend"
/>
And by adding the following lines in the code, it helped in achieving what you want.
edit_text.setMaxLines(Integer.MAX_VALUE);
edit_text.setHorizontallyScrolling(false);
textShortMessage is the inputType that you are looking for:
<EditText
android:id="#+id/commentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:gravity="left"
android:text=""
android:hint="hint"
android:textSize="16dp"
android:textColor="#android:color/black"
android:lineSpacingExtra="0dp"
android:lineSpacingMultiplier="1"
android:background="#android:color/white"
android:selectAllOnFocus="true"
android:inputType="textShortMessage|textMultiLine|textCapSentences"
android:singleLine="false" />
Refere to this to prevent paste function. To prevent pasting a new line.
Please use android:imeOptions="actionSend"
to solve your problem.
I have the same issue a long time ago, you have to programmatically change Edittext property on OnCreate().
So in XML, create your Edittext like this
<EditText
android:id="#+id/comment_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="80dp"
android:hint="Comment here"
android:maxLength="400"
android:inputType="textMultiLine" />
And on onCreate() (I wrote in kotlin not Java)
comment_box.imeOptions = EditorInfo.IME_ACTION_SEND
comment_box.setRawInputType(InputType.TYPE_CLASS_TEXT)
Related
I have a FrameLayout which contains an EditText and a Button.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/edittext_hint"
android:inputType="textPassword"
android:maxLength="25" />
<Button
android:id="#+id/forgottenPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:background="#null"
android:fontFamily="#font/sf_pro_display_lightitalic"
android:padding="16dp"
android:marginRight="16dp" // still, it gets cropped
android:text="#string/password_button"
android:textSize="18sp" />
</FrameLayout>
I gave padding=16dp to the Button but somehow the question mark is partially visible. If I make the phrase short like "Forgot it?" then the '?' mark fully visible. I didn't understand. I gave it even marginRight=16dp, still...
But when I don't set fontFamily attribute, again it is fully visible.
The confusion I have is that how come the last character gets cropped even though it has padding and marginRight. I even tried adding space after the question mark but nothing changed. Am I missing something?
Edit: I am actually using strings.xml for the button's text. When I add space after the '?' in the strings.xml it doesn't effect the text in the button, however, when text is hard coded and added space after the '?', question mark is fully visible again. But as it is said over and over Hardcoded string is bad.
I can't make my phrase short like "Forgot it?", because the app is in Turkish and the original phrase is relatively long. And also I have to use that font.
That is because your font is italic. If you want to insert a space character in XML file use after ? like this : ? .
That's because of the padding on your button. Remove the padding and try it out.
If all you want is some clickable text, there's no need for a Button:
<TextView
android:id="#+id/forgottenPassword"
android:clickable="true"
android:focusable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:fontFamily="#font/sf_pro_display_lightitalic"
android:padding="16dp"
android:marginRight="16dp"
android:text="#string/password_button"
android:textSize="18sp" />
Notice that I added clickable and focusable.
I have an EditText set to gravity Right, so that the text starts from the right if the language is Arabic.
Note: My application supports RTL, and I am not setting the TextDirection for my EditText as that will have the same problem. Gravity set to Right does the job perfectly. I have an issue only if I set the InputType to Number or Phone.
If the InputType is set to number/phone there are double cursor at the beginning and end of the text and it is a bit confusing.
To demonstrate this, I have two EditText with InputType Text and Number, Gravity set to Right for both. My application supports RTL and My phone is now set to Arabic language
Manifest
android:supportsRtl="true"
XML
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="text"
android:inputType="text"
android:lines="1"
android:maxLines="1"
android:gravity="right"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/phone"
android:inputType="number"
android:lines="1"
android:maxLines="1"
android:gravity="right"
/>
Here is a screen shot of the behaviour for the second EditText with InputType Number.
Any pointers on how to get rid of the double cursor? or any alternative.
Thanks
R
Use properties layout_direction as 'ltr' and gravity 'right'.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/phone"
android:inputType="number"
android:lines="1"
android:maxLines="1"
android:gravity="right" />
This solved my problem.
The Edit text doesn't know if the next char you will press is a number or a letter so in case of a letter it places the far left one while the far right one for a number(Most likely you know that just making sure).
So it will disappear if you switch to English.
I think this is fundamental in the edit text:
Solutions:
1- You can create your own edit text from scratch(From View).(May take month with all the problems that may arise).
2- Find a way to override the edit text to remove the split.
3- Find a git where someone had solved that problem(which I doubt).
This doesn't seem like a problem as a programmer or a user. A designer maybe will be a bit annoyed but ignoring it is the fastest way.
Sorry if this wasn't usefull.
ViewCompat.setLayoutDirection(findViewById(R.id.myedittext), ViewCompat.LAYOUT_DIRECTION_LTR);
it worked for me.
wrap your edittext in linearlayout with horizontal oriation and layoutDirection ltr
try to change android:inputType="number" into phone..
for example :
<com.test.utils.custom.CustomTelephone
android:id="#+id/custom_telephone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:inputType="phone"
android:text="#string/telephone_number_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/iv_logo" />
that worked for me ..
if not work try to add layoutDirection:Rtl in edit Text as attribute ..
Happy coding .. !
I have 2 multi line EditText with 2 lines of text entered as shown in images below.
Edittext 1:
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="50dp"
android:paddingStart="10dp"
android:background="#ffffff"
android:includeFontPadding="false"
android:paddingEnd="10dp"
android:maxLines="1"
android:inputType="textMultiLine|textNoSuggestions"/>
EditText 2:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:paddingStart="10dp"
android:background="#ffffff"
android:includeFontPadding="true"
android:paddingEnd="10dp"
android:maxLines="1"
android:inputType="textMultiLine|textNoSuggestions"/>
I have two questions..
The first EditText has fixed height 30dp and contains a fixed space above line "ASDFGHJKL...". How can i get the value of this space?
Secondly, how can i get the value of fontpadding, when android:includeFontPadding is set to true.?
#DevMarc
Question 1:
Idea is Android has options to have multi line text views, it seems you have forced it to be single line.
android:maxLines="1"
Please try using something like this:
android:lines="2"
Ref: Android Developer Site for Multiple Lines
If you interested in adding ellipses use this:
android:ellipsize="1"
Ref: Please find the link in comments as with my profile can't post more than two links.
Question 2
Try this :
textview.getPaint().getFontSpacing()
Ref: Get Paint method for textview
Hope it helps.
I want create simple calculator for android, and having problem with displaying long equations. Everything is OK (one line, horizontal scrolling), but I don't see end of line (always see start of line). Would it be possible to scroll it automatically to the latest character?
Example:
Hello there
...lo there
--------
-
<EditText
android:id="#+id/etDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:clickable="false"
android:cursorVisible="false"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="right"
android:textSize="15sp"
android:scrollHorizontally="true"
android:singleLine="true"
android:ellipsize="end"
android:hint="0." />
Image: SimpleCalc image
Try this one:
EditText editText = (EditText) findViewById(R.id.editText1);
editText.setSelection(editText.getText().length());
The "official" way of doing that is (as suggested in the comments), via the ellipsize attribute set to end. If it's not working, it might be due this attribute is a bit tricky, meaning that it has some circumstances to be happening in order to work (like for example, singleLine=true).
You might want to see this, it helped me when I had this issue.
Whenever a word is typed in the EditText box, I always see an underline under the word being typed. But when I press a space after that word I no longer see the underline.
My reqirement is to remove that underline when the user is typing the message.
Added is the screenshot and we see that Smith is underlined. But I don't want this to happen.
Below is the xml that I use for the AlertDialog box.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/name_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="#string/alert_dialog_name"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/username_edit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:inputType="textPersonName"
android:capitalize="none"
android:gravity="fill_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
android:inputType="textNoSuggestions"
There is a function that removes any composing state. I think if you call it after every time a user types, you will not see the underline. I use it after the user finishes typing, to get the drawing cache of the entire textView (which I need without underline). It's
someTextView.clearComposingText();
You don't have to use any logic to remove the underlines -- just call getText().toString() when you want to use the value. It won't include special formatting or anything.
accepted answer is not given solution, and some other user given answer but no one given full anser, so that's why i write here working solution.
If you want to remove underline then just add below code.
XML
android:inputType="textNoSuggestions"
Java
EditText ed;
ed = findViewById(yourId);
ed.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
If you want to set more than one input type then,
ed.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
May be above information is helpful to others.
The best solution for this is to add on your EditText:
android:inputType="textMultiLine|textVisiblePassword"
Use this from your class, if EditText view is dynamic (created from class file):
EditText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
OR include android:inputType="textNoSuggestions" with your EditText in XML.
Read this if you want to keep emojis and textNoSuggestions doesn't work for you.
textNoSuggestions does not work in every keyboard.
inputType="textVisiblePassword" works but it removes emoji's from most keyboards.
I found that setting inputType="textUri" works and keeps the ability to add emojis.
try:
android:inputType= InputTypes.TextVariationVisiblePassword;
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:imeOptions="actionDone"
android:inputType="textNoSuggestions"
android:maxLines="1"
/>