0

I'm trying to create a TableLayout and AdMob component inside ConstraintLayout. I want to make the tablelayout to be in center of the screen vertically and the ads component to be at the end. My code is follows,

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".relax.LevelsUX">
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
           <TableLayout
              android:id="@+id/relax_level_ux_TL"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:stretchColumns="*">
            </TableLayout>

            <com.google.android.gms.ads.AdView
              android:id="@+id/relax_gameplay_adView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal|bottom"
              app:adSize="BANNER"
              app:adUnitId="ca-app-pub-3940256099942544/6300978111">
            </com.google.android.gms.ads.AdView> 
     </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

But the problem is the ads component is not visible, as I'm giving android:layout_height="match_parent" for TableLayout. Now, when I make the android:layout_height="wrap_content" for TableLayout, the layout is going on the top of the screen. I can't give it a fixed height, as I'm adding rows dynamically.

How can I solve this problem?

Abhishek
  • 6,912
  • 14
  • 59
  • 85

1 Answers1

1

Make tablelayout height 0dp, with layout_weight=1 , It will work.

       <TableLayout
          android:id="@+id/relax_level_ux_TL"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:gravity="center"
          android:stretchColumns="*">
        </TableLayout>
akashzincle
  • 1,108
  • 6
  • 15
  • Awesome.. It works!! Can you please explain how and why does it work? I can mark it as answer, after stackoverflow allows me to do so. – Abhishek Apr 14 '20 at 14:57
  • So basically in LinearLayout, there is a property called "layout_weight", When in this layout all the views are either fixed height/wrap_content and when there is a view with layout_weight = 1, It covers the rest space remaining, You can read about layout_weight property of Linearlayout more on Google – akashzincle Apr 14 '20 at 14:59
  • Got it. Will mark it as answer in 2 more mins :) For others, you can look @ https://stackoverflow.com/questions/3995825/what-does-androidlayout-weight-mean to understand `android:layout_weight ` – Abhishek Apr 14 '20 at 15:03
  • I understand, this solution works. Do you recommend a better way of doing this? – Abhishek Apr 14 '20 at 15:05
  • 1
    Actually this is the best way, When you have many views of fixed height/wrap_content, but a view you want to cover rest space, We always follow this layout_weight paradigm. – akashzincle Apr 14 '20 at 15:07