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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:10:49+00:00 2026-06-18T11:10:49+00:00

I’m very new to android and trying to place the elements horizontally using android

  • 0

I’m very new to android and trying to place the elements horizontally using android apis without xml.

What I’m trying to do is to place RadioButtons and EditText horizontally. Something like:

R-----E
R-----E

I tried code like this:

RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
for(Map.Entry<String,String> entry : storedCards.entrySet())
{
    RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
    EditText ed = new EditText(this);
    RadioButton rb  = new RadioButton(this);
    rb.setId(counter++);

    lp2.addRule(RelativeLayout.RIGHT_OF,rb.getId());
    ed.setLayoutParams(lp2);

    rg.addView(rb); //the RadioButtons are added to the radioGroup instead of the layout
    rb.setText(entry.getKey());
    relativeLayout.addView(ed);
}

This doesn’t work. But this is what I’m trying to do : First I’m setting the id for each radiobutton using the counter variable and trying to set edittext view on the right hand site of that radio button using:

lp2.addRule(RelativeLayout.RIGHT_OF,rb.getId());

But I’m not getting proper result. I get only like this:

ER
R

all EditText are being overlapped. Where I’m making the mistake?

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-18T11:10:50+00:00Added an answer on June 18, 2026 at 11:10 am

    You cannot expect RelativeLayout to place views relative to other views that it does not contain. The RelativeLayout does not understand the ids of the RadioButtons because they haven’t been added to it. Consequently, a RadioButton added to any other layout except for RadioGroup (which is just a LinearLayout) will not have the mutual exclusion logic you are probably looking for when the user taps them to be checked.

    So you must lay out your RadioButton items in a vertical RadioGroup, and the simplest way to have your EditText items line up next to them is to create a second LinearLayout next to it, and use fixed height to ensure each “row” matches. Something like:

    LinearLayout root = new LinearLayout(this);
    root.setOrientation(LinearLayout.HORIZONTAL);
    
    RadioGroup buttonGroup = new RadioGroup(this);
    buttonGroup.setOrientation(RadioGroup.VERTICAL);
    
    LinearLayout editLayout = new LinearLayout(this);
    editLayout.setOrientation(LinearLayout.VERTICAL);
    
    //Add left/right pane to root layout
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
    root.addView(buttonGroup, lp);
    
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
    root.addView(editLayout, lp);
    
    //Fixed height for each row item (45dp in this case)
    //Getting DP values in Java code is really ugly, which is why we use XML for this stuff
    int rowHeightDp = (int)TypedValue.applyDimension(COMPLEX_UNIT_DIP, 45.0f, getResources.getDisplayMetrics());
    
    for(Map.Entry<String,String> entry : storedCards.entrySet())
    {
    
        EditText ed = new EditText(this);
        RadioButton rb  = new RadioButton(this);
        rb.setId(counter++);
        rb.setText(entry.getKey());
    
        //Add each item with its LayoutParams
        LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            rowHeightDp) );
        buttonGroup.addView(rb, lp1);
    
        lp1 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            rowHeightDp) );
        editLayout.addView(ed, lp1);
    }
    

    This creates two side-by-side layouts that stay lined up due to the fixed item height. You can adjust the item height in the LayoutParams used for each row. You can see how doing layouts in pure Java is extremely verbose, which is the reason the preferred method is XML.

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

Sidebar

Related Questions

I am using JSon response to parse title,date content and thumbnail images and place
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters

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.