Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6656873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:41:46+00:00 2026-05-26T01:41:46+00:00

Why does my relative layout occupy full screen width <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

  • 0

Why does my relative layout occupy full screen width

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#f00"
    >
    <Button  
        android:id="@+id/Button01"  
        android:text="Press Here"  
        android:layout_alignParentRight="true" 
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"></Button> 
     <Button  
        android:id="@+id/Button02"  
        android:text="02"  
        android:layout_alignParentLeft="true" 
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"></Button>  
</RelativeLayout>

I have specified the relative layout to "wrap_content", then why does it occupy full screen space. Out put is same even if i say android:layout_width="fill_parent". Enlighten

Enlighten me please!

EDIT : I think i was not very clear with my question earlier. Apologies for that.
When I have 2 child views in a relative layout and one of them is left aligned to parent and other is right aligned and relative layouts width is WRAP_CONTENT then I expected the layouts width to be just the sum of width of 2 buttons (isn’t that’s what WRAP_CONTENT means??). I know there are other ways of achieving the UI im looking for but Im just trying to understand these relative layout tags properly.

EDIT 2: I have experimented a bit and it looks like if we using Layout_AlighParentRight with its parent’s width as WRAP_CONTENT then the upper layout width is used for calculation (like few answers pointed out below). But we are using just the Layout_alignParentLeft then it works as expected and layout width is not extending to the complete screen. Thanks for the help folks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:41:46+00:00Added an answer on May 26, 2026 at 1:41 am

    The other answers have correctly pointed out that when your relative layout’s width is set to wrap_content, and its children are aligned to both left and right, the relative layout takes the width of its parent – in this case, the entire screen. If, however, both children were aligned to one side, the relative layout would be as wide as the widest child.

    Now if you want the two buttons to be placed next to each other, and the relative layout to be as wide as the sum of the widths of the buttons, a slightly different approach is needed. Instead of positioning both buttons relative to the parent, do that with one button only (e.g, the first one). Let’s say its positioning is left unchanged (android:layout_alignParentRight="true"). Now the button is floated to the right, so the second button, in order to be position next to it, has to be aligned to the first button’s left side. Thus, we just add android:layout_toLeftOf="@id/Button01" (and remove the android:layout_alignParentLeft="true" part).

    For more, I suggest you check out a very friendly tutorial on relative layouts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a flowing layout (position: static / relative), does the browser store
Short Question I have a 50dip by 40dip button in my Android XML layout.
I have a background image child inside a relative layout: <ImageView android:layout_width=wrap_content android:layout_height=wrap_content android:scaleType=centerInside
How does .NET's MessageBox determine its size relative to the resolution of the screen
Does anyone know a PHP RegEx to allow only relative paths, not absolute? For
Whar are the relative merits between GDI+ and WPF? Also does anyone know the
I'm trying to make a layout that is something similar to how the android
I'm going crazy trying to make an relative layout work. I was getting an
I have a layout xml file with a linear layout. One of the children
EDIT: Does TableLayout not work properly? I see most of you say use relative

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.