Define width of a TextView by the maximum number of characters

0

I'm creating a layout_item for my ListView, but I want all elements to be aligned, but some TextView may have size variation (they assume the value of a numeral that can range from 0 to 1000) so I'm assigning fixed values for the width of them.

The problem is that I do not know if the value I'm assigning is enough for every type of screen and I'd like to know if there's an ideal shape or dimension to use in this case.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="5dp"
android:orientation="horizontal">


<ImageView
    android:id="@+id/imagem"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_centerVertical="true"
    android:src="@drawable/bebidas_s_alcool" />

<TextView
    android:id="@+id/amigo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toStartOf="@id/lista"
    android:layout_toLeftOf="@id/lista"
    android:layout_toEndOf="@id/imagem"
    android:layout_toRightOf="@id/imagem"
    android:paddingLeft="10dp"
    android:text="Nome do Item"
    android:textColor="@color/Texto" />

<LinearLayout
    android:id="@+id/lista"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@id/rs"
    android:gravity="center">

    <ImageButton
        android:id="@+id/botaomenos"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:onClick="menos"
        android:src="@drawable/menos" />

    <TextView
        android:id="@+id/quantidade"
        android:layout_width="50sp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="5dp"
        android:text="5.55"
        android:textColor="@color/Texto" />

    <ImageButton
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginRight="5dp"
        android:onClick="mais"
        android:src="@drawable/mais" />

</LinearLayout>

<TextView
    android:id="@+id/rs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@id/valor"
    android:padding="3dp"
    android:text="R$"
    android:textColor="@color/Texto" />

<TextView
    android:id="@+id/valor"
    android:layout_width="60sp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:gravity="end"
    android:text="5000,00"
    android:textColor="@color/Texto" />


  </RelativeLayout>

As can be seen in the layout above the TextView which is nescessario specify the size are the ids quantidade e valor and the dimension I am using is sp

    
asked by anonymous 30.07.2018 / 17:33

1 answer

0

Specifying the "layout_width" property as "wrap_content" in both TextView will solve your problem because the "wrap_content" attribute is just for this, occupying the space required for full display of a certain content.     

04.08.2018 / 12:49