Set the width of a Textview by the maximum number of characters

Asked

Viewed 161 times

0

I’m creating a layout_item for my Listview, but I want all elements to be aligned, but some Textview can have size variation (they take the value of a numeral that can vary from 0 to 1000) so I’m assigning fixed values to their width.

The problem is that I do not know if the value I am assigning is enough for every type of screen and I would like to know if there is 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 and nescessario specify the size are the ids quantidade e valor and the dimension I’m using is sp

  • Related https://answall.com/q/10620/2541

1 answer

0

Specifying the property "layout_width" as "wrap_content" in the two Textview will solve your problem because the attribute "wrap_content" serves precisely for this, occupy the space necessary for full display of a certain content.

  • The problem and if wrap_contain the elements of the list will be misaligned in relation to the others because the size will vary with the size of the written text

Browser other questions tagged

You are not signed in. Login or sign up in order to post.