Make Button Image Square size always. Android

I have an image button. I can replace this with a special kind if necessary. I need to specify the height of the image button as Fill_parent. To stretch the image to an accessible height on the screen. However, I do not want to lose the width. I want it to be as low as height, and if it is greater than height, then this is not a problem. I do not want this for a long time. While I am doing this, I also want to maintain aspect ratio. Please let me know the parameters that I need to configure in order to get this view. Any help in this regard is appreciated. Thank you for your help and time.

Edit: Below is the full xml in which I am trying to get a horizontal scroll view with a height like fill_parent and trying to fill it with images that match its height, and expand or contract the width according to the aspect ratio. Even if the image is small, I want to scale so that the height always takes the form of horizontal scrolling.

<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> 
+4
source share
3 answers

Well, there is no need for it to be ImageButton , since you can do something in android with a button ...

You can add ImageView to your XML, give it id , image source, height as fill_parent , width as wrap_content and fix the aspect ratio with android:adjustViewBounds="true" . Then you can add android:clickable="true" and find it in the code using id . Then you can add onClickListener .

Hope this works.

+2
source

I recommend a different approach, using the gravity="center_horizontal" parent layout layout, and the next step is to create a custom view that extends the ImageButton class to set its size to square, it's really simple.

So create a new class, name it SquareImageButton and continue with ImageButton . add a default constructor with context and attributes and let it call a superfunction, you don't need to add anything.

Now override the onMeasure() method (which gives the measured width and height in int values), take the minimum value and (as needed) call setMeasuredDimension(w, h) with the new minimum value so that it is square. This worked for me, and here is the code if you are a little lost:

 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int minDimension = Math.min(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(minDimension, minDimension); } 
+1
source

Try this code, it works 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)); } 

}

0
source

All Articles