TuGordoBello
TuGordoBello

Reputation: 4509

How I can reduce the size of a textview when the text is too long?

I have the following situation

enter image description here

and this

enter image description here

my code:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <RelativeLayout
        android:id="@+id/container_description"
        android:layout_width="match_parent"
        android:layout_height="382dp"
        android:paddingBottom="15dp" >

        <RelativeLayout
            android:id="@+id/header_description"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_alignParentTop="true"       
            android:paddingLeft="5dip"
            android:paddingRight="5dip" >

            <LinearLayout
                android:id="@+id/llamar_description"
                android:layout_width="300dip"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                android:gravity="center_vertical|left"
                android:layout_marginTop="5dp"

                 >
                <ImageButton
                             android:id="@+id/carta"
                             android:layout_width="60dp"
                             android:layout_height="60dp"
                             android:src="@drawable/platos"
                             android:scaleType="centerInside"
                             android:onClick="onClick"
                             android:clickable="true"
                             android:background="@null"
                             android:layout_marginRight="6dp"
                             android:layout_marginLeft="25dp"
                             />
                <ImageButton
                             android:id="@+id/tragos"
                             android:layout_width="60dp"
                             android:layout_height="60dp"
                             android:src="@drawable/tragos"
                             android:scaleType="centerInside"
                             android:onClick="onClick"
                             android:clickable="true"
                             android:background="@null"
                             android:layout_marginRight="6dp"
                             android:layout_marginLeft="0dp"
                             />
                <ImageButton
                             android:id="@+id/happy"
                             android:layout_width="60dp"
                             android:layout_height="60dp"
                             android:src="@drawable/happy"
                             android:scaleType="centerInside"
                             android:onClick="onClick"
                             android:clickable="true"
                             android:background="@null"
                             android:layout_marginRight="6dp"
                             android:layout_marginLeft="0dp"
                             />

                 <!--<ImageButton
                             android:id="@+id/promo"
                             android:layout_width="50dp"
                             android:layout_height="50dp"
                             android:src="@drawable/promocion2"
                             android:scaleType="centerInside"
                             android:onClick="onClick"
                             android:clickable="true"
                             android:background="@null"
                             android:layout_marginRight="6dp"
                             />-->
                 <ImageButton
                             android:id="@+id/karaoke"
                             android:layout_width="60dp"
                             android:layout_height="60dp"
                             android:src="@drawable/karaoke"
                             android:scaleType="centerInside"
                             android:onClick="onClick"
                             android:clickable="true"
                             android:background="@null"
                             android:layout_marginRight="6dp"
                             android:layout_marginLeft="0dp"
                             />


            </LinearLayout>

           <TextView
                android:id="@+id/texto_usted_esta_en"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:ellipsize="end"
                android:paddingTop="20dip"
                android:singleLine="true"
                android:text="@+string/usted_esta_en"
                android:textColor="@android:color/white"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/address_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/llamar_description"
                android:layout_centerHorizontal="true"
                android:ellipsize="end"
                android:gravity="center"
                android:maxLines="2"
                android:paddingLeft="10dip"
                android:paddingRight="10dip"
                android:paddingTop="10dp"
                android:textColor="@android:color/white"
                android:textSize="18sp"
                 />

            <TextView
                android:id="@+id/name_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/address_description"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:paddingTop="25dp"
                android:gravity="center"
                android:lineSpacingExtra="-20dp"
                android:tag="bold"
                android:textColor="@android:color/white"
                android:textSize="60dp" />



        </RelativeLayout>

        <LinearLayout
            android:id="@+id/tragos_description"
            android:layout_width="300dip"
            android:layout_height="wrap_content"
            android:layout_below="@+id/header_description"
            android:layout_centerHorizontal="true"
            android:paddingLeft="25sp" >


            <TextView
                android:id="@+id/texto_carta"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="Platos"
                android:textColor="@android:color/white"
                android:textSize="10sp"
                android:layout_marginLeft="15dp"
                 />
             <TextView
                android:id="@+id/texto_tragos"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="Tragos"
                android:textColor="@android:color/white"
                android:textSize="10sp"
                android:layout_marginLeft="33dp"/>
              <TextView
                android:id="@+id/texto_happy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="Happy Hours"
                android:textColor="@android:color/white"
                android:textSize="10sp" 
                android:layout_marginLeft="22dp"/>
               <!--<TextView
                android:id="@+id/texto_promo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="Promos"
                android:textColor="@android:color/white"
                android:textSize="10sp"
                android:layout_marginLeft="10dp" />-->
               <TextView
                android:id="@+id/texto_karaoke"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="Karaoke"
                android:textColor="@android:color/white"
                android:textSize="10sp"
                android:layout_marginLeft="19dp" />


        </LinearLayout>

        <TextView
            android:id="@+id/sobre_happy_hour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tragos_description"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="60dp"
            android:gravity="center"
            android:text="Tutorial"
            android:textColor="@android:color/white"
            android:textSize="16sp"
            android:onClick="onClick"
            android:clickable="true"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:background="#fe005a"
             />
        <TextView
            android:id="@+id/horario_happy_hours"
            android:layout_width="250dip"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sobre_happy_hour"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dip"
            android:gravity="left"
            android:textColor="@android:color/white"
            android:textSize="12sp"
            android:paddingLeft="5sp"/>
    </RelativeLayout>
</ScrollView>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dip"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#ffffff"
    android:choiceMode="singleChoice"
    android:headerDividersEnabled="false" />

</android.support.v4.widget.DrawerLayout>

enter image description here

How could I do to shrink the size of textview to Xdp if the text contains more than 15 characters?

Upvotes: 1

Views: 2099

Answers (3)

Rishi Gautam
Rishi Gautam

Reputation: 1938

it is the logically part , give the condition if the number of size of string more then , the you reduce the size .

Upvotes: 1

hmartinezd
hmartinezd

Reputation: 1186

You can set the text size at runtime using the TextView method:

setTextSize (float size) 

A very easy way will be using a Paint object. You can create one, add a set a text size, and using it's method measureText("some text") you will be able to know width of the text. Having that, you can decide either to split it on multiple lines, or by reducing the text size and checking again until you have a size that fits your screen size.

Upvotes: 1

rogcg
rogcg

Reputation: 10471

These two questions provides a very useful solution for your case.

  1. Auto-fit TextView for Android
  2. Auto Scale TextView Text to Fit within Bounds

Hope this helps you.

Upvotes: 0

Related Questions