I'm trying to add a textview and a button to a linear layout programatically.
The button is showing up but the textview isn't.
Here is my code:
LinearLayout main = (LinearLayout) findViewById(R.id.mainlayout);
LinearLayout first = new LinearLayout(this);
LayoutParams fparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 5.0f);
LayoutParams tvparams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
LayoutParams btparams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
first.setLayoutParams(fparams);
first.setOrientation(LinearLayout.HORIZONTAL);
TextView tv = new TextView(this);
tvparams.weight = 3.0f;
tv.setLayoutParams(tvparams);
Button bt = new Button(this);
btparams.weight = 2.0f;
bt.setLayoutParams(btparams);
first.addView(bt);
first.addView(tv);
main.addView(first);
Try setting some text on the TextView. Use it's setText() method
ok try this to set programmatically
LinearLayout myLayout = (LinearLayout)findViewById(R.id.parent);
LayoutParams fparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 5.0f);
LayoutParams tvparams = new LayoutParams(0, LayoutParams.WRAP_CONTENT,3.0f);
LayoutParams btparams = new LayoutParams(0, LayoutParams.WRAP_CONTENT,2.0f);
LinearLayout first = new LinearLayout(this);
first.setLayoutParams(fparams);
first.setOrientation(LinearLayout.HORIZONTAL);
TextView tv = new TextView(this);
tv.setText("asdsad");
tv.setLayoutParams(tvparams);
Button bt = new Button(this);
bt.setLayoutParams(btparams);
first.addView(bt);
first.addView(tv);
myLayout.addView(first);
hope it ll help,working for me correctly.
Related
I was Programmatically Creating Linear Layout and I want to Align two Fields on same Line. But i Want to display the result as like expected.
lView = new LinearLayout(Main2Activity.this);
// lView.setPadding(0,150,0,0);
lView.setBackgroundColor(Color.parseColor("#EDFCFC"));
lView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Button logout = new Button(Main2Activity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
logout.setLayoutParams(params);
logout.setText("Logout");
logout.setBackgroundColor(Color.parseColor("#F53F37"));
lView.addView(logout);
TextView title = new TextView(Main2Activity.this);
title.setText("ASSOCIATES");
title.setTextColor(Color.parseColor("#2BD865"));
title.setTextSize(25);
title.setTypeface(null, Typeface.BOLD);
title.setGravity(Gravity.CENTER);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(title);
TextView title1 = new TextView(Main2Activity.this);
title1.setText("Date :");
title1.setTextSize(20);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(title1);
et1 = new EditText(Main2Activity.this);
et1.setWidth(10);
et1.setGravity(Gravity.RIGHT);
et1.setHint("Date");
//lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(et1);
My Actual Output is
Expected
You need to set gravity for your parent LinearLayout to align childs.
set the Linear Layout orientation to horizontal like this:
lView.setOrientation(LinearLayout.HORIZONTAL);
Add LayoutParams to your textview and edittext and you will get the specified view.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER;
TextView title1 = new TextView(Main2Activity.this);
title1.setLayoutParams(params);
title1.setText("Date :");
title1.setTextSize(20);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(title1);
params.gravity = Gravity.LEFT;
et1 = new EditText(Main2Activity.this);
et1.setLayoutParams(params);
et1.setWidth(10);
et1.setGravity(Gravity.RIGHT);
et1.setHint("Date");
lView.setOrientation(LinearLayout.HORIZONTAL);
lView.addView(et1);
I want to design an UI with 3 elements, one TextView tvStart, one SeekBar sbProgress and one TextView tvEnd.
I want tvStart aligned left and tvEnd aligned right, with sbProgress at the center.
And I want to do everything programmatically!
I have this code:
public void MyLayout() {
LinearLayout layout = new LinearLayout(this);
TextView tvStart = new TextView(this);
SeekBar sbProgress = new SeekBar(this);
TextView tvEnd = new TextView(this);
tvStart.setText("0");
tvEnd.setText("100");
sbProgress.setMax(100);
layout.addView(tvStart);
layout.addView(sbProgress);
layout.addView(tvEnd);
setContentView(layout);
}
But it seems the 3 elements are aligned from left to right and the SeekBar is very short.
Is it possible to do such tings with LinearLayout or do I need to use RelativeLayout?
public void MyLayout() {
LinearLayout layout = new LinearLayout(this);
TextView tvStart = new TextView(this);
SeekBar sbProgress = new SeekBar(this);
TextView tvEnd = new TextView(this);
tvStart.setText("0");
tvEnd.setText("100");
sbProgress.setMax(100);
LinearLayout.LayoutParams startParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams sbParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
sbParams.weight = 1;
LinearLayout.LayoutParams endParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.addView(tvStart, tvParams);
layout.addView(sbProgress, sbParams);
layout.addView(tvEnd, endParams);
setContentView(layout);
}
I am adding TextView dynamically using for loop to LinearLayout which orientation is horizontal. Is it possible to align the TextView to second line if it reach end of screen width?
You can do it in the following manner:
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
LinearLayout layout1 = new LinearLayout(this);
LinearLayout layout2 = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout1.setOrientation(LinearLayout.HORIZONTAL);
layout2.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 3; i++) {
TextView lblView = new TextView(this);
TextView lblView2 = new TextView(this);
lblView.setLayoutParams(params);
lblView.setText("hello");
lblView.setEms(5);
lblView.setGravity(Gravity.CENTER);
lblView2.setLayoutParams(params);
lblView2.setText("hello");
lblView2.setEms(5);
lblView2.setGravity(Gravity.CENTER);
layout1.addView(lblView);
layout2.addView(lblView2);
}
layout.addView(layout1);
layout.addView(layout2);
setContentView(layout);
The following will do the trick,
textview.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
Every time I set the layout param of TextView to wrap content or fill parent it's not showing in the view, but when I set it to pixels it's working why is that?
Here is the relevant code:
items = (LinearLayout) findViewById(R.id.llmain);
tb = new TableLayout(MainActivity.this);
tb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tb.setGravity(Gravity.CENTER);
TableRow tr = new TableRow(MainActivity.this);
ImageButton ima = new ImageButton(MainActivity.this);
ImageButton imr = new ImageButton(MainActivity.this);
TextView tvin = new TextView(this);
TableRow.LayoutParams trp = new TableRow.LayoutParams();
trp.setMargins(0, 0, 0, 10);
tr.setLayoutParams(trp);
tr.setGravity(Gravity.CENTER);
ima.setBackgroundColor(Color.TRANSPARENT);
ima.setImageResource(R.drawable.add);
imr.setBackgroundColor(Color.TRANSPARENT);
imr.setImageResource(R.drawable.remove);
tvin.setWidth(100);
tvin.setHeight(45);
tvin.setGravity(Gravity.CENTER);
tvin.setText(sil[i]);
tvin.setBackgroundColor(Color.rgb(200, 0, 255));
tr.addView(ima);
tr.addView(tvin);
tr.addView(imr);
tb.addView(tr);
items.addView(tb);
I tried using this
tvin.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
and this
tvin.setWidth(LayoutParams.FILL_PARENT);
tvin.setHeight(LayoutParams.FILL_PARENT);
but none of these work.
Try:
tvin.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
and remove the tvin.setWidth and height.
You should always set the layout params of view's parent, TableRow in this case.
MATCH_PARENT makes no difference but it's a new name and you should be using it instead of FILL_PARENT.
In this Line u have to use height and width .. TableRow.LayoutParams trp = new TableRow.LayoutParams(); Like this
TableRow row = new TableRow(context);
row.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT));
Try to use the LinearLayout.LayoutParams
TextView tvin = new TextView(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
tvin.setLayoutParams(llp);
I want that button(child) will have 25% of full width of horizontal LinearLayout (its parent). The code:
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
ll.setWeightSum (1.0f);
Button b = new Button (this);
ll.addView(b);
How can I do it? Thanks.
Try this:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENTS,LayoutParams.FILL_PARENT));
lp.weight = 0.25;
b.setLayoutParams(lp);
You can pass it in as part of the LinearLayout.LayoutParams constructor:
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);