I'm planning an android app and my first doubt came on what element should I use to represent a free text area.
I want to develop a quick note type of app and I want to know which kind of element is better to represent an area where the user can freely write that will automatically make newlines and with support for scroll.
So, can anyone help me?
Thx in advance,
CR
These 3 attributes are important: singleLine, inputType, lines. Also, you may need a scrollbar, the code below shows how to make one:
<EditText
android:inputType="textMultiLine"
android:lines="8"
android:minLines="6"
android:gravity="top|left"
android:maxLines="10"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scrollbars="vertical"
/>
A simple EditText should be sufficient with some tweaking. Something like this:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dip"
android:inputType="textLongMessage" />
use edit text with android:singleLine="false"
Related
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've set a TextView as the display. I want to display the operation being performed in one line, for example:
9856243187 + 2457896123 - 214583
The problem is, when the input reaches the edge of the TextView - it jumps to the next line.
So, is there anyway to avoid this behavior and keep the input in one line, even if it goes out of the screen?
You should use android:maxLines="1" in your TextView tag.
android:singleLine="true" is deprecated.
android:singleLine="true"
is the answer
Update :
android:singleLine attribute has been deprecated since API Level 3, you can use
android:maxLines="1"
tl;dr:
For new users seeing this question, use android:singleLine="true". Ignore deprecation warnings.
Explanation
According to this bug report, setting android:maxLines="1" may cause your application to crash when some specific values are set in android:ellipsize as well. Apparently it affects most Android devices running 4.1 Jellybean through 6.0.1 Marshmallow (being fixed in 7.0 Nougat). Depending on your device, your mileage may vary. However, if you as a developer are targeting a wide range of devices, it is best to use android:singleLine="true" in order to prevent crashes.
add the code in your Textview
android:singleLine="true"
Thanks guys single line did the tricks but i also had to remove the ellipsis:
android:ellipsize="none"
This code worked for me:
android:singleLine="true"
If you want to manually scroll your single line content then you should use TextView inside HorizontalScrollView
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Single line text view that scrolls automatically if the text is too long to fit in the widget" />
</HorizontalScrollView>
Set the TextView to singeline.
Take a look at this
Can you please try this one ?
<TextView
android:text="Single line text view that scrolls automatically if the text is too long to fit in the widget"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Hope this will help you.
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)
I would like to know how to remove the top space/margin of a soft-keyboard.
For clearer picture, checkout the following screenshots:
With space and without space:
The "spacing" you're seeing is the autocomplete suggestion area. If you have an input type set to something as simple as text then you're going to get suggestions from the keyboard. Adding textNoSuggestions to your inputType field will remove the suggestions area.
So for example:
<EditText android:id="#+id/username_field"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions"
android:hint="#string/username" />
Or if you were already using something like textCapWords you can combine them like so:
<EditText android:id="#+id/username_field"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords|textNoSuggestions"
android:hint="#string/username" />
Also, not sure if you are using it but just a heads up, you'll want to use inputType="password" for your password field.
Whatever your inputType is, add |textNoSuggestions to the end of it.
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"
/>