How to make TextInputLayout with two edit fields and only one hint? - android

Additional constraints: both edit fields are placed to each other horizontally and the hint is long enough to be above them
Long enough hint above two edit fields
======= ==================
enter image description here

It's impossible for TextInputLayout to have 2 EditText as children. Doing this throws you an InflateException like this :
android.view.InflateException: Binary XML file line #xxx: We already
have an EditText, can only have one
So I assume that it's a Material Design guideline, and having one TextInputLayout with 2 EditText is anti-pattern to google's recommandations.

Try this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:gravity="center"
android:text="Only one hint above two edit fields"
android:textSize="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/et1"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/et2"
/>
</LinearLayout>
</LinearLayout>
If you ant to hide the TextView, you can use a TextChangedListener and set the Visibilty to INVISIBLE if the text equals "".

Related

TextView singleLine

I want to create a textview with a single line which is inside a swipelayout. Every textview represents an action on which the user can touch. All textviews have a width of 100dp. Now i have the problem that the text of a textview is too long and it wraps to the next line. I don't want to use android:singleLine="true". Now i added android:maxLines="1" and android:inputType="text" and it works fine. But now i get the warning:
Attribute android:inputtype should not be used with <TextView>: Change
element type to <EditText>
I don't want to enter text so i don't need an EditText.
Is there another way or should i just ignore this warning?
<TextView
android:id="#+id/item_list_bottom_rename"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:inputType="text"
android:layout_gravity="center"
android:drawableTop="#drawable/ic_visibility_white_36dp"
android:gravity="center"
android:padding="10dp"
android:text="#string/rename"
android:textColor="#color/item_title_listname"/>
Solution:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fillViewport="true"
android:minWidth="100dp"
android:scrollbars="none">
<TextView
android:id="#+id/item_list_bottom_rename"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableTop="#drawable/ic_visibility_white_36dp"
android:gravity="center"
android:maxLines="1"
android:text="#string/rename"
android:textColor="#color/item_title_listname"/>
</HorizontalScrollView>
Have you read what it says ?
Using a <TextView> to input text is generally an error, you should be
using <EditText> instead. EditText is a subclass of TextView, and some
of the editing support is provided by TextView, so it's possible to
set some input-related properties on a TextView. However, using a
TextView along with input attributes is usually a cut & paste error.
To input text you should be using <EditText>.
InputType -Bit definitions for an integer defining the basic content type of text held in an Editable object. Supported classes may be combined with variations and flags to indicate desired behaviors.
Attribute android:editable should not be used with <TextView> and thats the reason!
you can remove that inputType in your textView
without android:singleLine="true" but with android:maxLines="1"
Alternatives
1.use what compiler asks :P EditText and make it act as textView
<EditText
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="text"
android:maxLines="1"
android:text="Helloo"
android:id="#+id/Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent" >
</EditText>
2.you can also use
<HorizontalScrollView
android:fillViewport="true"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/item_list_bottom_rename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:maxLines="1"
android:text="Heloo" />
</HorizontalScrollView>

How to make edittext scrollable, multiline and turn off auto suggest?

I am implementing a programming language code editor for the same I need EditText horizontally scrollable also multiline and I want to turn off the autocorrect and the autosuggest. Suppose, I want to write
int main(){
printf("This is an awesome platform! I can get any problem solved here!");
}
then EditText should show in the exact same way. Like shown in the below image
screenshot
Dots at the end of the line denotes that it should be scrollable horizontally.
I tried this
<EditText
android:layout_below="#id/label_code"
android:id="#+id/et_code"
android:layout_width="match_parent"
android:layout_height="400dp"
android:inputType="textNoSuggestions|textMultiline"
android:gravity="top"
android:scrollHorizontally="true"
android:background="#drawable/square_border_unselected"
android:padding="15dp" />
But in that case, horizontal scroll is not working. The text is going to next line if it's length is more than screen size.
Is there any solution to implement all three features in EditText?
Edit #1:
If I don't use textMultiline than I am able to achieve horizontally scrollable EditText
Update #1
This is not the perfect solution but through this I achieved what I wanted.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:minEms="1000"
android:layout_height="wrap_content"
android:minHeight="250dp"
android:inputType="textNoSuggestions|textMultiLine"
android:gravity="top"
android:padding="15dp" />
</HorizontalScrollView>
android:inputType="text" will do the job for you!
<EditText
android:text="This is an awesome platform! I can get any problem solved here!"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#876"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="2dp"
android:textColor="#FFFFFF"/>
EDIT: Here goes what you want! Horizontal scroll with multi line.
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#876"
android:paddingLeft="2dp"
android:text="This is an awesome platform! I can get any problem solved here!"
android:textColor="#FFFFFF" />
</HorizontalScrollView>
out put:
EDIT3 : This is nothing but because you are lazy.Adjust bounds like below!
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:background="#789"
android:inputType="textMultiLine"
android:layout_width="500dp"
android:layout_height="200dp"
android:textColor="#FFFFFF" />
</LinearLayout>
</HorizontalScrollView>
Cheers!
Try this:
android:scrollbars = "vertical"
in your EditText xml.
try these lines in your edittext
android:inputType="textMultiLine"
android:maxLines="10"
android:scrollbars="vertical"
Try putting your editText in a scroll view
The combination of XML attributes that removed both the Suggestions and the Suggestions Bar while retaining multi-line text was the following:
android:privateImeOptions="nm"
android:inputType="textNoSuggestions | textMultiLine"
or
android:privateImeOptions="nm"
android:inputType="textFilter | textMultiLine"

