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)
Related
I am adding a dynamic layout view which looks like this.
I am appending this view on button click. Here is the code for this view on button click
public void addDynamicView() {
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
LinearLayout lHori = new LinearLayout(this);
lHori.setOrientation(LinearLayout.HORIZONTAL);
//lHori.setWeightSum(3);
lHori.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout lVertical = new LinearLayout(this);
lVertical.setOrientation(LinearLayout.VERTICAL);
//lVertical.setWeightSum(2);
lVertical.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Button button = new Button(this);
button.setLayoutParams(new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)));
button.setVisibility(View.VISIBLE);
button.setText("button");
TextView txtEmpName = new TextView(this);
txtEmpName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
txtEmpName.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
txtEmpName.setText("txt1");
TextView txtEmpDropTime = new TextView(this);
txtEmpDropTime.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
txtEmpDropTime.setText("txt2");
lVertical.addView(txtEmpName);
lVertical.addView(txtEmpDropTime);
lHori.addView(lVertical);
lHori.addView(button);
relativeLayout.addView(lHori);
linear.addView(relativeLayout);
}
The problem is I want all the buttons to be placed below the click button.
But I get output like this
How can I place all Button with text Button below the Click button?
If you two do with xml than it's so simple just make a layout which you want after click on click button after that set visibility gone for all layout accept click button next in Java on button click you can call visibility view to make that layout ...
Hope so it will works for you..
You can create a LinearLayout lButton:
LinearLayout lButton = new LinearLayout(this);
lButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
then set its gravity as Gravity.END
lButton.setGravity(Gravity.END)
and add the button into it
lButton.addView(button);
lHori.addView(lVertical);
lHori.addView(lButton);
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
I am creating button on runtime like below...
Button newCategoryButton = new Button(this);
newCategoryButton.setText(catName);
newCategoryButton.setWidth(30);
newCategoryButton.setBackgroundResource(R.drawable.bnt);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(newCategoryButton, lp);
But my problem is that it disappear when I back from that activity.
I want this permanent. And another problem is that how to set this button below on other button??
LinearLayout.LayoutParams layoutParams;
LinearLayout ll;
Button newCategoryButton=new Button(this)
newCategoryButton.setText("Push Me");
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ll.addView(view, newCategoryButton);
You should put this code in the onResume(), not on the onCreate() method ;-)
If you want to use the button under another one, you will need to use a LinearLayout with a vertical orientation!
LinearLayout myll = (LinearLayout) findViewById(R.id.yourLinearLayout);
myll.setLayoutParams(LinearLayout.WRAP_CONTENT, LinearLayout.WRAP_CONTENT);
myll.setOrientation(LinearLayout.VERTICAL);
myll.addView(view, Button1);
myll.addView(view, Button2);
setContentView(myll)
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;
i want to add some buttons dynamically at some positions,can anyone suggest a sample code for this??
That's how you can add a button to a group at runtime:
final ViewGroup group = (ViewGroup)findViewById(R.id.some_group);
final Button button = new Button(context);
button.setText("Some text");
group.addView(button,
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setContentView(R.layout.medplan_index);
Take Orientation of linear Layout vertical.
LinearLayout linearLayout = (LinearLayout ) findViewById(R.id.linearLayout);
Button btn = new Button (this);
btn.setText("button name");
btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
linearLayout.addView(btn);