In my horizontal LinearLayout I have a TextEdit and an ImageButton. The ImageButton is as high as the TextEdit.
I'd like that the ImageButton is exactly as wide as it's long.
At the moment it looks like the width of the ImageButton is like when there is no scaling (ImageButton width [px] = unscaled drawable width [px]):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/txtName"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageButton
android:id="@+id/btSet"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitEnd"
android:src="@drawable/pin2" />
</LinearLayout>
How it should look like:
try adding
to the ImageButton, that should clip the excess width
I might be a bit late to the party. However there's an easy way to achieve this behavior by overriding the onMeasure(). Here's how it'd look like :
And then you'd simply replace your XML's ImageButton with this custom one :
You'd simply put wrap_content to width or height, depending on which one you want to dictate the size of your button. In the case you want your button to wrap its height to the image, and that the width simply matches the height, you'd use
and use
Just add the following line to your ImageButton and the extra background will fade away:
hope this works
I tried myself,it works
Simply do the Following...
Use Layout Weight ratio 5:1 inside a
LinearLayout
forEditText
andImageButton
Use
or
android:scaleType="centerInside"
in the ImageButton in xml File.