Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to size an Android view based on its parent's dimensions

Tags:

android

layout

How can I size a view based on the size of its parent layout. For example I have a RelativeLayout that fills the full screen, and I want a child view, say an ImageView, to take up the whole height, and 1/2 the width?

I've tried overriding all on onMeasure, onLayout, onSizeChanged, etc and I couldn't get it to work....

like image 645
Mitch Avatar asked Jan 29 '10 01:01

Mitch


People also ask

What is parent layout in Android?

Android RelativeLayout enables you to specify how child views are positioned relative to each other. The position of each view can be specified as relative to sibling elements or relative to the parent.

What is Android layout width?

android:layout_widthSpecifies the basic width of the view. This is a required attribute for any view inside of a containing layout manager.

What is default Android width?

android:defaultWidth Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters). May be a fractional value, which is a floating point number appended with either % or %p, such as " 14.5% ".

What is view parent?

Defines the responsibilities for a class that will be a parent of a View. This is the API that a view sees when it wants to interact with its parent.


2 Answers

I don't know if anyone is still reading this thread or not, but Jeff's solution will only get you halfway there (kinda literally). What his onMeasure will do is display half the image in half the parent. The problem is that calling super.onMeasure prior to the setMeasuredDimension will measure all the children in the view based on the original size, then just cut the view in half when the setMeasuredDimension resizes it.

Instead, you need to call setMeasuredDimension (as required for an onMeasure override) and provide a new LayoutParams for your view, then call super.onMeasure. Remember, your LayoutParams are derived from your view's parent type, not your view's type.

@Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);    this.setMeasuredDimension(parentWidth/2, parentHeight);    this.setLayoutParams(new *ParentLayoutType*.LayoutParams(parentWidth/2,parentHeight));    super.onMeasure(widthMeasureSpec, heightMeasureSpec); } 

I believe the only time you'll have problems with the parent LayoutParams is if the parent is an AbsoluteLayout (which is deprecated but still sometimes useful).

like image 66
Vic Avatar answered Sep 19 '22 14:09

Vic


You could solve this by creating a custom View and override the onMeasure() method. If you always use "fill_parent" for the layout_width in your xml then the widthMeasureSpec parameter that is passed into the onMeasusre() method should contain the width of the parent.

public class MyCustomView extends TextView {      public MyCustomView(Context context, AttributeSet attrs) {         super(context, attrs);     }      @Override     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {         super.onMeasure(widthMeasureSpec, heightMeasureSpec);          int parentWidth = MeasureSpec.getSize(widthMeasureSpec);         int parentHeight = MeasureSpec.getSize(heightMeasureSpec);         this.setMeasuredDimension(parentWidth / 2, parentHeight);     } }    

Your XML would look something like this:

<LinearLayout      android:layout_width="match_parent"     android:layout_height="match_parent">     <view         class="com.company.MyCustomView"         android:layout_width="match_parent"         android:layout_height="match_parent" /> </LinearLayout> 
like image 37
Jeff Avatar answered Sep 19 '22 14:09

Jeff