programmatically bottom margin to ImageView not working - android

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.

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.

Android negative margin does not work

I have encountered a problem when i try to give a negative left margin to a LinearLayout.
The negative margin does not appear.
Here is my code
HorizontalScrollView hview = new HorizontalScrollView(context); // HorizontalScrollView is the outer view
RelativeLayout.LayoutParams hs_lot_params = new RelativeLayout.LayoutParams(164, 164);
hs_lot_params.setMargins(100, 100, 0, 0); // set the positions
ImageView image = new ImageView(context);
image.setBackgroundResource(R.drawable.leder);
LinearLayout.LayoutParams img_lot_params = new LinearLayout.LayoutParams(164, 164);
img_lot_params.setMargins(0, 0, 0, 0);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(164, 164);
layoutParams.setMargins(-132, 0, 0, 0);
ll.addView(image, img_lot_params);
hview.addView(ll, layoutParams);
Note: my plan is to scroll the image from left to right.
First, the left part of the image is hidden and can scroll to right to see the full image
ViewGroup.MarginLayoutParams params =
(ViewGroup.MarginLayoutParams)view.getLayoutParams(); params.topMargin = -100;
Negative margins should work in LinearLayout and RelativeLayout. What you probably need, is to scroll the HorizontalScrollView with scrollBy(int x, int y) or scrollTo(int x, int y) to achieve the "peek and scroll" effect you described.
Also keep in mind that using raw pixel units is generally a bad idea as the actual size will depend on the pixel density of the screen. Prefer dp measurements instead.

How to set a desired layout for an animating fragment?

I have a fragment which is animating from right to left. I want to set it to stop at like 100 units from the left part of the screen. This is what i did so far.
RelativeLayout tempLi;
Display display = MyActivity.context.getWindowManager().getDefaultDisplay();
final int width = display.getWidth();
tempLi.setLayoutParams(new LayoutParams(width-100, LayoutParams.FILL_PARENT));
This line of code is getting the layout 100 units short from the right side. I tried doing -width + 100 , it didn't work.
Any suggestions will be appreciated. TIA
You can set the left-margin to the layout as follows:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layoutParams.setMargins(100, 0, 0, 0);
tempLi.setLayoutParams(layoutParams);

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.

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