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 8025931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:19:34+00:00 2026-06-04T23:19:34+00:00

I’ve been using a bit of a hack to stretch a view inside a

  • 0

I’ve been using a bit of a hack to stretch a view inside a parent RelativeLayout so that it starts at top-left and ends an bottom-center. While RelativeLayout does let you position something at the center of itself, it doesn’t however seem to let you stretch something to that same point.

My solution was simple, position something in that center, and give it 1px dimensions so its the actual center of the parent RelativeLayout. Don’t get me wrong, this works and I’ve had no issues whatsoever with it, but if there’s a better practice that this I’d like to know about it.

As for a graphical representation of what I’m talking about, let me add a few images illustrating.

                                                enter image description here

This first image has a text-less TextView positioned at the center of the RelativeLayout and I’ve given it a width of 0dp. This allows me as you can see in the next image to place anything, relative to that center. My point being that it strikes me as odd that you can’t do that without the need of adding the extra view at the center, for I can see properties such as Layout to left of or Layout to right of but no Layout to center of.

                                                 enter image description here

So the scennario may be a bit more in the lines of, "it’s not broken yet but I’m afraid it will pop at any minute". Then on the other hand if this is the correct way of doing it and I help someone learning a new thing, that also works for me.

Why do I ask questions that don’t get answered!? I’m so setting a bounty here..

The most sensible thing suggested yet (to my judgement) is that I replace my LinearLayout with a View which I would assume takes a little less memory, even if it’s marginally less. So I’m thanking @yorkw for it. I’m surprised that nobody’s asked this before.

Just to clarify since there seems to be a misunderstanding as to what I’m actually after. I don’t want something to take half of the parent width/height. I was asking for a way to use it as a reference point (that’s what I said in the title) so I can do stuff like, position an image to the left of the center without adding a linear layout that takes half and the gravity left or whatever.

Update

Since API 14 we have Space available which is a lightweight View subclass that may be more suitable for this kind of hack.

  • 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-06-04T23:19:35+00:00Added an answer on June 4, 2026 at 11:19 pm

    I like your present solution, but if you want to avoid the funky invisible thin centering widget, you can accomplish that as follows:

    • Calculate containerWidth as the pixel width of your containing RelativeLayout-derived class (call this MyRelativeLayout)
    • Create an instance of MyRelativeLayout.LayoutParams called leftHalfWidgetLayoutParams
    • Set leftHalfWidgetLayoutParams.width = containerWidth / 2, either using the constructor argument when it is created above, or after the fact. After the fact is necessary if you want to adjust it later (e.g., in an override of onSizeChanged() in MyRelativeLayout).
    • Each time you set or change leftHalfWidgetLayoutParams.width, invoke leftHalfWidget.setLayoutParams(leftHalfWidgetLayoutParams).
    • Call requestLayout() if required to redraw MyRelativeLayout each time the width changes

    If the widgets to the right of center are overly large, it might be necessary to set their layout width values explicitly, too, to stop them from encroaching into the left half of the container.

    Now I realize that this loses some of the “magic” of having RelativeLayout do everything automatically, and that’s where your approach is actually nicer. But RelativeLayout is still going to enforce the relative positions of your widgets as well as it can, given the added constraint of the width that will have been added to the layout parameters of your leftHalfWidget, so you wouldn’t be overriding or abandoning its overall contribution in that regard.

    I can’t think of a way to meet your requirements inside a RelativeLayout without explicitly setting the width value on the layout parameters in the manner described above. It’s an alternative approach to yours, but not necessarily a better one.

    I would point out that if your left-half widget is always going to take up the left half of the container, then I can’t see why a horizontal LinearLayout (instead of a RelativeLayout) at the top level (with a weight of 0.5 for your left-size widget with an equally-weighted right side RelativeLayout for the remaining right half), would not be a good solution. The “relative” stuff would, in that case, only be going on within that right half anyway, so why use RelativeLayout at the top level when LinearLayout could do the job more concisely? But perhaps you want to use RelativeLayout on the left side to deal with vertical relationships, and you therefore might actually have more than one control within that left side.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.