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>
Related
I'm using android TextView with android:maxLines="1" and trying to use all types of ellipsize, but no one of them does not satisfies my requirements. I want to ellipsize text without adding dots at the and and without cutting whole last word. I want to cut just chars of last word without loosing whole word
<androidx.appcompat.widget.AppCompatTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:orientation="vertical"
android:maxLines="1"
/>
You need next property:
android:singleLine="true"
android:ellipsize="none"
Here us your xml:
<androidx.appcompat.widget.AppCompatTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:singleLine="true"
android:ellipsize="none"
/>
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 "".
This is my Edittext's xml code
<EditText
android:id="#+id/meeting_who_was_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:gravity="top"
android:scrollbars="vertical"
android:layout_marginTop="5dp"
android:paddingRight="15dp"
android:inputType="textMultiLine"
android:maxLines="2"/>
This is java code
meetingWhoWas=(EditText)findViewById(R.id.meeting_who_was_txt);
meetingWhoWas.setMovementMethod(new ScrollingMovementMethod());
when i run my app,multiLine working perfect in Edittext,but maxlines not working.
What is a wrong in my code and how i can solve my problem?
You just need to make sure you have the attribute "inputType" set. It doesn't work without this line.
android:inputType="text"
See the Documentation
Attribute android:inputType="textMultiLine" allows multiple lines of text in the EditText field. If this flag is not set, the EditText field will be constrained to a single line.
Attribute android:maxLines="2" represensts the height of EditText. It makes EditText be at most two lines tall.
For single line EditText use:
<EditText
android:id="#+id/meeting_who_was_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"/>
For multiple line EditText use:
<EditText
android:id="#+id/meeting_who_was_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:maxLines="2"/>
Hope this will help~
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>
I don't know why is this happening.. In other activities EditText is working fine. This is how my fragment layout looks:
<EditText
android:gravity="center"
android:inputType="text"
android:background="#android:color/transparent"
android:layout_below="#+id/profile_image"
android:text="Dusan Dimitrijevic"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:textColor="#android:color/black"
android:id="#+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Is something affecting in layout these EditTexts so they can't be editable. If i need to show you Java code, let me know. I can set text to EditText by the way, but can't edit it..
Remove the background and the text. Use hint instead of text
<EditText
android:gravity="center"
android:inputType="text"
android:hint="Dusan Dimitrijevic"
android:layout_below="#+id/profile_image"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:textColor="#android:color/black"
android:id="#+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
I encountered this and resolved by identifying an overlapping layout/element declared below editText thus preventing interaction with the editText element. My editText element envelop (parent) layout had the same:
android:layout_below="#+id/xxxxxx"
specification as the layout below it.