Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I just started learning Android Studio.
I was trying to make multiple buttons and the app only shows the first button.
I don't see any errors in my code...HELP ME!!!

What I have tried:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button3"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/strBtn3" />
    <Button
        android:id="@+id/button2"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="132dp"
        android:layout_height="wrap_content"
        android:text="@string/strBtn2" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/strBtn1" />
    <Button
        android:id="@+id/button4"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/strBtn4" />
Posted
Updated 7-Jul-20 21:28pm
Comments
David Crow 7-Jul-20 8:14am    
The buttons are laying on top of each other because their width is the same as their parent.
Member 14882547 7-Jul-20 8:55am    
Oh that was the problem. thanks!

Any XML attributes prefixed with tools are removed when the app is compiled and are only rendered by Android Studio layout editor.

You need to properly set constraints in your ConstraintLayout, not use absolute positioning.

Or you may instead use RelativeLayout, LinearLayout, etc.

The problem is that your views are not properly constrained (basically they aren't linked to anything so when you run your app it doesn't know where to place them and just defaults to the upper left).

Solution 1 - Right Click on you Layout and select "Convert View" Option. then select "LinearLayout" Option. it will show all three buttons in the output.

Otherwise read about Constraints HERE, once you use the guidelines, your buttons will display correctly.

EDIT: As was rightly pointed out by David Crow, the width is set to the same as their parent causing buttons to overlap and lay on top of each other.
 
Share this answer
 
v2
Comments
David Crow 7-Jul-20 8:15am    
A ConstraintLayout is not being used.
Member 14882547 7-Jul-20 8:53am    
If you are struggling with same problem with me, try LinearLayout in "vertical mode" it works!
David Crow 7-Jul-20 8:56am    
I'm not struggling with anything.
Member 14882547 7-Jul-20 8:50am    
But I am using Linear Layout already.Anyway, I tried Linear Layout in vertical mode and it worked! Thanks
Andre Oosthuizen 7-Jul-20 9:29am    
@David, with info given this was closest I could get to help, I did miss the width though, thank you for pointing out.

@OP, it was a pleasure.
Bro u are unable to see ur buttons because u have given "horizontal" to ur
android:orientation="Horizontal"

U just copy this code and paste it to ur xml file

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button3"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="strBtn3" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="strBtn2" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="strBtn1" />
    <Button
        android:id="@+id/button4"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="strBtn4" />
</LinearLayout>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900