Search code examples
androidwidth

Change width of view dynamically doesn't work


I divide the main layout to 4 sections. First part is a image view that has the layout_weight="2" and two linear layouts with the layout_weight="1" for each one. in every linear layouts there are 2 CardViews, oriented horizontally, which I want to show them with equivalent width and height. enter image description here

I change the width of each CardView like below:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_page);

    CardView cardView1 = (CardView) findViewById(R.id.cardView1);
    ViewGroup.LayoutParams params = cardView1.getLayoutParams();

    params.width = params.height; // params.height == -1 ???
    cardView1.setLayoutParams(params);
}

Do you know whats the problem? Thanks.

Xml file is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4">

<ImageView
    android:id="@+id/userImage"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:weightSum="2">

    <android.support.v7.widget.CardView
        android:id="@+id/cardView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        app:cardCornerRadius="4dp"
        android:elevation="5dp"
        app:cardElevation="10dp"
        android:paddingBottom="15dp"
        android:paddingEnd="15dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingStart="15dp"
        android:paddingTop="15dp"
        android:layout_margin="15dp"
        app:cardBackgroundColor="?attr/colorButtonNormal">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"/>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardView2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:elevation="5dp"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="10dp"
        android:paddingBottom="15dp"
        android:paddingEnd="15dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingStart="15dp"
        android:paddingTop="15dp"
        android:layout_margin="15dp"
        card_view:cardBackgroundColor="?attr/colorButtonNormal">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"/>
    </android.support.v7.widget.CardView>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:weightSum="2">

    <android.support.v7.widget.CardView
        android:id="@+id/cardView3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:elevation="5dp"
        app:cardCornerRadius="4dp"
        android:background="@color/cardview_dark_background"
        app:cardBackgroundColor="?attr/colorButtonNormal"
        app:cardElevation="10dp"
        android:paddingBottom="15dp"
        android:paddingEnd="15dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingStart="15dp"
        android:paddingTop="15dp"
        android:layout_margin="15dp">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"/>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardView4"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:elevation="5dp"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="10dp"
        android:paddingBottom="15dp"
        android:paddingEnd="15dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingStart="15dp"
        android:paddingTop="15dp"
        android:layout_margin="15dp"
        card_view:cardBackgroundColor="?attr/colorButtonNormal">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"/>
    </android.support.v7.widget.CardView>
</LinearLayout>


Solution

  • If your problem is to show your view as a Square i.e Width and height should be equal, you can easily manipulate OnMeasure() method of view.
    Since your view is CardView, I'm modifying that,

    import android.content.Context;
    import android.support.v7.widget.CardView;
    import android.util.AttributeSet;
    
    public class SquareCardView extends CardView {
    
        public SquareCardView(Context context) {
            super(context);
        }
    
        public SquareCardView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public SquareCardView(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();
            //noinspection SuspiciousNameCombination
            setMeasuredDimension(width, width); // here comes manipulation
        }
    }
    

    And you're done :) Define width and height will be equals to width.