Home > Mobile >  Has LinearLayout been replace with ConstrainedLayout?
Has LinearLayout been replace with ConstrainedLayout?

Time:03-20

[EDIT: if LinearLayout is still ok, then what do I need in the 'dependencies' instead of implementation 'androidx.constraintlayout:constraintlayout:2.0.4', because I cannot find a LinearLayout equivalent?]

I have recently updated to BumbleBee Android Studio. My old code used LinearLayout.

I just want a WebView where the WebView is forced to cover the full width and height of the screen with an advert at the bottom.

Its saying I need ConstrainedLayout now, but I am unsure how to do this? Or can I still use LinearLayout, but why is WebView red underlined. My old code was this:

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <WebView
        android:id="@ id/webView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#00FFFF" />

    <!-- Sizes BANNER, SMART_BANNER, LARGE_BANNER -->
    <com.google.android.gms.ads.AdView
        android:id="@ id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#000000"
        android:paddingTop="5dp"
        app:adSize="LARGE_BANNER"
        app:adUnitId="ca-app-pub-XXXXXXXXXXXXXXXX"/>
</LinearLayout>

CodePudding user response:

No, LinearLayout is not deprecated, unlike RelativeLayout. So, it still has it's place.

This question is a bit broad in that opinions on how flat your View hierarchy should be come into place. However, in complex hierarchy's, ConstraintLayout is the best option.

Edit: Eh, w/e -- something close to this if you're using LinearLayout which implies no overlap:

<?xml version="1.0" encoding="utf-8"?>
<your.support.package.here.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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="0dp"
        android:background="#00FFFF"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/adView"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- Sizes BANNER, SMART_BANNER, LARGE_BANNER -->
    <com.google.android.gms.ads.AdView
        android:id="@ id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:paddingTop="5dp"
        app:adSize="LARGE_BANNER"
        app:adUnitId="ca-app-pub-XXXXXXXXXXXXXXXX"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/webView"
        app:layout_constraintBottom_toBottomOf="parent"/>
</your.support.package.here.ConstraintLayout>

I freeform typed that, so use it as guidance not the final solution hehe ;P. I'd advise reading up on the documentation, and tryin' a few views and it'll come together for you, it's relatively simple in this case.

  • Related