Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make layout width equal height?

Tags:

android

I want to have an ImageView with width=fill_parent, and the height should be whatever the width is. I'm not sure if there's a way to specify that in xml, is the only option to create my own ImageView-derived class?:

public class MyImageView extends ImageView {

    // Just return whatever the current width is?
    private int measureHeight(int measureSpec) {
        return getWidth();
    }
}

Is this the way to go, any other options? (I'm not sure if the above is even correct, not sure if the width measurement even takes place before the height measurement for example)

Thanks

like image 604
user291701 Avatar asked Apr 21 '12 21:04

user291701


People also ask

What is Layout_constraintdimensionratio?

A ConstraintLayout is a ViewGroup which allows you to Position and size Views in a flexible way. It is basically RelativeLayout on Steriods. The Views will be positioned and sized w.r.t other Views with the Use of Constraints.

What is layout height?

Android layout Width & HeightThis property is used to set the width of the given control. This property has following options:- Wrap_content :- when you set width as wrap_content then it will take the space according to the content. For example. Figure 1.

How do you find the width of a layout?

If you wanted to get the actual width of your view you would have to call getWidth() on your view once the layout has been inflated and displayed. Calling getWidth() before your layout has been displayed will result in 0. Show activity on this post. Which onMeasure() call are you talking about?


1 Answers

You can get the width with the method

imageView.getMeasuredWidth();

So, you can set it's height

imageView.setLayoutParams(new LayoutParams(imageView.getMeasuredWidth(), imageView.getMeasuredWidth()));
like image 147
Leandro Silva Avatar answered Oct 03 '22 14:10

Leandro Silva