Reputation: 1010
Brand new to Android dev, and I'm trying to figure out whats broken with a small piece of code.
I have a Relative Layout (which I believe suits my needs the best). I have two buttons together on a row that I specified in the activity_main.xml file. When I press one of the buttons I'd like to have a EditText field and button repeatedly appear on the same row, below the last set of items. Kinda like this:
BUTTON BUTTON
|__| BUTTON
My function adds the EditText where I expect it on the first tap however I end up with a row of 3 buttons like this:
BUTTON BUTTON BUTTON
|__|
Code is below, am I missing something about Relative Layouts? Am I accessing elements in a horribly botched way even?
//Get main layout as defined in activity_main.xml
final RelativeLayout rl = (RelativeLayout)this.findViewById(R.id.rootLayout);
//Setup rules
final RelativeLayout.LayoutParams below = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final RelativeLayout.LayoutParams right = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
newTimer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
below.addRule(RelativeLayout.BELOW,rl.getChildAt(rl.getChildCount()-1).getId() );
EditText timerName = new EditText(MainActivity.this);
Button btnAddNewTimer = new Button(MainActivity.this);
timerName.setText("New Timer");
timerName.setVisibility(View.VISIBLE);
timerName.setLayoutParams(below);
rl.addView(timerName);
right.addRule(RelativeLayout.RIGHT_OF,rl.getChildAt(rl.getChildCount()-1).getId());
btnAddNewTimer.setText("Add");
btnAddNewTimer.setVisibility(View.VISIBLE);
btnAddNewTimer.setLayoutParams(right);
rl.addView(btnAddNewTimer);
}
});
Upvotes: 2
Views: 91
Reputation: 11073
I personally don't trust the getChildCount()-1).getId() to get the last added view.
Also, you need to specify a layout parameter on where you want your new item to be vertically. So your you need to add a rule to right that will line up its top with the button.
I'd recommend that you get the id of your added view
timerName.setText("New Timer");
timerName.setVisibility(View.VISIBLE);
timerName.setLayoutParams(below);
timerName.setId(39);//or some other unique id in this range
rl.addView(timerName);
right.addRule(RelativeLayout.RIGHT_OF, timerName.getId());
right.addRUle(RelativeLayout.ALIGN_TOP,timerName.getId());
btnAddNewTimer.setText("Add");
btnAddNewTimer.setVisibility(View.VISIBLE);
btnAddNewTimer.setLayoutParams(right);
Upvotes: 1
Reputation: 5246
Think for some reason, the right.addRule
is not counting the new child timerName
added to the view.
As we know the btnAddNewTimer
needs to be placed to the right of the timerName
edittext, try passing in the timerName id
to the right.addRule
.
So instead of..
right.addRule(RelativeLayout.RIGHT_OF,rl.getChildAt(rl.getChildCount()-1).getId());
Try...
right.addRule(RelativeLayout.RIGHT_OF, timerName.getId());
Hope this helps.
Upvotes: 1