Skip to content Skip to sidebar Skip to footer

Bringtofront() Does Not Work With Constraintlayout

I need to bring an ImageView up above all the layouts to show that it is overlapping to the TextView. Unluckily, none of what I've tried from SO helped me. This is what's happening

Solution 1:

Try this one

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayoutxmlns: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" ><TextViewandroid:id="@+id/txtTitle"android:layout_width="0dp"android:layout_height="wrap_content"android:background="@android:color/holo_orange_light"android:gravity="center"android:padding="10dp"android:text="Students"android:textSize="25sp"android:textStyle="bold"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><ImageViewandroid:id="@+id/imgKOala"android:layout_width="100dp"android:layout_height="100dp"android:layout_marginEnd="8dp"android:layout_marginRight="8dp"android:layout_marginTop="-55dp"android:src="@mipmap/ic_launcher"></android.support.constraint.ConstraintLayout>

Solution 2:

Set an elevation on the ImageView. 2dp seems to work.

android:elevation="2dp"

Hope this helped!

Solution 3:

The bringToFront and Z-order of views are all related to the view's parent view, so if you call bringToFront on your imgKOala, it just at the front of its parent view fraLayout1. If you want imgKOala overlap the TextView, you should evaluate the FrameLayout which is sibling to the TextView.

In you layout file, your FrameLayout have the property:

app:layout_constraintTop_toBottomOf="@id/txtTitle"

and ImageView:

android:layout_marginTop="-55dp"

so your ImageView is not fully displayed, because the top 55dp of it is outside of its parent.

Try the code below:

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayoutxmlns: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" ><TextViewandroid:id="@+id/txtTitle"android:layout_width="0dp"android:layout_height="wrap_content"android:background="@android:color/holo_orange_light"android:gravity="center"android:padding="10dp"android:text="Students"android:textSize="25sp"android:textStyle="bold"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/><FrameLayoutandroid:id="@+id/frameLayout"android:layout_width="0dp"android:layout_height="0dp"android:elevation="2dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"><android.support.constraint.ConstraintLayoutandroid:id="@+id/constraintLayout1"android:layout_width="match_parent"android:layout_height="match_parent"><FrameLayoutandroid:id="@+id/fraLayout1"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"><ImageViewandroid:id="@+id/imgKOala"android:layout_width="100dp"android:layout_height="100dp"android:layout_marginEnd="8dp"android:layout_marginRight="8dp"android:src="@mipmap/koala" /></FrameLayout></android.support.constraint.ConstraintLayout></FrameLayout></android.support.constraint.ConstraintLayout>

just set android:elevation=2dp on the FrameLayout which is sibling to the TextView, and the code in your onCreate() method can be removed.

Post a Comment for "Bringtofront() Does Not Work With Constraintlayout"