Chand Ninder
Chand Ninder

Reputation: 171

How to auto width and height?

I am making app with Android Studio and want to auto set width and height of image that I am showing on front page. my code is like this

 <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="wrap_content"
        android:id="@+id/linearLayout1"
        android:orientation="vertical"
        android:layout_height="fill_parent">

        <TextView
            android:id="@+id/textView"
            android:layout_width="375dp"
            android:layout_height="220dp"
            android:layout_marginBottom="5sp"
            android:adjustViewBounds="true"
            android:background="@drawable/japuji"
            android:clickable="true"
            android:focusable="true"
            android:linksClickable="true"
            android:scaleType="fitXY"
            android:textStyle="bold" />

this is the scrollview and when i set fill_parent, wrap_content in TextView its not working either, when i test it on big screen images are small ,

i have tried all like changing width to fill_parent and wrap_content

Upvotes: 0

Views: 1958

Answers (2)

Tamim Attafi
Tamim Attafi

Reputation: 2511

You should not display an image as a background of a view, instead, you should use ImageView

    <ImageView
        android:contentDescription="@string/imagedesc"
        android:id="@+id/my_image"

        android:src="@drawable/your_image" 

        android:scaleType="fitCenter"

        android:adjustViewBounds="true"

        android:layout_width="wrap_content"
        android:layout_height="match_parent"
       />

src : sets the image drawable

scaleType : sets the desired scale type, in this case fitCenter with adjustViewBounds set to true and width or height (one of them) to wrap_content will show the full image.

EDIT : if you have a fixed view size and you want to cover it all with that image drawable, you should use centerCrop as a scaleType instead of fitCenter

Upvotes: 2

LarsH
LarsH

Reputation: 27994

You're apparently displaying the image (android:background="@drawable/japuji") in the TextView, but the TextView has its height and width hard-coded: android:layout_width="375dp" android:layout_height="220dp"

Instead set the TextView's height and width to match_parent.

Upvotes: 0

Related Questions