Android TextInputLayout/EditText is not full size and cuts off text

I have a fragment which contains a calculator (Just three TextInputEditTexts which listen for input).
These inputs are set out in a RelativeLayout as shown below-
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/binomial_probability_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_probability"
android:paddingBottom="16dp">
<android.support.design.widget.TextInputEditText
android:id="#+id/binomial_probability_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:inputType="numberDecimal"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/binomial_trials_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_trials"
android:paddingBottom="16dp"
android:layout_below="#+id/binomial_probability_wrapper">
<android.support.design.widget.TextInputEditText
android:id="#+id/binomial_trials_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:inputType="numberSigned"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/binomial_successes_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_successes"
android:paddingBottom="16dp"
android:layout_below="#+id/binomial_trials_wrapper">
<android.support.design.widget.TextInputEditText
android:id="#+id/binomial_successes_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:inputType="numberSigned"/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
I have also tried using a LinearLayout with each input on a separate row, but that had the same result.
As you can see in this image-
the bottom input is cut off. If I measure it on my screen it is clearly a different size, but Android returns the same value for the heights of each layout and input.
Any suggestions as to how to fix this are welcome.
Edit: Some extra information. The layout bounds show that the text cursor is actually taller than the height of the bottom TextInputEditText. The top have the correct height.
Second edit: I added the a TextView below (For output) and that is now cut off, while the TextInputEditText is now the same as the others.
As saying in official github page:
TextInputLayout provides two height variations for filled and outline
text fields, standard and dense. Both box styles default to the
standard height.
In order to reduce the height of a text box, you can use a dense
style, which will reduce the vertical padding within the text box. You
can achieve this by applying the appropriate styles to your
TextInputLayout and TextInputEditText, depending on whether you are
using a filled or outline text field
So try to apply #style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense to your TextInputLayout or change padding for TextInputEditText
<android.support.design.widget.TextInputLayout
android:id="#+id/binomial_probability_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_probability"
android:paddingBottom="16dp">
<android.support.design.widget.TextInputEditText
android:id="#+id/binomial_probability_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:inputType="numberDecimal"/>
</android.support.design.widget.TextInputLayout>
You have to use android:paddingTop="5dp" in
<android.support.design.widget.TextInputLayout
android:id="#+id/binomial_probability_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_probability"
android:paddingTop="5dp">
---- write something ----
</android.support.design.widget.TextInputLayout>

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 word wrap

I have this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin" >
<LinearLayout
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
What i want is when the user enters more text than which fits into a single line, the EditText wraps the line, increases it height automatically and shows the not fitting text in the next line. The EditText should not be scrollable horizontally.
I read lots of questions related to this, but none of them helped. Currently the EditText above does not wrap the lines, and can be scrolled horizontally. I tested on Android 4.2 and 4.4.
EDIT: added complete layout.
EDIT2:
Sorry, this is my fault. I change the input type programatically in my code:
mEditText.setInputType(mInputTypes.get(question.getType()));
So with this line, i have overriden the input type from xml. I modified the code, and the multiLine input type indeed works.
I have removed all unnecessary attributes and put only required attribute android:maxLines="20",
So it looks like this,
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:maxLines="20" />
add android:scrollHorizontally="false" and android:inputType="textMultiLine" this attribute in EditText from this post on SO
<EditText
android:id ="#+id/editText"
android:layout_width ="0dip"
android:layout_height ="wrap_content"
android:layout_weight ="1"
android:inputType="textCapSentences|textMultiLine"
android:maxLines ="4"
android:maxLength ="2000"
android:scrollHorizontally="false" />
The only solution that worked for me:
EditText someedittext = (EditText)findViewById(R.id.someedittext);
someedittext.setMaxWidth(someedittext.getWidth());

Categories

Resources