0

When that ad at the bottom appears, it reduces space of TextView1 but the space of green (#4CAF50) TextView2 remains the same. I want both textviews' space to be percentally the same even after the ad showing up. I mean, guidelines should lift a bit after the banner shows up. I even tried to make editor count from the top to the banner by adding app:layout_constraintBottom_toTopOf="@+id/adView"to the guidelines but it still counts percents from the top to the bottom of ConstraintLayout. How can I prevent that from happening?

<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">


    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        app:adSize="BANNER"
        app:layout_constraintBottom_toBottomOf="parent"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/topGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        app:layout_constraintGuide_percent="0.67"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/TextView1"

        android:layout_width="0dp"
        android:layout_height="0dp"

        android:background="@color/black"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/bottomGuideline" />

    <TextView
        android:id="@+id/TextView2"

        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#4CAF50"
        app:layout_constraintBottom_toTopOf="@+id/bottomGuideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/topGuideline" />


    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/bottomGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        app:layout_constraintGuide_percent="0.85"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Edit: my main goal is to solve this problem with piano keyboard.

pbendzios
  • 21
  • 4

1 Answers1

0

The problem is caused by android:layout_height="0dp" while using Guidelines: in this way the black TextView will never up when banner is visible.

Take always the black textview: app:layout_constraintTop_toTopOf="@id/bottomGuideline" and app:layout_constraintBottom_toTopOf="@+id/adView": you are saying to set the top to the top of a guideline ,which never change its position, while the bottom to the bottom of the banner. Plus you set 0dp as height, so if the banner has Visibility GONE the black textView will have the bottom to the bottom of the parent (screen). If the banner is VISIBLE, instead, the textview will get the available space between banner and guideline.

You should give wrap_content instead of 0dp, but you should think to something else instead of guidelines. If TextView content is too low for what you want to achieve, you can always use this attribute android:minHeight="your_value_in_dp" to set a minimum height your TextView will have.

  • Thanks a lot for explaining me. Maybe you could suggest something in my case? It's a piano keyboard. https://imgur.com/a/qgyevMX As you see, my key shrinks when I set it to `wrap_content` – pbendzios Jan 15 '21 at 13:13