MarginLayoutParams.setMargins() is not working? - android

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.

Related

ALIGN_PARENT_TOP programmatically not working

Why this isn't working?
for (PlayingCard playingCard : Stack0.Cards)
{
ImageView myImg = new ImageView(this);
myImg.setImageResource(R.drawable.c2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.LayoutParams(CardWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
lp.setMargins(0, 0, 0,0);
//lp.addRule(RelativeLayout.ALIGN_TOP); fails
//lp.addRule(RelativeLayout.ALIGN_PARENT_TOP); fails
//lp.addRule(RelativeLayout.ALIGN_START); fails
lp.addRule(RelativeLayout.ALIGN_PARENT_START);
myImg.setLayoutParams(lp);
mat.addView(myImg);
}
the xml
<RelativeLayout
android:id="#+id/PLAY_Mat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
>
</RelativeLayout>
The ImageView is successfully being added, but it's centered vertically. I want it to align to top.
I expect this to be the way even without the added rule, as "By default, all child views are drawn at the top-left of the layout" (RelativeLayout documentation).
Try this code:
RelativeLayout rl = new RelativeLayout(this);
for (PlayingCard playingCard : Stack0.Cards)
{
ImageView myImg = new ImageView(this);
myImg.setImageResource(R.drawable.c2);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.setMargins(0, 0, 0,0);
lay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rl.addView(myImg, lay);
//myImg.setLayoutParams(lp);
//mat.addView(myImg);
}
set MATCH_PARENT to RelativeLayout
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.LayoutParams(CardWidth, ViewGroup.LayoutParams.MATCH_PARENT));
or
You can get using getLayoutParams() of RelativeLayout from XML
RelativeLayout.LayoutParams lp = parentRelativeImageview.getLayoutParams();
Solution by OP.
This code works:
for (PlayingCard playingCard : Stack0.Cards)
{
ImageView myImg = new ImageView(this);
myImg.setImageResource(R.drawable.a1);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(CardWidth, CardHeight);
lay.setMargins(playingCard.UI_MarginLeft, playingCard.UI_MarginTop, 0, 0);
mat.addView(myImg, lay);
}
The key here is CardWidth and CardHeight are both set, and both correct. By correct, I mean correct ratios. Want to double the width? Then double the height etc. If one of w or h is a pixel int, and the other WRAP_CONTENT, then weirdness happens (in the form of the image having either a top margin or a left margin).
Once both w and h are set correctly, lp.addRule(RelativeLayout.ALIGN_PARENT_START) isn't needed.

programmatically bottom margin to ImageView not working

marginBottom not working. It works but the margin is only a few space regardless of how much margin is applied. Why?
Java-code :
LinearLayout onepage= (LinearLayout) findViewById(R.id.onepage);
RelativeLayout bluegreen = new RelativeLayout(this);
p_ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
bluegreen.setLayoutParams(p_ll);
//some other views that make up the whole page
//bottom most image
ImageButton migs = new ImageButton(this);
p_rl = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p_rl.addRule(RelativeLayout.CENTER_HORIZONTAL);
p_rl.setMargins(0, 0, 0, 20);
register.setLayoutParams(p_rl);
register.setImageResource(R.drawable.migs);
register.setPadding(0, 0, 0, 0);
bluegreen.addView(migs);
onepage.addView(bluegreen);
XML:
<LinearLayout
android:id="#+id/onepage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".PageActivity" >
</LinearLayout>
Have you notice that in setMargins method parameters are in pixel and not in dp? take a look at android doc page
So depending of your device screen size but 20 px can be very small.
Don't forget to call requestLayout() to take in account margin changes.

How to set a button's parameters programmatically

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);

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);

Margin of buttons in linear layout

I am creating some buttons and add them to a linear layout which is defined as
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/mylayout">
</LinearLayout>
The buttons are created with
for (int i = 0; i < 3; i++)
{
Button btn = new Button(activity);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(1, 1, 1, 1);
btn.setText("Button");
btn.setPadding(0, 0, 0, 0);
mylayout.addView(pv, lp);
}
These buttons always have a margin (about 3px) which I'd like to remove. Is there anything I am missing? If I use a custom view which I created there is no space between them.
Should I set
lp.setMargins(-3, -3, -3, -3);
which removes the margin? Is there a drawback with this?
I do not really think they have a margin but it is related to the background of the button. Probably the default background of the button has a image like this one:
http://developer.android.com/guide/developing/tools/draw9patch.html
which includes fiction margins. Here you can find more info about 9-Patch.
http://developer.android.com/guide/topics/resources/drawable-resource.html
In my opinion if you want to remove the "margins", you should create a different background for the image because the -3 value is not a good workaround (IHMO).
Why are you using Layout Params .. simply add the view after its creation...
This will surely remove the problem
for (int i = 0; i < 3; i++)
{
Button btn = new Button(activity);
btn.setText("Button");
mylayout.addView(pv);
}

Categories

Resources