Android LinearLayout programmatically one column left, one column float right - android

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 !

Related

How create LinearLayout dynamically with aligned elements

I'm very new at android. Need to dynamically create LinearLayout inside RadioGroup with aligned TextViews as following. Here I just moved elements from palette and haven't changed any properties
But get this
What am I missing?
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, /* or MATCH_PARENT */
LinearLayout.LayoutParams.WRAP_CONTENT, /* or MATCH_PARENT */
1.0f);
final LinearLayout linearLayout = new LinearLayout(this);
final TextView text1 = new TextView(this);
text1.setText("aaa");
text1.setLayoutParams(param);
linearLayout.addView(text1);
final TextView text2 = new TextView(this);
text2.setText("bbb");
text2.setLayoutParams(param);
linearLayout.addView(text2);
final TextView text3 = new TextView(this);
text3.setText("ccc");
text3.setLayoutParams(param);
linearLayout.addView(text3);
radioGroup.addView(linearLayout);
LayoutParams should be both MATCH_PARENT and linearLayout should also be set to that params

Adding Buttons below TextViews dynamically

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

The TextView doesn't fit in the center

I dynamically declared some horizontal layouts witch contains some elements like ImageButtons, Buttons, and TextViews, these elements centrally oriented, all of the elements are oriented perfectly but the TextViews. I tried both declaring them directly like the rest of the elements and declaring them each inside a vertical layout but both didn't work:
that's my code:
// the layout params for the Horizontal Layout
LinearLayout.LayoutParams lp_icon = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
0, 1);
lp_icon.setMargins(10, 15, 5, 0);
// the layout params for the elements that contained in the horizontal LinearLayout
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER);
// the layout params for the TextViews
LinearLayout.LayoutParams tests = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
// the layout params for the vertical layouts that contaion TextViews
LinearLayout.LayoutParams temp_lay = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
temp_lay.weight = 1;
// the elements declaration
icon1.setLayoutParams(lp_icon);// icon1 is then horizontal LinearLayout
icon1.setBackgroundResource(R.drawable.ac_overlay);
icon1.setOrientation(LinearLayout.HORIZONTAL);
icon1.setTag(NORMAL);
// TextView contained in a vertical Layout
LinearLayout lay = new LinearLayout(this);
icon1.addView(lay);
lay.setLayoutParams(temp_lay);
lay.setBackgroundColor(Color.TRANSPARENT);
lay.setOrientation(LinearLayout.VERTICAL);
TextView text1 = new TextView(this);
lay.addView(text1);
text1.setLayoutParams(tests);
text1.setText("Master Bedroom");
text1.setTextSize(12);
text1.setTextColor(Color.WHITE);
// other elements
ImageButton image = new ImageButton(this);
icon1.addView(image);
image.setLayoutParams(lp_ineer_ver);
image.setImageResource(R.drawable.grpbuttonfocus6);
image.setBackgroundColor(Color.TRANSPARENT);
Button testbut = new Button(this);
icon1.addView(testbut);
testbut.setLayoutParams(lp_ineer_ver);
testbut.setText(" 8");
testbut.setTextSize(12);
testbut.setBackgroundColor(Color.TRANSPARENT);
testbut.setTextColor(Color.WHITE);
ImageButton testcol = new ImageButton(this);
icon1.addView(testcol);
testcol.setLayoutParams(lp_ineer_ver);
testcol.setImageResource(R.drawable.home_cool);
testcol.setBackgroundColor(Color.TRANSPARENT);
// the TextView that's declared directly in the Horizontal Layout
TextView text2 = new TextView(this);
icon1.addView(text2);
text2.setLayoutParams(lp_ineer_ver);
text2.setText("00");
text2.setTextSize(12);
Try this..
Juse two changes in your code like below for first textview icon1.addView(text1); and the last textview text2.setLayoutParams(tests);
First
TextView text1 = new TextView(this);
icon1.addView(text1); //correction here
text1.setLayoutParams(tests);
text1.setText("Master Bedroom");
text1.setTextSize(12);
and Second
TextView text2 = new TextView(this);
icon1.addView(text2);
text2.setLayoutParams(tests); //correction here
text2.setText("00");
text2.setTextSize(12);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
and you can have Try adding the following code for applying the layout params to the TextView
LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(LinearLayout.CENTER_IN_PARENT);
textView.setLayoutParams(lp);
in XML file :
android:gravity = "center"

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

How to maximize the width of middle cell of linear layout in android?

I have a linear layout that's horizontal. It has three textviews in it. How can I set it so that the left and right widths are wrapping contents, but the center cell width is fully maximized, and the textview is left aligned in it? I want to create this in only java.
This is what I have so far, but the middle cell is not being maximized...
Does anyone know how to fix this?
Thanks
LinearLayout titleLL = new LinearLayout(this);
titleLL.setOrientation(LinearLayout.HORIZONTAL);
TextView num = new TextView(this);
num.setText(String.format("%d", index+1));
num.setPadding(5, 5, 5, 5);
num.setTextAppearance(this, R.style.title_numbering);
num.setBackgroundResource(R.drawable.title_numbering_background);
TextView name = new TextView(this);
name.setText(task.name);
name.setPadding(10, 0, 0, 10);
name.setTextAppearance(this, R.style.title);
TextView edit = new TextView(this);
edit.setText("Edit");
titleLL.addView(num);
titleLL.addView(name);
titleLL.addView(edit);
Use weight when you add your middle textview:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
titleLL.addView(name, params);

Categories

Resources