How to maintain text padding on EditText when there are multiple lines - android

For an application that I'm working on, I have an editText that I allow users to input data into. I have defined padding on the input that is not maintained when multiple lines of text are entered in. For example:
After adding a second line of text:
As more and more lines are added, I'm also seeing that the position of the edittext in the parent view gets lower and lower:
My XML for the editText is as follows:
<EditText
android:id="#+id/message_edit"
android:layout_width="0dp"
android:layout_weight="0.75"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/dp_8"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/someBackground"
android:paddingLeft="#dimen/dp_12"
android:paddingRight="#dimen/dp_3"
android:elevation="3dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="Type here"
android:lineSpacingExtra="#dimen/dp_4"
android:textColor="#color/black"
android:textSize="#dimen/sp_17" />
I assume that both issues are related given but i'm not sure what the underlying cause is.

Instead android:paddingLeft and android:paddingRight use only android:padding to add padding on all side. Check below:
<EditText
android:id="#+id/message_edit"
android:layout_width="0dp"
android:layout_weight="0.75"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/dp_8"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/someBackground"
android:padding="#dimen/dp_12"
android:elevation="3dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="Type here"
android:lineSpacingExtra="#dimen/dp_4"
android:textColor="#color/black"
android:textSize="#dimen/sp_17" />
Result:

Related

How to make an EditText automatically scroll after an X amount of lines

I have found plenty of answers on how to make scrollable EditText, but none mentions how to make the text scroll after an X amount of lines. In my app i need the text scrolling on its own after 5 lines, otherwise text gets mixed up with icons bellow. So far i have been restricting maxLength to avoid this, but i need to be able to write more characters without worrying that they get mixed up with icons.
<EditText
android:id="#+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textColorHint="#color/white"
android:textColor="#color/colorPrimaryDark"
android:textIsSelectable="true"
android:gravity="top"
android:fontFamily="sans-serif"
android:textSize="25dp"
android:textStyle="normal"
android:ems="30"
android:maxLength="180"
android:hint="Input here..."
android:textAlignment="center"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="textMultiLine"
android:overScrollMode="always"
android:scrollbarStyle="insideInset"/>
Turns out what i was searching for was:
android:lines="5"

Android: TextInputLayout always leaves some space from top

I have this layout:
<LinearLayout
android:id="#+id/dialogCentralContent"
android:gravity="center_horizontal"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_below="#+id/phoneNumberInfo"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:background="#drawable/edittext_white_rounded">
<EditText
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:maxLines="1"
android:inputType="none"
android:focusable="false"
android:text="#={dataContext.phonePrefix}"
style="#style/Widget.App.PurchaseAmountEditText" />
<android.support.design.widget.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
tools:errorEnabled="true"
app:errorText="#{dataContext.validator.phoneValidation}">
<android.support.design.widget.TextInputEditText
android:layout_height="60dp"
android:layout_width="match_parent"
android:gravity="center"
android:maxLines="1"
android:inputType="phone"
android:text="#={dataContext.phoneNumber}"
style="#style/Widget.App.PurchaseAmountEditText" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
It looks like this:
As you can see the second EditText is rendered lower than the first one. When I look at it in the visual editor of Android Studio I can see that the TextInputLayout has some free space at its top, however setting paddingTop="0dp" didn't change anything. So how to make those two EditTexts render the same way?
By default, android.support.design.widget.TextInputLayout will leave room at the top for the floating hint text. If you're not using the floating hint, you can remove that space by adding the following to the opening <TextInputLayout> element.
app:hintEnabled="false"
The regular hint text will still be visible on the TextInputEditText when it's empty.
N.B. – This answer does not apply to the current Material Components version, which is markedly different than the original.

Set AutocompleteTextView hint in a single line with Ellipsize end

I want to set the hint of my AutocompleteTextView in a single line with extra string to be truncated with dots(...) in the end.
I am using singleLine="true", maxLines="1", ellipsize="end".
But still the hint is shown in two lines. Although The text is set to single line. But I need the hint should also need to shown in single line.
The Preview in Android Studio shows the hint in single line. But when I run it on Device it shows in 2 lines.(S5, Nexus-5, Nexus-6, J7).
I also want the imeOption should be always search. i.e. imeOptions="actionSearch" Hence I can't change it to actionNext.
Below is my XML Code:
<RelativeLayout
android:id="#+id/liLaySearchMap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:paddingLeft="10dp"
android:background="#color/cococo"
android:paddingRight="10dp">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/five_dp"
android:background="#color/white"
android:padding="#dimen/two_dp"
android:scrollHorizontally="true"
android:drawablePadding="#dimen/two_dp"
android:singleLine="true"
android:lines="1"
android:maxLines="1"
android:ellipsize="end"
android:textSize="#dimen/twelve_sp"
android:drawableLeft="#drawable/ic_action_search"
android:inputType="text"
android:hint="A Very Long Hint Text Which Need To Be Tuncated At the End In Single Line"
android:id="#+id/edSearch"
android:imeOptions="actionSearch"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/tvCancel"
/>
<TextView
android:id="#+id/tvCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:layout_margin="#dimen/five_dp"
android:text="#string/cancel"
android:textSize="#dimen/twelve_sp"
android:textColor="#color/tw__composer_red"
android:padding="#dimen/five_dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
Can Anyone Please en-curtain the issue I am facing. As I think I had used every possible solution but still the hint is not in single line.
these are screenshots on my Device and xml i tried is as follows
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/white"
android:padding="2dp"
android:scrollHorizontally="true"
android:drawablePadding="2dp"
android:singleLine="true"
android:lines="1"
android:maxLines="1"
android:ellipsize="end"
android:textSize="16sp"
android:inputType="text"
android:drawableLeft="#mipmap/ic_launcher"
android:hint="A Very Long Hint Text Which Need To Be Tuncated At the End In Single Line"
android:id="#+id/edSearch"
android:imeOptions="actionSearch"/>
try Setting android:inputType="text" on the AutoCompleteTextView it worked for me
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:inputType="text"
android:ellipsize="end"
android:imeOptions="actionNext"
android:hint="this is to check if it's getting truncated and in single line or not"/>
Setting android:inputType="text" on the AutoCompleteTextView it worked for me, unlike android:ellipsize="end", the text did not stay with '...' on end, but it does meet expectations.
android:completionThreshold="1"

