Adding Buttons below TextViews dynamically - android

I have the following:
Button[] buttons = new Button[forSale.size()];
TextView[] textViews = new TextView[forSale.size()];
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < forSale.size(); i++) {
rl = (RelativeLayout)findViewById(R.id.marketView);
textViews[i] = new TextView(this);
textViews[i].setText("\n" + forSale.get(i).getTradeGoodType()
+ "\t$"
+ forSale.get(i).getTradeGoodType().getBasePrice() +
"\n");
textViews[i].setId(i);
goodsForSaleText.append(textViews[i].getText());
buttons[i] = new Button(getApplicationContext());
buttons[i].setText("BUY");
// add the rule that places your button below your TextView object
params.addRule(RelativeLayout.BELOW, textViews[i].getId());
buttons[i].setLayoutParams(params);
rl.addView(buttons[i]);
}
My problem is that my the buttons are being added on top of each other at the top of the screen rather than one after the other below the TextViews.
Basically, it should look like:
TextView
Button
TextView
Button
TextView
Button
I need to dynamically add buttons for each number of TextViews I have (derived from a list). Can anyone help me out?

Use LinearLayout with vertical orientated instead of RelativeLayout. You will get automatically vertical position of objects

Related

Android LinearLayout programmatically one column left, one column float right

My Android layout consists of 2 TextView inside a LinearLayout. However, I want the second text view to be align right.
Explanation:
// LinearLayout A
LinearLayout linearLayoutA = new LinearLayout(this);
linearLayoutA.setLayoutParams(new LinearLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayoutWorkoutPlansSessionsMain.addView(linearLayoutA);
// Title
TextView textViewExerciseTitle = new TextView(this);
textViewExerciseTitle.setText("Pull ups");
textViewExerciseTitle.setTextSize(16);
textViewExerciseTitle.setTextColor(Color.BLACK);
linearLayoutA.addView(textViewExerciseTitle);
// Reps & sets
TextView textViewSetsReps = new TextView(this);
textViewSetsReps.setText("8 x 4");
textViewSetsReps.setTextSize(16);
textViewSetsReps.setTextColor(Color.BLACK);
textViewSetsReps.setPadding(20,0,0,0);
linearLayoutA.addView(textViewSetsReps);
TextView textViewSetsReps = new TextView(this);
textViewSetsReps.setText("8 x 4");
textViewSetsReps.setTextSize(16);
textViewSetsReps.setTextColor(Color.BLACK);
textViewSetsReps.setPadding(20,0,0,0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
textViewSetsReps.setLayoutParams(params);
textViewSetsReps.setGravity(Gravity.RIGHT);
ll.addView(textViewSetsReps);
you could edit your sencond TextView like this !

ImageButton is not visible when adding to a tablerow dynamically

I am creating a form where the user enters his/her address and when the user saves the form, the flow goes to another activity where the users' name is shown one below the other. I am using TableLayout and in each TableRow I have a TextView which shows the name and an ImageButton beside the textview which can be used to delete the entry. Now when I run the app I can only see the TextView in the layout, the ImageButton is not visible.Can someone please help me where I am going wrong?
Here is part of my code:
if (requestCode == REC_INFO && resultCode == RESULT_OK && data != null) {
RecipientArray = (ArrayList<Person>) data
.getSerializableExtra("RecArray");
TableLayout tbl = new TableLayout(this);
TextView[] tv = new TextView[RecipientArray.size()];
ImageButton delete_btns[] = new ImageButton[RecipientArray.size()];
TableRow tr[] = new TableRow[RecipientArray.size()];
for (int i = 0; i < RecipientArray.size(); i++) {
tv[i] = new TextView(this);
tv[i].setBackgroundResource(R.drawable.fill_rece);
Person p = RecipientArray.get(i);
tv[i].setText(p.getName());
tv[i].setTextColor(Color.WHITE);
delete_btns[i] = new ImageButton(this);
delete_btns[i]
.setImageResource(R.drawable.ipad_postcare_landscape_from);
delete_btns[i].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tr[i] = new TableRow(this);
tr[i].addView(tv[i]);
tr[i].addView(delete_btns[i]);
tbl.addView(tr[i]);
}
recs_layout.addView(tbl);//I add the TableLayout to a RelativeLayout
}
delete_btns[i] = new ImageButton(this);
delete_btns[i].setImageResource(R.drawable.ipad_postcare_landscape_from);
delete_btns[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Drawable d = delete_btns[i].getDrawable();
d.setBounds(0, 0, delete_btns[i].getWidth(), delete_btns[i].getHeight());
I think your textview is taking all the space in the table row try setting layout params for that as well
For ex:
tv[i].setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 2f));
And similarly for image button
delete_btns[i].setLayoutParams(new TableRow.LayoutParams(
0, LayoutParams.WRAP_CONTENT,1f));
And to table row
tr[i].setWeightSum(3f)
Here the third argument of float type is weight of the view as it is set to 1f for both your image view and textview both will occupy equal space you can change it and set it what you need.
While using weight set width to 0dp for both textview and imageview

android dynamically created layouts overlapping each other

I want to create a relative layout inside a linear layout dynamically.This relative layout contains a text view and button which are also created in dynamic method.The alignment of text view and button not work properly.The code which i have tried is given below.
final LinearLayout lab_linear = (LinearLayout) findViewById(R.id.contact_list_layout);
lab_linear.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < Size_contact; i++)
{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(lp);
final TextView Questions_value = new
android.widget.TextView(getApplicationContext());
Questions_value.setText(contact_name.get(i));
Questions_value.setTextSize(18);
Questions_value.setId(i);
layout.addView(Questions_value);
Button myButton = new Button(this);
myButton.setBackgroundResource(R.drawable.close);
myButton.setLayoutParams(new LayoutParams(20,20));
layout.addView(myButton);
lab_linear.addView(layout);
}
Try this..
Give layout parems for RelativeLayout individually to TextView and Button and also add addRule(RelativeLayout.ALIGN_PARENT_RIGHT); or addRule(RelativeLayout.ALIGN_PARENT_LEFT); for that views
Or add the below in for Button
lp1.addRule(RelativeLayout.RIGHT_OF, Questions_value.getId());
for (int i = 0; i < Size_contact; i++)
{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
final TextView Questions_value = new
android.widget.TextView(getApplicationContext());
Questions_value.setText(contact_name.get(i));
Questions_value.setTextSize(18);
Questions_value.setId(i);
layout.addView(Questions_value,lp);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//Or below remove above code and uncomment the below code
//lp1.addRule(RelativeLayout.RIGHT_OF, Questions_value.getId());
Button myButton = new Button(this);
myButton.setBackgroundResource(R.drawable.close);
myButton.setLayoutParams(new LayoutParams(20,20));
layout.addView(myButton,lp1);
lab_linear.addView(layout);
}
Might I suggest using LinearLayouts instead of RelativeLayouts in your loop? That way, you won't have the overlap problem.

