Issues with XML Layout and Software Buttons

Multi tool use
Multi tool use


Issues with XML Layout and Software Buttons



So basically I have an activity with 4 buttons that in Android Studio look out of the way of the software buttons at the bottom of the screen but when I run it on my phone they are not. I'm not sure if I have the right constraints on the bottom button or not?




<?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"
tools:context=".NewMainMenu"
tools:layout_editor_absoluteY="25dp">

<Button
android:id="@+id/button"
android:layout_width="411dp"
android:layout_height="100dp"
android:layout_marginTop="258dp"
android:background="@android:color/holo_blue_light"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button4"
android:layout_width="411dp"
android:layout_height="100dp"
android:background="@android:color/holo_red_light"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

<Button
android:id="@+id/button3"
android:layout_width="411dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_light"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />

<Button
android:id="@+id/button2"
android:layout_width="411dp"
android:layout_height="100dp"
android:background="@android:color/holo_orange_light"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3" />
</android.support.constraint.ConstraintLayout>



What the app looks like in Android Studio



This is what the app looks like on my phone





post your xml code
– Abu Yousuf
Jul 1 at 5:17



xml





@AbuYousuf I've updated the post with the XML code
– Chris
Jul 1 at 6:07




4 Answers
4



if you use from ConstraintLayout, you can add this attribute:


app:layout_constraintBottom_toBottomOf="parent"



by this code, your view sticks on bottom of layout.



and for other views you can use from this code:


app:layout_constraintBottom_toBottomOf="@+id/{bottom View}"





My code already has this sadly D:
– Chris
Jul 1 at 6:03





some codes is not right;
– mehrdad Ss
Jul 1 at 9:25



The problem is that you are explicitly setting the button heights and the top button's top margin to fixed values. On your device, the total sum of all the heights and the padding exceed the height of the device.



An easy fix is to not constrain the top of the topmost button to the parent and remove the top margin. Then it will lay above the button below it but won't push down from the top of the parent.



Generally speaking, you should avoid fixed-size widths and height for this exact reason. You can leverage things like weighted chains in ConstraintLayout to size things proportionally instead of explicitly.



See the ConstraintLayout docs for more.



Hope that helps!



Your constraints are wrong. You have used both app:layout_constraintTop_toBottomOf and app:layout_constraintBottom_toTopOf constraints which is wrong. Use only app:layout_constraintBottom_toTopOf constraint to stack one above other.
Use this code:


app:layout_constraintTop_toBottomOf


app:layout_constraintBottom_toTopOf


app:layout_constraintBottom_toTopOf


<?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"
tools:context=".NewMainMenu"
tools:layout_editor_absoluteY="25dp">

<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="@android:color/holo_blue_light"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="@android:color/holo_red_light"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_light"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="@android:color/holo_orange_light"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"


/>
</android.support.constraint.ConstraintLayout>



enter image description heresome codes is not right
please write below codes:


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

<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_green_light"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_blue_light"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent" />

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_orange_dark"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />








By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

7 0D8k2HPehT9US,KhQ6iR o44olfb5erqnbT9
FvjMRmueaips,8HJbqoY8bLx,IVAYD,cXiN1S,p1XJZdsl2mAD9dn1cG5dR

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters