I have a LinearLayout with vertical orientation. It has two children elements - a TextView and a Button. The button is to be aligned to the right/end of parent. and the textView must be aligned such that center of both siblings match.
Constraints: Due to some reasons, use of RelativeLayout is NOT allowed. Use of ConstraintLayout is allowed. I think it might help. (Although i want to avoid increasing complexity as much as I can.)
Can this be achieved? If yes, How can this be achieved?
Here is my activity_main.xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/frame_layout_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity"
tools:layout_height="400dp">
<View android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/green">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/left_overlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:layout_weight="0.65"
android:gravity="start"
android:orientation="vertical">
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:paddingBottom="8dp"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:text="John Mayer Doe"
android:textSize="20sp"
tools:text="Title"/>
<TextView
android:id="@+id/textViewSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:autoSizeTextType="uniform"
android:ellipsize="end"
android:maxLines="2"
android:minLines="1"
android:paddingBottom="8dp"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:textSize="15sp"
android:text="Alice Bob Charlie Dave Eve Frank"
tools:text="Subtitle"/>
</LinearLayout>
<LinearLayout
android:id="@+id/right_overlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_weight="0.35"
android:gravity="center_horizontal|end"
android:orientation="vertical">
<TextView
android:id="@+id/textontopofbutton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:gravity="center_horizontal"
android:textAlignment="center"
android:textColor="@color/black"
android:text="text on top"
tools:text="on top of button"/>
<Button
android:id="@+id/bottomrightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="4dp"
android:textAlignment="center"
android:textColor="@color/white"
android:background="@color/colorAccent"
android:text="Click me now"
tools:text="Tap here now"/>
</LinearLayout>
</LinearLayout>
colors.xml for ref:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorAccentBright">#D81B60</color>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
<color name="green">#3D9D2A</color>
<color name="red">#D80D15</color>
<color name="yellow">#B7AB4C</color>
<color name="maroon">#BB4767</color>
</resources>
MainActivity.kt
package com.example.flames
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Screenshot: With Layout Bounds:
Goal: I want the "text on top" to be center-aligned with the button.
I appreciate your help. Thanks.
Yes you can easily do with ConstraintLayout
with minimum complexity,
Here is the solution that you can try,
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view5"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/green"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:paddingBottom="8dp"
android:text="John Mayer Doe"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/textViewSubTitle"
app:layout_constraintStart_toStartOf="@+id/view5" />
<TextView
android:id="@+id/textViewSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:autoSizeTextType="uniform"
android:ellipsize="end"
android:maxLines="2"
android:minLines="1"
android:paddingBottom="8dp"
android:text="Alice Bob Charlie Dave Eve Frank"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="@+id/view5"
app:layout_constraintStart_toStartOf="@+id/view5" />
<TextView
android:id="@+id/textontopofbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp"
android:gravity="center_horizontal"
android:text="text on top"
android:textAlignment="center"
android:textColor="@color/black"
app:layout_constraintBottom_toTopOf="@+id/bottomrightbutton"
app:layout_constraintEnd_toEndOf="@+id/bottomrightbutton"
app:layout_constraintStart_toStartOf="@+id/bottomrightbutton" />
<Button
android:id="@+id/bottomrightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="4dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@color/colorAccent"
android:text="Click me now"
android:textAlignment="center"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="@+id/view5"
app:layout_constraintEnd_toEndOf="@+id/view5" />
</android.support.constraint.ConstraintLayout>
Yes, you can do either
<LinearLayout
android:id="@+id/right_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/textontopofbutton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:textAlignment="center"
android:text="text on top"/>
<Button
android:id="@+id/bottomrightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:textAlignment="center"
android:textColor="@color/white"
android:background="@color/colorAccent"
android:text="Click me now"
tools:text="Tap here now"/>
</LinearLayout>
or using constraint layout
<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="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textontopofbutton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:text="text on top"
android:textAlignment="center"
app:layout_constraintBottom_toTopOf="@+id/bottomrightbutton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bottomrightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:background="@color/colorAccent"
android:text="Click me now"
android:textAlignment="center"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textontopofbutton"
tools:text="Tap here now" />
I have made a very minor fix to your XML, this should work.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/frame_layout_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity"
tools:layout_height="400dp">
<View android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/green">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/left_overlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:layout_weight="0.65"
android:gravity="start"
android:orientation="vertical">
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:paddingBottom="8dp"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:text="John Mayer Doe"
android:textSize="20sp"
tools:text="Title"/>
<TextView
android:id="@+id/textViewSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:autoSizeTextType="uniform"
android:ellipsize="end"
android:maxLines="2"
android:minLines="1"
android:paddingBottom="8dp"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:textSize="15sp"
android:text="Alice Bob Charlie Dave Eve Frank"
tools:text="Subtitle"/>
</LinearLayout>
<LinearLayout
android:id="@+id/right_overlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_weight="0.35"
android:gravity="center_horizontal|end"
android:orientation="vertical">
<TextView
android:id="@+id/textontopofbutton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="end"
android:layout_margin="4dp"
android:textAlignment="center"
android:textColor="@color/black"
android:text="text on top"
tools:text="on top of button"/>
<Button
android:id="@+id/bottomrightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="4dp"
android:textAlignment="center"
android:textColor="@color/white"
android:background="@color/colorAccent"
android:text="Click me now"
tools:text="Tap here now"/>
</LinearLayout>
</LinearLayout>
All the best :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.