layout = new LinearLayout(this);
addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Button btn = new Button(this);
btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
btn.setText("button");
layout.addView(btn);
Button btn1 = new Button(this);
btn1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
btn1.setText("button");
layout.addView(btn1);
I know I must be missing something because whenever I add views to this layout I only have one of them show up.
Enjoy buddy
layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
Reome this addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
You have not set Oreientation , Default is Horizontal, and you have given width LayoutParams.FILL_PARENT
layout = new LinearLayout(this);
layout .setOrientation(LinearLayout.VERTICAL); // orientation vertical try this
addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Button btn = new Button(this);
btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
btn.setText("button");
layout.addView(btn);
Button btn1 = new Button(this);
btn1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
btn1.setText("button");
layout.addView(btn1);
orientation of your linear layout must be horizontal. change it to vertical
Related
Two LinearLayout linearfirstRow and linearsecondRow both has four buttons each.First linear layout works and second layout is not displayed.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
linearfirstRow=new LinearLayout(this);
linearfirstRow.setOrientation(LinearLayout.HORIZONTAL);
linearfirstRow.setId(1);
LinearLayout.LayoutParams Layoutparam1 = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,25);
linearfirstRow.setLayoutParams(Layoutparam1);
setContentView(linearfirstRow);
linearsecondRow=new LinearLayout(this);
linearsecondRow.setOrientation(LinearLayout.HORIZONTAL);
linearsecondRow.setId(2);
linearsecondRow.setLayoutParams(Layoutparam1);
btn_1=new Button(this);
btn_1.setLayoutParams(Layoutparam1);
btn_2=new Button(this);
btn_2.setLayoutParams(Layoutparam1);
btn_3=new Button(this);
btn_3.setLayoutParams(Layoutparam1);
btn_4=new Button(this);
btn_4.setLayoutParams(Layoutparam1);
btn_5=new Button(this);
btn_5.setLayoutParams(Layoutparam1);
btn_6=new Button(this);
btn_6.setLayoutParams(Layoutparam1);
btn_7=new Button(this);
btn_7.setLayoutParams(Layoutparam1);
btn_8=new Button(this);
btn_8.setLayoutParams(Layoutparam1);
linearfirstRow.addView(btn_1);
linearfirstRow.addView(btn_2);
linearfirstRow.addView(btn_3);
linearfirstRow.addView(btn_4);
buttons are --> linearsecondRow.addView(btn_5);
notdisplayed linearsecondRow.addView(btn_6);
linearsecondRow.addView(btn_7);
linearsecondRow.addView(btn_8);
linearfirstrow is displaying because of this setContentView(linearfirstRow);
for the linearsecondRow to display add this at the last part of onCreate()
addView(linearsecondRow);
I want to add linearlayout in table row and that linear layout will hold some buttons and textview.How I can achieve that.I am using this but it does not display anything
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
final CheckBox checkbox = new CheckBox(this);
checkbox.setPadding(10, 5, 0, 0);
checkbox.setTextSize(TypedValue.COMPLEX_UNIT_PX, 15);
checkbox.setOnClickListener(new View.OnClickListener() {
TextView tv = new TextView(this);
tv.setText("Hello");
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 14);
tv.setPadding(0, 0, 0, 0);
tv.setOnClickListener(new View.OnClickListener() {
checkbox.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tv.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout = new LinearLayout(tr.getContext());
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(checkbox, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(tv, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tr.addView(linearLayout);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Please Help me.
Try adding row params like this
TableRow row = new TableRow(this);
row.addView(linearLayout, new TableRow.LayoutParams(1));
Add relativeLayout instead of linearlayout and set params like this for textview
RelativeLayout.LayoutParams rel_lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_lp.addRule(RelativeLayout.BELOW);
tv.setLayoutParams(rel_lp);
For a start you need to close your curly braces at right place. While Setting onClickListener, you are not closing the curly brace and not even putting a semicolon there. So it seems you are making a textView in the listener. The following line
checkbox.setOnClickListener(new View.OnClickListener() {
should be like this
checkbox.setOnClickListener(new View.OnClickListener() {
protected void onClick(View v){
//your listener code here.
}
});
And when the layout is so complex, you should use XML files instead of Dynamically coding, unless you have to.
I declared a RelativeLayout in a xml layout file. Now I want to add Views from code to the existing Layout. I added a Button dynamically to this existing layout as below through code:
rLayout = (RelativeLayout)findViewById(R.id.rlayout);
LayoutParams lprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
Button tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lprams);
tv1.setId(1);
rLayout.addView(tv1);
Now I need to add another Button to the right of the already added Button. I am not able to find the way in which I can add the new one to the right of the previously added button.
Add the rule RelativeLayout.RIGHT_OF for the second added Button LayoutParams:
// first Button
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.rlayout);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lprams);
tv1.setId(1);
rLayout.addView(tv1);
// second Button
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv2 = new Button(this);
tv1.setText("Hello2");
newParams.addRule(RelativeLayout.RIGHT_OF, 1);
tv2.setLayoutParams(newParams);
tv2.setId(2);
rLayout.addView(tv2);
may be this can help you, try it.
rLayout = (RelativeLayout)findViewById(R.id.rlayout);
LayoutParams lprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TableLayout tl=new TableLayout(this);
rLayout.addView(tl);
TableRow tr1=new TableRow(this);
tl.addView(tr1);
Button btn1 = new Button(this);
btn1.setText("Hello");
btn1.setLayoutParams(lprams);
btn1.setId(1);
tr1.addView(btn1);
TextView tv1 = new TextView(this);
tv1.setWidth(40);
tv1.setHeight(LayoutParams.WRAP_CONTENT);
tr1.addView(tv1);
Button btn2 = new Button(this);
btn2.setText("World");
btn2.setLayoutParams(lprams);
btn2.setId(2);
tr1.addView(btn2);
Create another button:
Button tv2 = new Button(this);
tv2.setText("World");
tv2.setLayoutParams(lprams);
tv2.setId(2);
Add add it into your RelativeLayout:
rLayout.addView(tv2);
I've added a footer to my ListView like so:
...{
listView.addFooterView(getButtonFooter(context));
...}
private View getButtonFooter(Context context) {
Button button = new Button(context);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.add_reminder_btn_selector);
button.setId(ADD_REMINDER);
button.setOnClickListener(this);
return button;
}
I'm trying to figure out how I can now center the button horizontally.
EDIT: oh, maybe like this? button.setGravity(Gravity.CENTER_HORIZONTAL);
EDIT: lol, nope. That didn't seem to work. Guess that just center's the text inside the button...
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
layout.setGravity(Gravity.CENTER_HORIZONTAL);
Button button = new Button(this);
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
button.setBackgroundResource(R.drawable.add_reminder_btn_selector);
button.setId(ADD_REMINDER);
button.setOnClickListener(this);
layout.addView(button);
return layout;
Here is my code
Button myButton = new Button(this);
myButton.setText("Press Me");
myButton.setTextColor(Color.WHITE);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
layout.addView(myButton);
How do I add wrapcontent to this button?
Use this line (I used FILL_PARENT to demonstrate)
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
set the wrap content for button
myButtonsetLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
or for layout
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
Kotlin
val button = Button(context)
button.layoutParams = ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)