Is there any way to place an images half is on top of another image using only constraint layout. I know it can be done using relative and frame layouts but in the case of constraint layout is there anyway? prefer ways which do not require any hardcoding of heights/widths
the requirement will look like this
Android App Development for Beginners This example demonstrates how do I overlay two images In Android to set an ImageView. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Constraints define the relation between two different view elements, and chains can be used dictate the spacing between them. In addition, guidelines can be used to create percentage-based layouts. Most of what can be achieved in LinearLayout and RelativeLayout can be done in ConstraintLayout.
A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning on enriching its API and capabilities over time.
you can set layout using only constraint layout like below :
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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"> <ImageView android:id="@+id/imageView4" android:layout_width="0dp" android:layout_height="0dp" android:background="@android:color/holo_orange_dark" app:layout_constraintBottom_toTopOf="@id/guideline" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> <android.support.constraint.Guideline android:id="@+id/guideline" android:layout_width="0dp" android:layout_height="0dp" android:orientation="horizontal" app:layout_constraintGuide_percent="0.5" /> <ImageView android:id="@+id/imageView_upper" android:layout_width="70dp" android:layout_height="70dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="@id/guideline" app:layout_constraintBottom_toBottomOf="@id/guideline" android:background="@android:color/holo_purple"/> </android.support.constraint.ConstraintLayout>
Note: If you are using androidx then you have to use androidx.constraintlayout.widget.ConstraintLayout instead of android.support.constraint.ConstraintLayout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With