Setting view height dependent on its width in android in layout xml

how to set layout height and width dynamically in android
constraint layout
how to set layout for different screen size in android
how to auto resize, compatible, adjust screen size for all android devices
framelayout
layoutparams
how to set android layout to support all screen sizes
android set layout params programmatically

Is it possible that a view should make it height same as it's width?

or any other layout for this purpose, because for vector image it's compulsory to provide width and height.

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/googleLoginBtn"
            android:orientation="horizontal"
            android:layout_centerVertical="true">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">


                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="<height should be equal to the width>"
                    app:srcCompat="@drawable/simple"
                    tools:ignore="MissingPrefix"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Snappy"
                    android:textColor="#FF4081"
                    android:textSize="30dp"
                    android:singleLine="true"
                    android:gravity="center"
                    android:layout_margin="10dp"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Type less, do more. Fastest way to transfer money &amp; make other transactions."
                    android:layout_margin="10dp"/>

            </LinearLayout>


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:srcCompat="@drawable/simple"
                    tools:ignore="MissingPrefix"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Smart"
                    android:textColor="#FF4081"
                    android:textSize="30dp"
                    android:gravity="center"
                    android:layout_margin="10dp"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Type less, do more. Fastest way to transfer money &amp; make other transactions."
                    android:layout_margin="10dp"/>

            </LinearLayout>
</LinearLayout>

It's not possible entirely in xml without using a custom ImageView. This would be a solution:

public class SquareImageView extends ImageView {
  @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = getMeasuredWidth();
    setMeasuredDimension(width, width);
  }
}

However if your Image is already Square you can use the normal ImageView:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true" />

A third solution would be setting the width and height directly in your Code:

LayoutParams params = imageView.getLayoutParams();
params.height = params.width ;
imageView.setLayoutParams(params);

ViewGroup.LayoutParams, or any other layout for this purpose, because for vector image it's compulsory to provide width and height. <LinearLayout android:layout_width="match_parent"  – Have my main Activity linked with an xml layout file that has two empty nested linear layouts that fill the screen. – Make a custom view that houses the main draw code. For this example, all I do is fill the screen with black and display the available screen size.

You should be able to use the Percent support library for this. You can either replace the LinearLayout the ImageViewis inside of with a PercentRelativeLayout and have the other views android:layout_belowor wrap the ImageView in a PercentFrameLayout.

I'm going to assume you've already defined the app namespace in this file as xmlns:app="http://schemas.android.com/apk/res-auto" since I see you using the namespace.

Once you've included the appropriate support library, the PercentFrameLayout approach would look something like this:

   <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

          <android.support.percent.PercentFrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="fill_parent"
                app:layout_aspectRatio="100%"
                app:srcCompat="@drawable/simple"
                tools:ignore="MissingPrefix"/>
            </android.support.percent.PercentFrameLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Snappy"
                android:textColor="#FF4081"
                android:textSize="30dp"
                android:singleLine="true"
                android:gravity="center"
                android:layout_margin="10dp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Type less, do more. Fastest way to transfer money &amp; make other transactions."
                android:layout_margin="10dp"/>

        </LinearLayout>

FrameLayout, int, FILL_PARENT. Special value for the height or width requested by a View. layoutDirection). Resolve layout parameters depending on the layout direction. Although the actual layout for this view depends on other attributes in its parent view and any sibling views, this TextView intends to set its width to fill all available space (match_parent) and set its height to exactly as much space is required by the length of the text (wrap_content). This allows the view to adapt to different screen sizes

Just a little update: The PercentFrameLayout class was deprecated in API level 26.1.0. Consider using ConstraintLayout and associated layouts instead. More information here: https://developer.android.com/reference/android/support/percent/PercentFrameLayout

Support different screen sizes, The size of the FrameLayout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits). Views that are View. By adding android:weightSum=”100″ attribute inside LinearLayout gives us the facility to divide its child view elements using adding another attribute android:layout_weight=” “ into child view elements. So here is the complete step by step tutorial for Set height and width in percentage format in layout android.

use this

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
       int myWidth = (int) (parentHeight * 0.5);
       super.onMeasure(MeasureSpec.makeMeasureSpec(myWidth, MeasureSpec.EXACTLY), heightMeasureSpec);
}

LinearLayout, "wrap_content" tells the view to set its size to whatever is necessary to fit the Although the actual layout for this view depends on other attributes in its parent view For more information, see how to adjust the view size in ConstraintLayout. res/layout/main_activity.xml # For handsets (smaller than 600dp  In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. You can add a spinner to your layout with the Spinner object. You should usually do so in your XML layout with a <Spinner> element. For example:

Constructing View Layouts, Select an option To control how linear layout aligns all the views it contains, set a value all children with a weight will be considered having the minimum size of Measure the view and its content to determine the measured width of cached information that depends on the resolved layout direction,  Notice that in the screen XML the RelativeLayout was given an id (@+id/screen), which means the base View’s width and height can be grabbed (change R.id.imageView to R.id.screen in the code): Notice that the layout height is less than the screen height because of the notification bar. Finding the Size of an Android View During Screen Construction

To set an image to fill 50% of the screen on all Android devices , It's important to note the class hierarchy of these View Layouts. and then setting the android:layout_weight value for each subview to determine width distribution. Using the above XML, btnLocEnable will have 2/5 of total container width and Using this approach is especially useful in matching the widths or heights  Below is part of the code from our ImageView. We are trying to say that we want to resize our logos to always have an 18dp height and then use whatever width they need. We also had set scaleType to fitXY. android:layout_width="wrap_content" android:layout_height="18dp" android:scaleType="fitXY" Let’s have a look at what fitXY does.

Percentage Width/Height using Constraint Layout, get the screen size of device and set the image height screenheight/2 get I think it's wrong to hold a single resource for all Android device. you should take 512*512 full length image and copy paste the following code into your xml file. <​LinearLayout; android:layout_width="fill_parent" 3.2k views · View 4 Upvoters. To create a fluid layout, use automatic or proportional sizing for elements, and allow layout panels to position their children as needed. Here are some common layout properties and how to use them to create fluid layouts. Height and Width. The Height and Width properties specify the size of an element. You can use fixed values measured in

Comments
  • Possible duplicate of making grid view items square
  • I am using Vector Image, that's why second solution doesn't fit.
  • One simple question, Does onMeasure() function is called everytime the size is changed?
  • onMeasure is called every time the parent view needs to change its layout. So yes. It even can be called several times.