android: how to fill a relativelayout ( screen size) with many textviews created dynamically?

I tried to use below code, but the textviews do not change line after it reach the end of screen.
RelativeLayout ll = (RelativeLayout)findViewById(R.id.viewObj);
int lastid=0;
for (int i=0; i<100; i++) {
String teststr = " hello ";
TextView textView2 = new TextView(this);
textView2.setId(i+1);
textView2.setTextSize(30);
textView2.setBackgroundColor(Color.GREEN);
textView2.setTextColor(Color.RED);
textView2.setTypeface(Typeface.MONOSPACE);
textView2.setText(teststr+String.valueOf(i));
if (i>0)
{
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.RIGHT_OF, lastid);
ll.addView(textView2, lay);
} else {
ll.addView(textView2);
};
lastid = textView2.getId();
}
However, I dont know how to determine when to change line. I just keep on putting new textview to the right of the last one created. Can anyone tell me the correct logic to do the task? many thanks.
Easy fix. Switch to a dynamically-created LinearLayout, and set its orientation to vertical:
lay.setOrientation(LinearLayout.VERTICAL);

How to make activity with n buttons same size using RelativeLayout?

I need do design programatically one activity with 6 buttons (same size h and w), and all buttons show a fullsize of activity.
I tried do this: RelativeLayout with buttons and modify for tests.
Show one button!!!
`
setContentView(R.layout.main);
ArrayList<Button> buttons = new ArrayList<Button>();
//RelativeLayout bg = (RelativeLayout) findViewById(R.layout.main);
for (int i = 0; i < 10; i++) {
Button newButton = new Button(this);
newButton.setId(100 + i + 1); // ID of zero will not work
newButton.setText("XXXX");
buttons.add(newButton);
// New layout params for each button
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
// using getId() here in case you change how you assign IDs
int id = buttons.get(i - 1).getId();
lp.addRule(RelativeLayout.RIGHT_OF, id);
}
this.addContentView(newButton, lp);
}`
please look this line if ok: this.addContentView(newButton, lp);
Thanks!!!
mateus
It's a bit not clear from your question whether you want all buttons to distribute evenly across the activity space or each button to take over all activity space?
In first case what you need is a LinearLayout with layout_weight for buttons set to 1.
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.weight = 1;
In the second case I think it's better to use a FrameLayout and just let buttons take over all space by setting
FrameLayout.LayoutParams.FILL_PARENT
to both dimensions.

Categories

Resources