Amir_P
Amir_P

Reputation: 9019

LinearLayout not measuring TextView inside

I'm trying to achieve a RecyclerView with GridLayoutManager with square ImageView inside and a TextView under it. This is my row layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dp">

    <com.myCompany.myProject.SquareImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp" />
</LinearLayout>

and my java code to prepare the RecyclerView

categoryRecyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this, 2, LinearLayoutManager.HORIZONTAL, false));

but the ImageView is taking whole row layout and the text is not showing. enter image description here How can I set priority or something like this to views in LinearLayout?

And here is the add shell dumpsys activity top result:

android.support.v7.widget.RecyclerView{bd92bdf VFED..... ........ 0,450-1440,1352 #7f09003a app:id/categoryRecyclerView}
         android.widget.LinearLayout{44fd02c V.E...... ........ 989,0-1440,451}
           com.myCompany.myProject.SquareImageView{5713af5 V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
           android.support.v7.widget.AppCompatTextView{faaf08a V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}
         android.widget.LinearLayout{5cea6fb V.E...... ........ 989,451-1440,902}
           com.myCompany.myProject.SquareImageView{ffc318 V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
           android.support.v7.widget.AppCompatTextView{6e79171 V.ED..... ......ID 193,435-419,435 #7f0900d1 app:id/text}
         android.widget.LinearLayout{d4f8b56 V.E...... ........ 538,0-989,451}
           com.myCompany.myProject.SquareImageView{1d6b7d7 V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
           android.support.v7.widget.AppCompatTextView{bb9f8c4 V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}
         android.widget.LinearLayout{392d3ad V.E...... ........ 538,451-989,902}
           com.myCompany.myProject.SquareImageView{6d106e2 V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
           android.support.v7.widget.AppCompatTextView{be4fa73 V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}
         android.widget.LinearLayout{cbfdd30 V.E...... ........ 87,0-538,451}
           com.myCompany.myProject.SquareImageView{635fda9 V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
           android.support.v7.widget.AppCompatTextView{1fcaf2e V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}
         android.widget.LinearLayout{18acacf V.E...... ........ 87,451-538,902}
           com.myCompany.myProject.SquareImageView{cfd9c5c V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
           android.support.v7.widget.AppCompatTextView{8c7cb65 V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}
         android.widget.LinearLayout{228903a V.E...... ........ -364,0-87,451}
           com.myCompany.myProject.SquareImageView{e1344eb V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
           android.support.v7.widget.AppCompatTextView{5f62248 V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}
         android.widget.LinearLayout{3eeb8e1 V.E...... ........ -364,451-87,902}
           com.myCompany.myProject.SquareImageView{7ff7606 V.ED..... ........ 32,32-419,419 #7f090075 app:id/image}
           android.support.v7.widget.AppCompatTextView{c5f44c7 V.ED..... ......ID 295,435-419,435 #7f0900d1 app:id/text}

Upvotes: 0

Views: 102

Answers (3)

Xenolion
Xenolion

Reputation: 12715

Lets make use of layout weight in LinearLayout. What it will do is setting priority to the SquareImageView and so much of the part is taken by it. Do not get suprised but we will set the height to 0dp to improve perfomance. Go on add this attributes to the SquareImageView.

android:layout_weight="1"
android:layout_height="0dp"

Upvotes: 1

Satender Kumar
Satender Kumar

Reputation: 635

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dp">

    <com.myCompany.myProject.SquareImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
android:layout_weight=1 />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp" />
</LinearLayout>

Your Square image view class is like this

public class SquareImageView extends ImageView {

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

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

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

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

    int height = getMeasuredHeight();
    setMeasuredDimension(height , height);
}

}

Upvotes: 1

Sreyas
Sreyas

Reputation: 784

I have done like this

//customrow.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="@android:color/white"
  >
   <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:orientation="vertical"
        android:paddingLeft="2dp"
        android:paddingRight="2dp">
        <View
            android:layout_width="match_parent"
              android:layout_height="20dp"
        />
        <ImageView
            android:id="@+id/imggridicon"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:src="@drawable/book"/>
            <View
            android:layout_width="match_parent"
                android:layout_height="20dp"
        />
        <TextView
            android:id="@+id/txticdgridmaintitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center|center_horizontal"
            android:text="title"
            android:textColor="@color/colorPrimary"
            android:textSize="13sp"
            android:textStyle="bold"/>
        <View
            android:layout_width="match_parent"
              android:layout_height="20dp"
        />
    </LinearLayout>
</RelativeLayout>
 </android.support.v7.widget.CardView>

//Layout Manager

 GridLayoutManager lLayout = new GridLayoutManager(Activity_Grid_Main_Home.this, 2);
    recyclerView = (RecyclerView) findViewById(R.id.mainhome_recycler);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(lLayout);

Upvotes: 0

Related Questions