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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:04:15+00:00 2026-05-20T15:04:15+00:00

I’ve been trying to center this custom view I have inside this other custom

  • 0

I’ve been trying to center this custom view I have inside this other custom View that extends LinearLayout. This all needs to be done via code because it’s all done at runtime.

I’ve tried the standard approach with setting the gravity:

this.setGravity(Gravity.CENTER);

That was done inside of my class that extends LinearLayout.

I’ve also tried the LayoutParams method:

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER;
    block.setLayoutParams(params);
    this.addView(block);

This was done in the place where I add the block to the view (as you can see).

Both methods resulted in the block still being aligned in the upper-left hand corner of my view. What are my other options or, more importantly, what in the world am I doing wrong?

  • 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-20T15:04:15+00:00Added an answer on May 20, 2026 at 3:04 pm

    Try also setting the layout weight:

    LinearLayout.LayoutParams params =
        new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER_HORIZONTAL;
    params.weight = 1;
    this.addView(block, params);
    

    This (assuming your orientation is vertical) will allow your view to fill the remaining space in the linear layout and should center it horizontally.

    I don’t think there is a way to have a view child that is smaller than the LinearLayout cell that contains it. If the view is smaller than its “cell” then the LinearLayout will shrink to accomodate it. If the layout weight causes the “cell” to grow, then the contained view will grow as well.

    If you really want the view to be smaller than its “cell” and centered, wrap it in a FrameLayout. Then you will be able to use a center gravity to your heart’s content.
    layout_width=”fill_parent”
    In xml (I know you can’t use this directly, but it’s easier to explain this way):

    <LinearLayout orientation="vertical">
        <FrameLayout layout_weight="1" layout_height="wrap_content">
            <View layout_gravity="center" layout_{width,height}="wrap_content"/>
        </FrameLayout>
            ... other views ...
    </LinearLayout>
    

    Unmarked layout attributes are “fill_parent”.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker

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.