Search code examples
androidheightandroid-relativelayout

android relativelayout height not working corretly


I want android:text="30" textview be center position in Relativelayout.

but android:layout_height="match_parent" is not working correctly. It look like "wrap_content".

I was trying everything I found adding one more Relativelayout and do working correctly.

  1. how text"30" move center position
  2. why match_parent not work?
  3. why added one more Relativelayout then working correctly?

    <androidx.core.widget.NestedScrollView
        android:id="@+id/product_grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="56dp"
        android:background="@color/productGridBackgroundColor"
        android:elevation="8dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:orientation="vertical"
            android:padding="24dp"
            android:paddingTop="16dp">
    
            <com.google.android.material.card.MaterialCardView
                style="@style/Widget.MaterialComponents.CardView"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_marginLeft="@dimen/mtrl_card_spacing"
                android:layout_marginTop="@dimen/mtrl_card_spacing"
                android:layout_marginRight="@dimen/mtrl_card_spacing"
                android:minHeight="200dp">
    
                **<!--height = match_parent not working-->**
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/home_card_title_level"/>
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
    
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:text="30"/>
    
                </RelativeLayout>
    
                <!--**if this code added then work perfectly**
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
                -->
    
            </com.google.android.material.card.MaterialCardView>
    
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
    

not correct added one more relativelayout


Solution

  • Remove Relative Layout and add android:layout_gravity="center

      <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:text="30"/>