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));
Related
I want to make a layout that it
LinearLayout
ScrollView
LinearLayout
but it seems like the ScrollView has cover the rest of LinearLayout.
LinearLayout all = new LinearLayout(this);
LinearLayout upper = new LinearLayout(this);
LinearLayout footer = new LinearLayout(this);
ScrollView sv = new ScrollView(this);
all.setOrientation(LinearLayout.VERTICAL);
upper.setOrientation(LinearLayout.HORIZONTAL);
footer.setOrientation(LinearLayout.HORIZONTAL);
TextView code = new TextView(this);
code.setText("Code : ");
EditText codeEdit = new EditText(this);
upper.addView(code);
upper.addView(codeEdit);
LinkedList<LinearLayout> lk = new LinkedList<LinearLayout>();
TextView[] tt = new TextView[100];
LinearLayout ll= new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
for(int a = 0 ; a < 100; a++){
LinearLayout temp = new LinearLayout(this);
temp.setOrientation(temp.HORIZONTAL);
tt[a] = new TextView(this);
tt[a].setText("Hello " + a);
temp.addView(tt[a]);
lk.add(temp);
ll.addView(temp);
}
sv.addView(ll);
Button next = new Button(this);
next.setText("Next");
footer.addView(next);
all.addView(upper);
all.addView(sv);
all.addView(footer);
setContentView(all);
The scrollview is too large and cover the rest of the linearlayout, the linearlayout after is not showing becasue of the scrollview.
How can I actually solve this?
Do like this
LinearLayout dummy = new LinearLayout(this);
dummy.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
dummy.addView(sv);
add one linearlayout to wrap the scrollview inside it
instead of adding scroolview add this dummy linearlayout
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.
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"
Here is my code to display table layout below linear layout.here i am setting scroll view as contentview.but only table layout displaying on screen.here any problem in layout.please help me thanks.
ScrollView scrollView = new ScrollView(this);
LinearLayout ll = new LinearLayout(getApplicationContext());
ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
Button btn1 = new Button(this);
btn1.setText("Button1");
ll.addView(btn1);
btn1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TableLayout resultLayout = new TableLayout(this);
resultLayout.setStretchAllColumns(true);
resultLayout.setShrinkAllColumns(true);
Here tried by placing the linear layout above the table layout now only linearlayout displaying
LinearLayout ll = new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Set the TextView for the Question
TextView tv1 = new TextView(getApplicationContext());
tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv1.setText("HI");
ll.addView(tv1);
ScrollView scrollView = new ScrollView(this);
TableLayout resultLayout = new TableLayout(this);
resultLayout.setStretchAllColumns(true);
resultLayout.setShrinkAllColumns(true);
//Here i have table rows code
ll.addView(scrollView);
setContentView(ll);
now setting linear layout as contentview as per your answers scroll view contains only table layout.but why it is not displaying in the screen
ScrollViews in android should contain only a single child. So might I suggest you add a LinearLayout inside the ScrollView and then add the other components in this LinearLayout
check this:\
LinearLayout ll = new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Set the TextView for the Question
TextView tv1 = new TextView(getApplicationContext());
tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv1.setText("HI");
ll.addView(tv1);
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TableLayout resultLayout = new TableLayout(this);
resultLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
scrollView.addView(resultLayout);
resultLayout.setStretchAllColumns(true);
resultLayout.setShrinkAllColumns(true);
ll.addView(scrollView);
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);