Add dynamically buttons to layout in android - android

I was wondering how can I add dynamically buttons to layout from code. I don;t know how many buttons I will should add. This number will be random. I want to add them in this way:
Any ideas how can I do that dynamically in this way?

You can do it this way to add button:
Button myButton = new Button(this);
myButton.setText("Button");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
Button myButton1 = new Button(this);
myButton1.setText("Button1");
ll.addView(myButton1, lp);

create one linearlayout with horizontal orientation and add two buttons on that.. when you have more than two buttons then add another linearlayout with horizontal for that buttons..

Related

adding linear layout to current view

I'm sure this has come up often, but I couldn't find an exact solution for my issue. I'm simply trying to programmatically create a linear layout with a textview and 2 buttons and add it to the bottom of my fragment (which is also a linear layout). I.e. I want it to appear below the other views on the screen not the actual bottom. However, when I try to add it, it always appears at the top corner and not the end. Here's my code:
LinearLayout layout = new LinearLayout(getActivity());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout.setBackgroundColor(Color.CYAN);
TextView noteView = new TextView(getActivity());
LayoutParams lparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
noteView.setLayoutParams(lparams);
noteView.setText(noteText);
layout.addView(noteView);
getActivity().addContentView(layout, lparams);
}
Have you tried to add gravity to the LinearLayout?
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.BOTTOM;
layout.setLayoutParams(params);
If your main goal is to have your views line up relative to each other, perhaps it would be worth considering using a Relative Layout to accomplish a similar task.
If that's not an option for whatever reason, I think LayoutParams has a gravity option that can be set to bottom which will align views to the bottom of the parent view. I can't guarantee this will work though as I usually do not accomplish tasks like these programmatically.

Android layout creation dynamic runtime

I need to create a layout dynamically like this through code instead creating through XML.
I am able design either in vertical or horizontal buttons. But I need to create both vertical and horizontal in a same layout.
Please help me to do it in easy way.
Thanks in advance
Its simple, First create the parent linear layout
LinearLayout parentLayout = new LinearLayout(this);
set the parameters and orientation for this parent layout:
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)
parentLayout.setOrientation(VERTICAL OR HORIZONTAL);
parentLayout.setLayoutParams(layoutParam);
Thats it , your parent layout has been generated. Now create new layout and start adding it to this layout. For ex.:
LinearLayout firstLayout = new LinearLayout(this);
LinearLayout secondLayout = new LinearLayout(this);
LinearLayout thirdLayout = new LinearLayout(this);
parentLayout.addView(firstLayout);
parentLayout.addView(secondLayout);
parentLayout.addView(thirdLayout);
Also you need to set the parameters for all the layout seperatly. You can optimise the code according to your need.
Hope it works!

android - How to set the button params in the Listview

I added the Button has footer view of the List view pro-grammatically. Now i want to set the params of the button and set the button gravity as center vertical. I did in the following way but it's not working.How to implement this?
btnMoreUpcomingExits = new Button(this);
btnMoreUpcomingExits.setText("More Upcoming Exits");
btnMoreUpcomingExits.setBackgroundResource(R.layout.moreupebtn_widget);
btnMoreUpcomingExits.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.WRAP_CONTENT, ListView.LayoutParams.WRAP_CONTENT));
btnMoreUpcomingExits.setTextColor(Color.WHITE);
btnMoreUpcomingExits.setTypeface(null, Typeface.BOLD);
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
btnMoreUpcomingExits.setOnClickListener(btnMoreUpcomingExitsListener);
getListView().addFooterView(btnMoreUpcomingExits);
setListAdapter(new UpcomingExitsResultsListViewAdapter(this));
You currently have
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
I assume this is an oversight and you meant CENTER_VERTICAL? This will vertically align the text inside the button which it should already be doing so your question seems confusing. Are you instead asking how to align the button itself vertically? If so you need to set the gravity on the buttons parent.
As for the params, you are already setting layout params. What else are you trying to do? You should aim to be more specific in your questions so it is easier to identify what you are trying to do.
Try this:
LinearLayout layout=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
btnMoreUpcomingExits = new Button(this);
btnMoreUpcomingExits.setText("More Upcoming Exits");
btnMoreUpcomingExits.setBackgroundResource(R.layout.moreupebtn_widget);
btnMoreUpcomingExits.setLayoutParams(params);
btnMoreUpcomingExits.setTextColor(Color.WHITE);
btnMoreUpcomingExits.setTypeface(null, Typeface.BOLD);
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
layout.addView(btnMoreUpcomingExits);
btnMoreUpcomingExits.setOnClickListener(btnMoreUpcomingExitsListener);
getListView().addFooterView(btnMoreUpcomingExits);
setListAdapter(new UpcomingExitsResultsListViewAdapter(this));
Check this this it might help you.
Check this link
you might use WindowManager.LayoutParams to set attribute of button in listview.

Add button to bottom of linear layout programmatically

I have a bunch of buttons displayed using the default gravity. The last button I add to the LinearLayout view is what I'd like to appear at the bottom of the view. How do I add it programmtically to appear at the bottom of the screen? I've tried setting the gravity, but everything falls to the bottom. I just want the one button to fall to the bottom of the screen. Ideally, I won't have to make another view.
Try this:
Button button = new Button(this);
youLinearLayout.addView(button, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM));
edit
sorry, the code above dont work.
You cant do this using an single LinearLayout if orientation==vertical.
You'll need create another layout(RelativeLayout) and add TextView to it.
RelativeLayout relative = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayout.addView(textView, params);
linearLayout.addView(relativeLayout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
If the rest of the screen is not empty, you can give android:layout_weight=0.0 to the Button, and 1.0 to the widget on the top.
In that way, widget with 1.0 weight will expand to fill empty areas, and Button with 0.0 will take only the default space, and being the last item addes to a vertical LinearLayout, it will be sticked to the bottom.

How to set the android property layout_alignParentBottom to a button dynamically

I have a dynamic layout and I have a button. How can I set the property layout_alignParentBottom to that button so that it would appear in the bottom of my relative layout?
Or do you know another way?
This will only work if your layout is a RelativeLayout, as other ViewGroups don't understand alignParentBottom.
RelativeLayout layout = findViewById(R.id.my_relative_layout);
Button button = new Button(this); // your button
lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); // You might want to tweak these to WRAP_CONTENT
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(button, lp);

Categories

Resources