0

i have a problem with the ImageView. I want left of my TextView a ImageView with icon, but only have the height of the TextView. So it should scaled down, because the image is larger.

this is my code:

<RelativeLayout
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:padding="5dip" >

   <LinearLayout
       android:id="@+id/icon_container"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBottom="@+id/name"
       android:layout_alignTop="@+id/name" >

       <ImageView
           android:id="@+id/icon"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:contentDescription="@null"
           android:scaleType="fitStart"
           android:src="@drawable/m1" />
   </LinearLayout>

   <TextView
       android:id="@+id/name"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_toRightOf="@+id/icon_container"
       android:text="Large Text"
       android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

(works also without LinearLayout)

but this creates a gap between icon and text, as you see here: enter image description here

now i really dont know how i can solve this. I also tried a LinearLayour with Icon and TextView within, but that also doesnt work.

EDIT:

i found no solution without code, but with code, i called:

ImageView icon = (ImageView)vi.findViewById(R.id.icon);
TextView name = (TextView)vi.findViewById(R.id.name);

name.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
name.setText("Test 1");

int height = name.getMeasuredHeight();
icon.getLayoutParams().height = height;
icon.getLayoutParams().width = height;
vtni
  • 950
  • 3
  • 14
  • 43

1 Answers1

1

You could try using drawableLeft on your TextView, this places a drawable on the left hand side of the TextView. I think it may scale with the height of the TextView, if not you should be able to get the height of the TextView and scale it yourself.

Programmatically set left drawable in a TextView

EDIT Here are some links on getting the height of views

How to retrieve the dimensions of a view?

getHeight returns 0 for all Android UI objects

Android TextView has height and width of 0

The most useful might be in the last question

"To find the width And height of a View before it being drawn:

First call measure"

view.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED)

"Now you can get width using getMeasuredWidth and height using getMeasuredHeight"

int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
Community
  • 1
  • 1
Dave S
  • 3,378
  • 1
  • 20
  • 34
  • i tried this, but the textview scales to the height of the image. even if i set the width to e.g. 30dp, the image gets cut, instead of scaled. i also try to do it without code, only xml if possible. – vtni Oct 03 '14 at 01:09
  • You can set a width and height and a scale type, which can work for a lot of cases, but not all. Often times it's easier and makes more sense just to scale the image yourself. – Dave S Oct 03 '14 at 05:35
  • in some cases, this may be true, but i use this image for different screen sizes. of course i could make one for xhdpi, hdpi, ... but on some screens the problem would also be there (when the height of the image is larger than the height of my TextView. if the picture is smaller, it gets scaled up and looks not good. – vtni Oct 03 '14 at 13:31
  • And if you want to scale differently in those cases you need to use code, xml won't do it. – Dave S Oct 03 '14 at 16:36
  • i use this in a ListView with adapter and there i can't get the width/height in the function `getView()` – vtni Oct 03 '14 at 20:16
  • sure you can, you use TextView myTextView = (TextView) view.findViewById(R.id.myListItemTextView); If the TextView is outside the ListView, then you want to do it in the Activity or Fragment that hosts the TextView, not the adapter. – Dave S Oct 03 '14 at 20:19
  • i did, but when i log `Log.d("height", ""+myTextView.getHeight());` it is 0. (in the getView function) – vtni Oct 03 '14 at 20:21
  • Ah I see, the TextView hasn't measured itself yet, instead store an array of TextView references, then perform the action on onResume(), at that point in the lifecycle the TextView should have measured itself. If that doesn't work there are a few work arounds, see my Edit for some links that should help you. – Dave S Oct 03 '14 at 20:25
  • the adapter class has no onResume(): `public class LazyAdapter extends BaseAdapter{...}` maybe i should start a new thread – vtni Oct 03 '14 at 20:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62437/discussion-between-vtni-and-dave-s). – vtni Oct 03 '14 at 20:32