This question may look similar to the following question, but I need help solving my use case. It provides explanations for adding constraints inside the XML file.
I'm trying to migrate from RelativeLayout to ConstraintLayout, but I need help programmatically adding the ad view to the bottom and the rest of the content above it. I chose the code approach to allow dynamic padding that we receive from a backend server.
Previously, I used a FramedLayout to hold the ad view (banner). As a result, the WebView above was resized automatically during the view update (I don't know the best solution) and shrank upon ad removal.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".MainActivity"> <WebView android:id="@+id/webview" android:background="#000000" android:layout_width="match_parent" android:layout_height="match_parent" android:animateLayoutChanges="true" android:layout_above="@id/ad_view_container_bottom" /> <FrameLayout android:id="@+id/ad_view_container_bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_alignParentBottom="true"> </FrameLayout> </RelativeLayout> I want to add the ad view programmatically to the bottom of the screen and make the WebView resize to fit the screen. I tried the following ConstraintLayout, but it places the ad view container beneath the WebView without resizing it (the ad is not visible).
<?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=".MainActivity"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" android:animateLayoutChanges="true" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"/> <FrameLayout android:id="@+id/ad_view_container_bottom" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/webview" app:layout_constraintBottom_toBottomOf="parent"> </FrameLayout> </androidx.constraintlayout.widget.ConstraintLayout> My questions are:
- What is the correct way to place the ad view beneath the WebView?
- Do I need the
FrameLayout, or can I achieve it more straightforwardly (the width is screen width, and the height size is fixed)? - Do I gain any performance improvement here regarding screen rendering (less layout-and-measure stages)?