Matt
Matt

Reputation: 3912

Making a image in a textview a fixed size - Android

I am trying to assign an image to a TextView. I pulled a .PNG from Google and now am trying to assign the width and height of the TextView to the size I want the image to be. I want it to be a small square but it is turning out extremely wide and short rectangle.

Am I going about this all wrong? Below is the code.

xml code

    <TextView
        android:id="@+id/q1Image"
        android:layout_width="1dp"
        android:layout_height="10dp"
        android:layout_weight=".1"
        android:textSize="8sp"
        android:gravity="center_horizontal" />

java code

q1Image.setBackgroundResource(R.drawable.red_x);

Upvotes: 0

Views: 3615

Answers (3)

Gabe Sechan
Gabe Sechan

Reputation: 93569

What are you trying to do- get the text to go over the image, centered? If so, you need to do it a bit differently

<RelativeLayout
  android:layout_width=wrap_content
  android:layout_height=wrap_content>
    <TextView 
        android:layout_width=fill_parent
        android:layout_height=fill_parent
        android:gravity:"center" />
    <ImageView
        android:layout_width=wrap_content
        android:layout_height=wrap_content
        android:scaleType="centerInside" />
</RelativeLayout>

This will place an image view and a text view at the same spot, making the relative layout equal in size to the image and the text view equal in size to the layout. Then it centers the text via gravity

Upvotes: 1

EvZ
EvZ

Reputation: 12179

Please check the documentation of TextView.

TextView have functions like :

setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) setCompoundDrawablesRelativeWithIntrinsicBounds(int,int,int,int)
and other functions that might help you.

Upvotes: 0

Ramesh Sangili
Ramesh Sangili

Reputation: 1631

public Drawable getDrawable(String source) {

        Log.i(" get drawable mathod ", "");
        Bitmap B = BitmapFactory.decodeResource(getResources(),
                R.drawable.bone);
        BitmapDrawable BD = new BitmapDrawable(B);

        BD.setBounds(0, 0, B.getWidth(), B.getHeight());

        return BD;
    }

You can specify the height and width to get it adjusted based on your requireements

Upvotes: 0

Related Questions