How to set a button's parameters programmatically - android

I'm trying to add a bunch of buttons to a layout like this:
for( int i = 0; i < 10; i++ ) {
Button button = new Button( this );
button.setText( "" + i );
( ( LinearLayout )dialog.findViewById( R.id.Buttons ) ).addView( button );
}
My problem is how do I do this programmatically to all the buttons:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="32dip" />
I've been looking at LayoutParams but it doesn't look complete. Like how do I set the textSize to 32 dip?

Set your attributes using the following code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER_HORIZONTAL);
button.setTextSize(32);
If you want to specify the text size units use:
button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 32);

LayoutParams relates to the parent ViewGroup which will contain the view. So in your case it's a LinearLayout so you need to create parameters for that one. Here's what I'm talking about:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.weight = 1f;
Button button = new Button(this);
button.setLayoutParams(lp);
button.setText("" + i);
((LinearLayout)dialog.findViewById(R.id.Buttons)).addView(button);

Use LayoutParams for the height, width and gravity with
LinearLayout.LayoutParams (int width, int height)
where you can use WRAP_CONTENT for the ints.
Then there are Button.setGravity() and Button.setTextSize() for the last two.
Hope this helps.

You'd use a LayoutParams object for the layout settings, and to set the text size use setTextSize() from the Button class.
You can set the gravity with setGravity() too.

TextSize is not inside Layout params. To set textSize you have to
button.setTextSize(32);

Related

Set View height to 0dp and weight to 1 programatically

When creating GridView via xml I use the below code, but how could I achieve that programmatically during runtime?
android:layout_height="0dp"
android:layout_weight="1"
Thanks.
use this
GridView YOUR_GRID_VIEW =(GridView )findViewById(R.id.GridView )
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ 0,
/*weight*/ 1.0f
);
YOUR_GRID_VIEW.setLayoutParams(param);
LayoutParams.MATCH_PARENT is for width, 0 for Height and 1.0f is for weight
If you have layout_weight in xml, that means, that the parent layout of GridView is LinearLayout.
Having a reference to a GridView, that is already attached to view hierarchy, you can simply get LayoutParams of that view, cast it to LinearLayout.LayoutParams and perform necessary changes:
GridView gridView = ...;
LinearLayout.LayoutParams params = gridView.getLayoutParams();
params.weight = 1;
params.height = 0;
gridView.setLayoutParams(params);
Otherwise, if you are constructing GridView from scratch, you should create LinearLayout.LayoutParams on your own:
LinearLayout linearLayout = ...;
GridView gridView = new GridView(context);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
/* height */ 0,
/* weight */ 1);
linearLayout.addView(gridView, params);

Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

I want to add buttons to relative layout dynamically as per button and screen width as shown in image. Number of buttons is not fixed and width of button depends on the text which is set to the button.
I tried to achieve this using below code but that to not working fine as per my requirement.
Can anyone please help me ? Please help me through this.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.genre_layout);
for(int i=0; i < genreList.size(); i++){
Button btn = new Button(MovieDetailsActivity.this);
btn.setText(genreList.get(i).getGenreName());
btn.setPadding(15, 5, 15, 5);
btn.setBackgroundColor(Color.parseColor("#FFFFFF"));
btn.setBackgroundColor(Color.parseColor("#80333333"));
LayoutParams param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
param.setMargins(5, 5, 5, 5);
if (i != 0){
int prevId = genreList.get(i).getGenreId();
param.addRule(RelativeLayout.RIGHT_OF, prevId);
}
btn.setLayoutParams(param);
btn.setId(genreList.get(i).getGenreId());
layout.addView(btn);
}
Try this
Display display=getWindowManager().getDefaultDisplay();
int width=display.getWidth();
btn.setWidth(width);
or if you have two buttons, do
Display display=getWindowManager().getDefaultDisplay();
int width=display.getWidth();
btn1.setWidth(width/2);
btn2.seTwidth(width/2);
Update
LinearLayout.LayoutParams paramz = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
0dp, 1.0f);
then btn.setLayoutParams(paramz);
I have the code to add buttons dynamically to linear layout. Just check out whether it helps you https://docs.google.com/document/d/1QA1tAIe601fZT2Dlp1A0qDuLD0U4IqKL3f_Crx1rtkE/edit?usp=sharing
Try this for adding dynamic buttons in relativelayout
RelativeLayout layout = (RelativeLayout)findViewById(R.id.slidingDrawerContent);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
for (int i=0; i<4; i++) {
Button btn = new Button(this);
btn.setId(i);
btn.setText("some_text");
// lp.addRule(RelativeLayout.RIGHT_OF, <Id>);
layout.addView(tv2, lp);
}
I would like to suggest use LinearLayout as you asked in your question and give a Weight.
LinearLayout
I Solved my problem by using FlowLayout.
Sample code and references are available here
Its simple to use. Thank you all for help.

