abdul khan
abdul khan

Reputation: 863

Android View same Width and Height

Following is my xml code. In my LinearLayout. I have two child views , which are aligned using weightSum. I have an ImageView whose Width is aligned properly due to weightSum, but the problem is with its height. I want it's Height to be same dimensions as of its width. Is there any solution without using static dp values.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp">

    <LinearLayout
        android:background="@drawable/background_gradien_yellow"
        android:weightSum="10"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <ImageView
                android:layout_weight="6"
                android:id="@+id/startbtn"
                android:textStyle="bold"
                android:alpha="0.8"
                android:textColor="#fff"
                android:text="@string/start"
                android:layout_centerInParent="true"
               android:background="@drawable/background_gradient_circular"
                android:layout_width="match_parent"
                android:layout_height="150dp"/>


        <LinearLayout
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:orientation="vertical"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <TextView
                android:textColor="@color/White"
                android:id="@+id/tv_challange_title"
                android:textSize="20sp"
                android:text="Early-Bird Challange"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/tv_challange_desc"
                android:alpha="0.5"
                android:textColor="@color/White"
                android:textSize="15sp"
                android:layout_marginTop="5dp"
                android:text="Take 100 steps five times within.. "
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>


        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

Upvotes: 4

Views: 15867

Answers (6)

Ani
Ani

Reputation: 1061

I think you can do it with your activity's java code.

Something like this one:

android.view.ViewGroup.LayoutParams mParams = imageView.getLayoutParams();
mParams.height = imageView.getWidth();
imageView.setLayoutParams(mParams);

Upvotes: 8

J.Doe
J.Doe

Reputation: 366

Make custom ImageView, then set its height same as width. check this answer

Upvotes: 5

abdul khan
abdul khan

Reputation: 863

Found solution by using custom ImageView. Its the Best solution. Setting Height same as width in custom ImageView class.

Java code:

public class SquareImageView extends ImageView {

    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = getMeasuredWidth();
        setMeasuredDimension(width, width);
    }

}

XML code

         <com.mypakage.utils.SquareImageView
            android:background="@drawable/fitbit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

Upvotes: 10

PriyankaChauhan
PriyankaChauhan

Reputation: 953

You set ImageView's scaleType as centerInCrop

android:scaleType="centerInCrop"

Upvotes: 1

mehd azizi
mehd azizi

Reputation: 594

use an image with same size on width and height (better .9patch)

      <ImageView
        android:layout_weight="6"
        android:id="@+id/startbtn"
        android:textStyle="bold"
        android:alpha="0.8"
        android:textColor="#fff"
        android:text="start"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"

        android:src="@drawable/background_gradient_circular"
        android:scaleType="fitCenter"
        android:layout_height="match_parent" />

Upvotes: 0

Uma Achanta
Uma Achanta

Reputation: 3729

you can try this just give weights to inner linear layout also

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingBottom="5dp"`enter code here`
    android:paddingLeft="5dp"
    android:paddingRight="5dp">

    <LinearLayout
        android:background="@color/colorPrimary"
        android:weightSum="10"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_weight="6"
            android:id="@+id/startbtn"
            android:textStyle="bold"
            android:alpha="0.8"
            android:textColor="#fff"
            android:text="start"
            android:layout_centerInParent="true"
            android:background="@drawable/common_ic_googleplayservices"
            android:layout_width="match_parent"
            android:layout_height="150dp"/>


        <LinearLayout
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:orientation="vertical"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="2">


            <TextView
                android:textColor="#fff"
                android:id="@+id/tv_challange_title"
                android:textSize="20sp"
                android:text="Early-Bird Challange"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <TextView
                android:id="@+id/tv_challange_desc"
                android:alpha="0.5"
                android:textColor="#000"
                android:textSize="15sp"
                android:layout_marginTop="5dp"
                android:text="Take 100 steps five times within.. "
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>


        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

Upvotes: 0

Related Questions