Edge
Edge

Reputation: 933

How to set textview width for different screen size width in Android?

<TextView
            android:id="@+id/text"
            android:layout_width="@dimen/width_chat"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/left"
            android:fontFamily="calibri"
            android:padding="8dp"
            android:textColor="#636363"
            android:background="@anim/mercy"
            android:textSize="17dp" />

This is my text view. I have screen size 4 and 6 inch. I want to set width so that according to that it should auto adjust. I have created 2 dimension files in values and values-sw600dp. In both folder I have set dimension width 250dp and and 150 respectively but unable to set size according to different files. Please help me and suggest me where am doing wrong.

Upvotes: 1

Views: 1223

Answers (2)

Saurabh Rajpal
Saurabh Rajpal

Reputation: 1557

You might simply also try to define the dimensions in 'px' instead of 'dp'.. However, assigning weight is always a better solution..

Upvotes: 1

Hemanth
Hemanth

Reputation: 2737

Use a Horizontal LinearLayout and android:layout_weight property to each child.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <View
        android:id="@+id/left"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </View>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

If you want to use dimensions files(e.g. to set text_size), enter the values in dimens.xml in the appropriate folder.

res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-large/dimens.xml
res/values-xlarge/dimens.xml

Upvotes: 1

Related Questions