1

Trying to position two cardview in a gridlayout with 2 columns and one row. But getting only the image in the screen. I did the following way. Two cardView in a gridlayout with 1 row 2 rows. I would like they appear evenly positioned in the gridlayout. Trying to position two cardview in a gridlayout with 2 columns and one row. But getting only the image in the screen. I did the following way. Two cardView in a gridlayout with 1 row 2 rows. I would like they appear evenly positioned in the gridlayout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"

android:rowCount="1"
android:columnCount="2">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_columnWeight="1"
android:layout_rowWeight="1">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2"
    android:orientation="horizontal">
 <ImageView
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:src="@drawable/dictionary"
  />
 <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_weight="1"
   android:gravity="center"
   android:orientation="vertical">
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="ТОЛЬ БИЧИГ">
   </TextView>
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="50000 үгийн сантай англи монгол толь "> 
      </TextView>
      </LinearLayout>
      </LinearLayout>
      </androidx.cardview.widget.CardView>  
        <androidx.cardview.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_columnWeight="1"
    android:layout_rowWeight="1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/dictionary"
            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ТОЛЬ БИЧИГ">
            </TextView>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="50000 үгийн сантай англи монгол толь ">
            </TextView>
        </LinearLayout>
    </LinearLayout>
 </androidx.cardview.widget.CardView>
</GridLayout>
</LinearLayout>
dorj bayar
  • 53
  • 8

1 Answers1

0

You are getting only imageview visible here because you set width and height to match parent and also weight to 1

In this situation weight parameter is not working correctly.

       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:weightSum="2"
       android:orientation="horizontal">
       <ImageView
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:src="@drawable/dictionary"
           />
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:gravity="center"
           android:orientation="vertical">
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="ТОЛЬ БИЧИГ">
           </TextView>
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="50000 үгийн сантай англи монгол толь ">
           </TextView>
       </LinearLayout>
   </LinearLayout>

Try to use this

       <ImageView
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:src="@drawable/dictionary"
           />