ashishduh
ashishduh

Reputation: 6699

How to make TextView's height match ImageView or CompoundDrawable

I've tried using a layout of ImageView + TextView and also just a TextView with a CompoundDrawable but can't get the TextView's text height to align with the height of the ImageView. Here's what I have:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/default_padding" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/image"
        android:layout_alignBottom="@+id/image"
        android:layout_toRightOf="@+id/image"
        android:layout_marginRight="@dimen/default_padding" />
</RelativeLayout>

Upvotes: 0

Views: 1501

Answers (2)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Try this way,hope this will help you to solve your problem.

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

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="demo text demo text demo textdemo textdemo textdemo textdemo textdemo textdemo textdemo textdemo textdemo textdemo textdemo textdemo text demo textdemo textdemo textdemo"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher"/>
</LinearLayout>

Upvotes: 1

tknell
tknell

Reputation: 9047

Not very simple to do... but I tried and came up with this solution, but be warned, doing this is certainly not a clean way of defining your UI. I'd rather set the font size to something fitting and not exactly the size of the ImageView.

So here's how it works:

In your layout, additionally set this parameter to the TextView to disable the additional padding:

  android:includeFontPadding="false"

And in your Activity, you have to set the font size with the help of an OnGlobalLayoutChangeListener, because to get the size of the Views, they have to be layouted first. And we need the size before we can change the font size (simplified here, to be short):

private boolean layouted = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.some_layout);

    final TextView t = (TextView) findViewById(R.id.name);
    final ImageView i = (ImageView) findViewById(R.id.image);

    t.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
           //check if already did this, as changing the textSize will cause another layout to happen
           if(!layouted){
               t.setTextSize(TypedValue.COMPLEX_UNIT_PX, i.getHeight());
               layouted=true;
           }
        }
    });

Upvotes: 1

Related Questions