Rob Carrigan
Rob Carrigan

Reputation: 111

How can I make the Text Size adapt to different sized devices/linear layouts in Android

I am currently working on a layout file that adjusts to android devices of different sizes. So far I have had some success. Using "layout_weight" I have been able to make a linear layout to take up a certain percentage of space in a layout. This means the display can be stretched to cover the whole screen weather its on a 2'7 inch screen or a 10'1 inch screen. However, I am having problems with the text size in the linear layout at the top. How can I make the TextSize adapt to big or small devices? Here is my code.

 <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:baselineAligned="false"
          android:weightSum="1.0"
          android:background="#ff0000"
          >
      <LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="0dip"
          android:layout_weight="0.25"
          android:weightSum="1.0">

     <TextView
        android:id="@+id/textView1"

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="medium"
        android:textSize="30sp"
        android:layout_weight="1"

        android:textAppearance="?android:attr/textAppearanceLarge" />

     </LinearLayout>

     <LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="0dip"
          android:layout_weight="0.25"
          android:weightSum="1.0">        
         <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_1"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_2"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_3"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_4"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />
     </LinearLayout>

     <LinearLayout
         android:orientation="horizontal"
         android:layout_width="fill_parent"
         android:layout_height="0dip"
         android:layout_weight="0.25"
         android:weightSum="1.0">        
           <ImageView 
               android:layout_width="0dip"
               android:layout_height="fill_parent"
               android:src="@drawable/sample_1"
               android:layout_gravity="center_horizontal"
               android:layout_weight="0.25"
           />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_2"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_3"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_4"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />
     </LinearLayout>
     <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="0dip"
      android:layout_weight="0.25"
      android:weightSum="1.0">        
        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_1"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_2"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_3"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
        />

        <ImageView 
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/sample_4"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.25"
               />
      </LinearLayout>
      </LinearLayout>

Upvotes: 1

Views: 775

Answers (2)

Tarsem Singh
Tarsem Singh

Reputation: 14199

inside res folder create different folders like values-ldpi, values-mdpi, values-hdpi, values-xhdpi

than inside these folders create dimens.xml file

should be like

res/values-ldpi/dimens.xml
res/values-mdpi/dimens.xml
res/values-hdpi/dimens.xml

than define different sizes for your TextView inside dimens.xml

Upvotes: 1

KOTIOS
KOTIOS

Reputation: 11194

Make use of value.xml in various folder(ldpi,mdpi,hdpi)

Upvotes: 0

Related Questions