sriram
sriram

Reputation: 9062

Placing elements linearly in android

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.

Upvotes: 0

Views: 64

Answers (1)

devunwired
devunwired

Reputation: 63303

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.

Upvotes: 1

Related Questions