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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:34:00+00:00 2026-06-18T07:34:00+00:00

I have created 2 buttons in a relative layout programmatically. I want no margin

  • 0

I have created 2 buttons in a relative layout programmatically. I want no margin space between the buttons and tried to achieve this by using ‘setmargin’ but failed. Below is the code.

//creating the relative layout
  RelativeLayout relativeLayout = new RelativeLayout(this);
  RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);   
//creating buttons
    Button button1 = new Button(this);
    button1.setId(R.id.button1);
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
            SCR_W/2,
            RelativeLayout.LayoutParams.WRAP_CONTENT);//SCR_W is the device screenwidth
    params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    params1.setMargins(0, 0, 0, 0);
    button1.setLayoutParams(params1);
    relativeLayout.addView(button1);  

    Button button2 = new Button(this);
    button2.setId(R.id.button2);
    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
            SCR_W/2,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params2.addRule(RelativeLayout.BELOW, button1.getId());
    params2.setMargins(0, 0, 0, 0);
    button2.setLayoutParams(params2);
    relativeLayout.addView(button2);

//setting the relative layout as the contentview
     setContentView(relativeLayout, rlp);

Thanks in advance..

  • 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-18T07:34:01+00:00Added an answer on June 18, 2026 at 7:34 am

    I tried your code, and works ok to me,

    The two buttons are at distance 0. To check it, simply set different color to each button, and you will see that they are together.

    button1.setBackgroundColor(Color.BLUE);
    button2.setBackgroundColor(Color.RED);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created sliding drawer for my relative layout which includes some buttons. But
In my application I have created a Relative layout containing 5 buttons and now
I have created an alert view with two buttons using the following code: UIAlertView
I'm using Relative Layout to create a calculator screen, and i have a row
I have two cases with Linear (also tried Relative) layout in android. The one
I have a RelativeLayout completely created within java, not using xml layout. I have
I have created a layout for an activity. The layout contains some image buttons
I have created 4 buttons via Interface Builder. I have an array which has
I am trying to create a rich text editor. I have created some buttons
I have dynamically created the buttons in a div. And binding the click 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.