Junseok Lee
Junseok Lee

Reputation: 715

What's a simple way to make a view's layout_width="match_parent" and make the height equal to its width?

I would like a view styled similar to the album art in the Rdio app (img)

I don't know how I should be going about this. I want to load a bitmap into the view, if that affects the solution at all.

Upvotes: 0

Views: 703

Answers (1)

Femi
Femi

Reputation: 64700

In this case (where the control has the full width of the device) you can always just use getWindowManager().getDefaultDisplay().getWidth(); to get the width and then manually set the height.

If you're not using the full display width then you'll probably need to override onMeasure in the View class and set the height to match the width in there, perhaps like this:

@Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec )
{
    viewWidth = MeasureSpec.getSize( widthMeasureSpec );
    viewHeight = MeasureSpec.getSize( heightMeasureSpec );

    if(viewWidth != 0) viewHeight = viewWidth;
    setMeasuredDimension( viewWidth, viewHeight );
}

Upvotes: 2

Related Questions