2

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>
Charuක
  • 12,953
  • 5
  • 50
  • 88
beeb
  • 1,615
  • 1
  • 12
  • 24

1 Answers1

4

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>
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • I didn't use `android:editable` and **i don't want to edit** my `TextView`. As far as i know since `android:singleLine="true"` is deprecated, `android:maxLines="1"` and `android:inputType="text"` should be used instead. My code works fine i just wanted to know if i should ignore the warning or if there is another way. – beeb Jan 14 '17 at 22:57
  • neither i said that you used `android:editable` look at it again what i said was about **InputType** and its definition – Charuක Jan 14 '17 at 22:59
  • Yes i read it but how should i provide a single line without using `android:singleLine="true"` or `android:inputType="text"`? – beeb Jan 14 '17 at 23:01
  • why you want to use `android:inputType="text"` you can remove that line – Charuක Jan 14 '17 at 23:05
  • Because as i said, as far as i know the combination of `android:maxLines="1"` and `android:inputType="text"` should be use instead of `android:singleLine="true"`. If i just use `android:maxLines="1"` **without** `android:inputType="text"`, then the text of the `TextView` is just cutted. – beeb Jan 14 '17 at 23:10
  • 1
    @beeb The inputType="text" does nothing for non-editabe TextViews. Its only sent as a parameter to the keyboard. So you can safely remove it, it will not change the display. I also don't know why you think singleLine=true is deprecated, it isn't. Check the docs. – Gabe Sechan Jan 14 '17 at 23:14
  • The thing is **it does change the display**! If not i would delete `android:inputType="text"`. Check this link http://stackoverflow.com/a/30315287/6632568 for a discussion on SO why it's deprecated. And Android Studio tells mee too that it's deprecated. – beeb Jan 14 '17 at 23:20
  • says hi to @Gabe Sechan and beeb you can use HorizontalScrollView with maxLines=1 – Charuක Jan 14 '17 at 23:31
  • @beeb how was that? – Charuක Jan 14 '17 at 23:37
  • Nice that works too. I'm still wondering why `android:inputType="text"` has the same effect... I think it's much overhead to wrap the textfield like this instead of a single code line. Do you recommend this solution instead of ignoring the warning? – beeb Jan 14 '17 at 23:42
  • 1
    @beeb Not deprecated in AS for me, not deprecated in the documentation https://developer.android.com/reference/android/widget/TextView.html#attr_android:singleLine. The page you reference says deprecated since version 3, I can assure you it hasn't been- I've been using it for the last 6 years without seeing any deprecation warnings. – Gabe Sechan Jan 14 '17 at 23:52
  • @beeb i recommend you to use `singleLine=true` as my knowledge .. even there is an argument about API 3 will not be removed because it's still the only way to make some effects that `android:maxLines` can't make – Charuක Jan 15 '17 at 00:06
  • 1
    Thanks guys for your answers and time. @Charuka i accepted your answer. I tested very much and i will use your second solution with `HorizontalScrollView`. That's exactly what i need. I set the `minWidth="100"` and if there is a text which is longer, the textview will increase it's width. – beeb Jan 15 '17 at 00:37