1

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> 

0

5 Answers 5

1

you are getting view height to early before it draws on screen. better approach is get the screen width first using observer like this

view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { view.getViewTreeObserver().removeOnGlobalLayoutListener(this); view.getWidth(); //width is ready here // assign your image view the same height // divide this width by 2 and assign same height width to your cars views } }); 

you may follow this thread for more details

Sign up to request clarification or add additional context in comments.

1 Comment

How to set the view.height? The view.setLayoutParam() doesn't work
1

That happens because the view have not been sized yet, try this :

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final CardView cardView1 = (CardView) findViewById(R.id.cardView1); cardView1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // remove the layout listener if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { cardView1.getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { cardView1.getViewTreeObserver().removeGlobalOnLayoutListener(this); } final int height = cardView1.getHeight(); if (height >= 0) { // use the linearLayout LayoutParams LinearLayout.LayoutParams newParams = new LinearLayout.LayoutParams(height, height); cardView1.setLayoutParams(newParams); } } }); } 

2 Comments

the height is calculated correctly, but cardView1.setLayoutParams(params); doesn't work
Please check the edit and let me know if it makes any difference
0

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.

Comments

0

If you want that view to be square Its better to Create it By extending the targeted widget class Like below:

 public class SquareView extends View{ public SquareView(Context context) { super(context); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, widthMeasureSpec); } } 

Just assign the width of the view and it will take it as height to form a square.

Comments

0

Try this, you will get to know how to use weight in LinearLayout.

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="2"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <!--Place your image view here--> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical" android:weightSum="2"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" android:weightSum="2"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <!--CardView 1st--> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <!--CardView 2nd--> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" android:weightSum="2"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <!--CardView 3rd--> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <!--CardView 4th--> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.