Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Blog Reader Android App Rebuilding from Scratch Handling a Lack of Data

Seyfullah Erdogan
Seyfullah Erdogan
1,922 Points

How do you add Button inside the RelativeLayout?

Please Help!

2 Answers

Make sure you're in the .xml file under Layout, not under Menu. You can either use the design editor to drag in a button, or create one yourself directly in the xml code. Here's an example:

<Button
    android:id="@+id/loginButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp" />

You can copy and paste that into the xml file in between the RelativeLayout header and </RelativeLayout>.and a button will be created, then you can modify it to suit your needs.

Hi Seyfullah,

All you need to create a button is this syntax within Relative Layout ---- <Button*/>* and is completely valid.

However, adding additional attributes to the button like id, size etc. you can rely on Brahm's answer.

<RelativeLayout 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"
    tools:context=".MainActivity" >

    // This is where you create the button (inbetween the opening and closing Relative Layout tags)

</RelativeLayout>

Hope this helps