Reputation: 32861
I have a Image Button. I can replace this with a custom view if required. I need to specify the height of the Image Button as Fill_parent. So that the image stretches to the available height in the screen. When doing this i do not want to loose the width. I want it to be a minimum as the height and if greater than height, then it is not a problem. I do not want it this and long. While I do this I also want to maintain the aspect ratio. Please let me know the parameters I need to adjust to get this kind of view. Any kind of help in this regards is appreciated. Thank you for your help and time.
Edit: Below is the complete xml in which I am trying to have a horizontal scroll view with height as fill_parent and am trying to fill it with images which fits to its height and expands or contracts the width as per the aspect ratio. Even if the image is small, i want to scale up so that the height always occupies the Horizontal scroll view.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:layout_weight="3"
android:background="#666666">
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:layout_weight="7"
android:background="#888888">
<HorizontalScrollView android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView android:src="@drawable/image1"
android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:adjustViewBounds="true"
android:id="@+id/attachimagebutton_2" />
<ImageView android:src="@drawable/image2"
android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:adjustViewBounds="true"
android:id="@+id/attachimagebutton_2" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</LinearLayout>
Upvotes: 4
Views: 11147
Reputation: 395
Try this code, it work for me .
public class ButtonSquare extends Button {
public ButtonSquare(Context context) {
super(context);
}
public ButtonSquare(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ButtonSquare(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
super.onMeasure(
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
}
}
Upvotes: 0
Reputation: 485
I recommend taking a different approach, have the parent layout apply gravity="center_horizontal"
and the next step is having a custom view extending the ImageButton
class to set its size as square, it's really simple.
So create the new class, let's name it SquareImageButton
, and extend ImageButton
. add the default constructor with the context and attributes and let it call the super function, you don't need to add anything else.
Now override the onMeasure()
method (which gives you measured width and height in int values), take the minimum value, and (as the method requires) call setMeasuredDimension(w, h)
with the new minimum value so it will be square. It worked for me, and here's the code if you are kinda lost:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int minDimension = Math.min(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(minDimension, minDimension);
}
Upvotes: 1
Reputation: 63
Well, there's no need for it to be an ImageButton
since you can make anything in android a button...
You can add an ImageView
in your XML, give it an id
, the image source, the height as fill_parent
, the width as wrap_content
and you fix the aspect ratio with android:adjustViewBounds="true"
. Then you can also add android:clickable="true"
and find it in code by your id
. Then you can add the onClickListener
.
Hope it works
Upvotes: 2