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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:53:31+00:00 2026-05-25T18:53:31+00:00

I have to do this programmatically. So, bear with me. I have text and

  • 0

I have to do this programmatically. So, bear with me.

I have text and two buttons that I set to align below each other. So I created a new RelativeLayout.LayoutParams instance for each and added a rule to set it below one another. That works and I’m happy with it.

My question is: why do I have to create a new instance in order for the layout to display them this way? Is there any way to reduce the following code so that it’s not so repetitive (besides writing my own private method to dry it up. I’m wondering whether there’s a way to accomplish the same thing in fewer lines of code utilizing something I’ve possibly overlooked in the Android SDK)? Do I have to keep creating a new RelativeLayout.LayoutParams instance for each element?

layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

text = new TextView(this);
text.setId(1);
text.setText("This is text");
layout.addView(text);

myButton1 = new Button(this);
myButton1.setId(2)
myButton1.setOnClickListener(this);
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.BELOW, text.getId());
layout.addView(myButton1, buttonParams);

myButton2 = new Button(this);
myButton2.setId(3)
myButton2.setOnClickListener(this);
buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.BELOW, myButton1.getId());
layout.addView(myButton2, buttonParams);
  • 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-25T18:53:32+00:00Added an answer on May 25, 2026 at 6:53 pm

    Unfortunately, there is no removeRule() method for RelativeLayout.LayoutParams (at least according to the API) so you have to create a new LayoutParams object each time.

    I would tend to agree with @jeffamaphone that you should prefer xml layouts over programmatically setting the layout. Even if you don’t put the entire layout in an xml, you should use the resource id (see doc for details) to set the id’s for the items. This will guarantee a unique id.

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

Sidebar

Related Questions

I am trying to send email programmatically... I have this line of code: _mail.AddAttachmentData(nsd,text/plain,
I have this iPad application with different NIBs and views. Each view has two
I need to create a Firebird Database programmatically using DBExpress. I have done this
I have added a UILabel to my view programmatically like this: myLabel = [[UILabel
I'm working on configuring a RollingFileAppender in log4net programmatically. I have something like this
Would you call this implementation of a multiton in objective-c 'elegant'? I have programmatically
I have this code in jQuery, that I want to reimplement with the prototype
I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the
I have this function that creates a unique number for hard-disk and CPU combination.
I have a String[] that looks like this where the first String is aways

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.