MarginLayoutParams.setMargins() is not working?

Here's the thing: I want to add some images programmatically. The images should have to have a topMargin of 5dip except for the first image, in a LinearLayout with a vertical orientation manner. below the code segment:
LinearLayout body = (LinearLayout) findViewById(R.id.body);
for (int i = 1; i <= 4; i++) {
ImageView img = new ImageView(this);
MarginLayoutParams lp = new MarginLayoutParams(-2, -2);
img.setImageResource(R.drawable.image);
if (i != 1) {
lp.setMargins(0, 5, 0, 0);
}
img.setLayoutParams(lp);
body.addView(img);
body.requestLayout();
}
By running the program I can see the the 4 images(here) vertically aligned one by one but there is no topMargin(as in the code,5dip). body is the id of the LinearLayout. here's the XML segment to:
<LinearLayout
android:id="#+id/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#901b0e08"
android:orientation="vertical"
android:paddingLeft="6dp"
android:paddingRight="8dp" >
</LinearLayout>
I cant get what went wrong here.
Thanks.
Try changing your MarginLayoutParams to this:
LayoutParams lp = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
The reason for doing this, is that body is a LinearLayout and thus you would want to use the LinearLayout-specific LayoutParams.
Try to set padding but margin for ImageView. Sometimes it works fine. Your can directly set padding size to ImageView w/o declare LayoutParams.
You probably need to set LayoutParams' gravity property, for example:
lp.gravity = 0; //AXIS_X_SHIFT
That's what works fine for me.

Setting LayoutParams with few Lines of Code

Is there an easy and simple way to set LayoutParams ? Or to be exactly, the MarginLayoutParams? I want to set the MarginRight to a few dp, unfortunately im not able to set these in the LayoutFile cause the Target is a ListFragment and in Code-Behind looks it very ugly.
The reason i do this not in the Layout of the items is so the code is optimized and perfomant.
To sum up: Is there any very simple and clean way to set Params ?
Yes, you can do something like this:
MyImageView i2 = new MyImageView(context);
LayoutParams lp = new LayoutParams(300, 300);
lp.LeftMargin = 100;
lp.TopMargin = 100;
lp.Gravity = 0;
this.AddView(i2, lp);
LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
lp.setMargins( left, top, right, bottom );
You might need to write it as LinearLayout.LayoutParams, depending on what type of layout is the container.
And then you call the method setLayoutParams( lp ); on the given view/layout/widget.

How to add margin to a Button at run time?

I have to add more than one buttons on a LinearLayout. But I want to put those buttons say 5px apart. I could not find a way to set a margin of a Button. Any idea?
Use the layout_margin property of the button element. Does that not work?
<Button android:layout_margin="5px" (...)/>
Edit
When creating a button in java, LayoutParams is used to specify margins and so.
Button button = new Button();
(....)
params = new LinearLayout.LayoutParams();
params.leftMargin = 5;
(set params as you need)
parent.addView(button, params);
The LayoutParams you use must match the Layout you add your button in (see http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html). For a LinearLayout, use a LinearLayout.LayoutParams and set the *Margin fields accordingly.
Try this:
//Assuming your button is in a LinearLayout as stated
LinearLayout.LayoutParams params = myButton.getLayoutParams();
params.setMargins(0, 0, 0, 0) //left, top, right, bottom
myButton.setLayoutParams(params);
MarginLayoutParams params = (MarginLayoutParams) vector8.getLayoutParams();
params.width = 200; params.leftMargin = 100; params.topMargin = 200;
vector8.setLayoutParams(params);
Need use type of: MarginLayoutParams
Adding padding to the buttons would also solve your problem. Check this out
http://developer.android.com/resources/tutorials/views/hello-formstuff.html#CustomButton
use this code to set margin at runtime
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btnContainer.getLayoutParams();
params.setMargins(5, 50, 10, 0); //left, top, right, bottom
view_whwre_set_margin.setLayoutParams(params);

Categories

Resources