EGHDK
EGHDK

Reputation: 18130

Fit imageView and textView in one small layout in Android

I have a tiny linearlayout that I have to fit an imageView and a textView in. I know this is a terrible way to make an app. I'm just trying to test to see if this would work. The code below only shows the imageview and not the textView. Any ideas?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:src="@drawable/my_icon" />
    </LinearLayout>

</LinearLayout>

Upvotes: 0

Views: 1148

Answers (4)

Atul Bhardwaj
Atul Bhardwaj

Reputation: 6717

Please try this

<TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="TextView" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:src="@drawable/my_icon" />

Upvotes: 0

EGHDK
EGHDK

Reputation: 18130

The answer ended up being to give the textView a weight of 0 and the imageView a weight of 1. That way, textView showed up perfectly but only took as much space as it needed, and the imageView took up the rest of the space. Perfect1

Upvotes: 3

Mufazzal
Mufazzal

Reputation: 883

You should use weight like that:

android:layout_weight="2" in LinearLayout

and layout_weight="1" in imageView and textView

so that both of your view will occupy half of space

Upvotes: 0

Mike T
Mike T

Reputation: 4787

It depends what you want it to look like. If you want the LinearLayout to be split in half, then in your ImageView set

android:layout_weight="1"

The views inside the LinearLayout get space in proportion to their layout_weight so if your ImageView has layout_weight="0" it will get 0 proportion of the View.

Upvotes: 0

Related Questions