EditText won't write into single line

I'm making a calculator and user input is contained in EditText. Input is typed through buttons on screen, so Android keyboard is disabled. However, I want it to write all input in one line and when line reaches border, it needs to be ellipsized and then horizontally scrollable so you can reach whole text.
I tried doing this by setting android:maxLines="1" and android:ellipsize="end", but that doesn't work. Text is still in multiple lines, but only one line is visible and you need to scroll vertically to see other lines. I also tried using android:singleLine="true", but that makes EditText completely unusable.
This is EditText XML:
<EditText
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/colorBlue"
android:textAlignment="textEnd"
android:textSize="40sp" />
I think this will work for you. Let me know if it works.
<EditText
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/colorBlue"
android:textAlignment="textEnd"
android:textSize="40sp"
android:inputType="text"
android:maxLines="1"
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end"/>
This post might help you as well.
For single line in EditText, you just need to set two properties:
android:inputType="text"
android:maxLines="1"
Adding this line in the edittext make scrollable singleline text.
android:inputType="textShortMessage"
**This example helps you to create multiline EditText in Android.**
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity"
>
<!--
android:scrollbars="vertical"
It will display a vertical scrollbar if the EditText text exceed
3 lines (android:maxLines).
-->
<EditText
android:id="#+id/et"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginBottom="10dp"
android:hint="Multiline EditText by XML layout"
android:fontFamily="sans-serif-condensed"
android:background="#d3d7b6"
android:minLines="2"
android:maxLines="3"
android:scrollbars="vertical"
android:inputType="textMultiLine"
/>
<EditText
android:id="#+id/et2"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:fontFamily="sans-serif-condensed"
android:hint="Multiline EditText programmatically"
android:background="#d4d7a5"
android:layout_below="#+id/et"
/>
</RelativeLayout>

Edittext jumps to next edittext, but wrong

I need help with my app. This is my xml file with Relative layout, and I need to jump to the next edittext after pressing Enter key.
But there is a problem, if I'm writing to the editTextKm and press Enter key, it jumps to the editTextFullTank, but I need to jump to the editTextManufactYear. How to do it?
<EditText
android:id="#+id/editTextSPZ"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLength="11"
android:inputType="textCapCharacters"/>
<EditText
android:id="#+id/editTextKm"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editTextSPZ"
android:inputType="number"
android:maxLength="7"/>
<EditText
android:id="#+id/editTextManufactYear"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editTextKm"
android:layout_alignBottom="#+id/editTextKm"
android:layout_alignParentRight="true"
android:layout_marginRight="2dp"
android:layout_toRightOf="#+id/editTextKm"
android:inputType="number"
android:maxLength="4" />
<EditText
android:id="#+id/editTextFullTank"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editTextKm"
android:inputType="number"
android:maxLength="3"/>
<EditText
android:id="#+id/editTextReserveTank"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/editTextManufactYear"
android:layout_toRightOf="#+id/editTextFullTank"
android:inputType="number"
android:maxLength="3" />
<EditText
android:id="#+id/editTextNote"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#+id/editTextFullTank"
android:capitalize="sentences"
android:singleLine="false"/>
For further details you can consult Anddroid Developer blog.
Focus movement is based on an algorithm which finds the nearest neighbor in a given direction. In rare cases, the default algorithm may not match the intended behavior of the developer. In these situations, you can provide explicit overrides by using these XML attributes in the layout file:
android:nextFocusLeft="reference"
android:nextFocusRight="reference"
android:nextFocusUp="reference"
android:nextFocusDown="reference"
add the attributes:
android:nextFocusDown="#id/your_next_edit_text_to_focus" to each EditText according to what you need.
You can also use :
android:nextFocusUp="#id/your_next_edit_text_to_focus"
android:nextFocusLeft="#id/your_next_edit_text_to_focus"
android:nextFocusRight="#id/your_next_edit_text_to_focus"

Categories

Resources