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

  • SEARCH
  • Home
  • 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 9073139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:20:46+00:00 2026-06-16T18:20:46+00:00

I have a class to which I need to add one or more Views.

  • 0

I have a class to which I need to add one or more Views. In this example, a single ImageView.
I can add views without a problem and align them using LayoutParameters, but when I try to align or center them somewhere along the vertical axis, they either stick to the top or don’t appear at all (they are likely just out of view).
In the constructor I call a method fillView(), which happens after all dimensions and such are set.

fillView()

public void fillView(){
    img = new ImageView(context);
    rl = new RelativeLayout(context);

    img.setImageResource(R.drawable.device_access_not_secure);

    rl.addView(img, setCenter());
    this.addView(rl, matchParent());
}

matchParent()

public LayoutParams matchParent(){
    lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    lp.setMargins(0, 0, 0, 0);
    return lp;
}

setCenter()

public LayoutParams setCenter(){
    lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); //This puts the view horizontally at the center, but vertically at the top
    return lp;
}

Similarly, adding rules such as ALIGN_RIGHT or BELOW will work fine, but ALIGN_BOTTOM or CENTER_VERTICALLY will not.

I tried using both this method and the setGravity() a LinearLayout offers, with the same results.

  • 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-16T18:20:47+00:00Added an answer on June 16, 2026 at 6:20 pm

    While I still don’t know why my method worked horizontally, but not vertically, I did solve the problem. The posted methods worked, the problem was hidden in onMeasure().
    I previously set the dimensions by simply passing them to setMeasuredDimension(). I fixed the issue by also passing them to the layoutParams(). I also changed the integers I used to MeasureSpecs while I was at it.

    I changed this:

     @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
            super.onMeasure(this.getT_Width(), this.getT_Heigth());     
            this.setMeasuredDimension(desiredHSpec, desiredWSpec);
        }
    

    to this:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        final int desiredHSpec = MeasureSpec.makeMeasureSpec(this.getT_heigth(), MeasureSpec.EXACTLY);
        final int desiredWSpec = MeasureSpec.makeMeasureSpec(this.getT_width(), MeasureSpec.EXACTLY);
        this.getLayoutParams().height = this.getT_heigth();
        this.getLayoutParams().width = this.getT_width();
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(desiredWSpec);
        int height = MeasureSpec.getSize(desiredHSpec);
        setMeasuredDimension(width, height);
    }
    

    getT_Width() and getT_Heigth() are methods I used to get some custom dimensions I set elsewhere.
    I hope this helps somebody.

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

Sidebar

Related Questions

I have a problem to test my non activity-class which need the context of
I have a business class which I need to serialize to xml. It has
I have a class which is going to need to use the strategy design
I have a form builder class which inherits from AbstractType and I need to
I have a simple class for use on JNI, which i need to export
I have a program in which I need to store a Class object into
I need clarification in calling methodologies, i have a base class and from which
I have a .NET 2.0 class the properties of which are marked virtual.I need
I have xpath page.search(//table[@class='campaign']//table) which returns two tables. I need to choose only first
I have class which have one public method Start , one private method 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.