Android: Align Parent Bottom + Bottom margin programmatically - android

How can I programatically add a RelativeLayout that is aligned to the bottom of parent, and add a margin or padding in the same RelativeLayout?
Example:

Here's how you can do that:
// Get the parent layout
RelativeLayout parent = (RelativeLayout) findViewById(R.id.parent);
// Create your custom layout
RelativeLayout relativeLayout = new RelativeLayout(this);
// Create LayoutParams for it // Note 200 200 is width, height in pixels
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(200, 200);
// Align bottom-right, and add bottom-margin
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.bottomMargin = 100;
relativeLayout.setLayoutParams(params);
relativeLayout.setBackgroundColor(Color.BLUE);
// Add the custom layout to your parent layout
parent.addView(relativeLayout);

you can try this it work in my case:
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) view.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

Related

Dynamic LinearLayout with same height as width

I'm trying to create a dynamic LinearLayout with the same height as width. With XML I can achieve this by setting the the height as 0px i.e. android:layout_height="0dp". But I'm not able to achieve the same thing while creating the layout from Java code.
LinearLayout parentLayout = layoutInflater.inflate();
LinearLayout childLayout = new LinearLayout(context);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.height = 0;
parentLayout.addView(childLayout, layoutParams);
I want the childLayout to be of the same height as width but the height appears to be set to WRAP_CONTENT instead, what am I missing?
layoutParams.weight = layoutParams.height

Set webview dimensions programmatically

I'm using a webview in my code, and I'm trying to set the dimensions via programmatically. Is there a way i can do it?
You should use RelativeLayout instead
EXAMPLE:
Suppose you want a WebView of size (50, 60) on your screen at position (70, 80)
// RelativeLayout. though you can use xml RelativeLayout here too by `findViewById()`
RelativeLayout relativeLayout = new RelativeLayout(this);
// webView
WebView w= new WebView(this);
w.setId(0X100);
w.setScrollContainer(false);
// Setting layout params to our RelativeLayout
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(50, 60);
// Setting position of our ImageView
layoutParams.leftMargin = 70;
layoutParams.topMargin = 80;
// Finally Adding the imageView to RelativeLayout and its position
relativeLayout.addView(w, layoutParams);

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

Android How to set margin in Relativelayout

I am trying to set margin from top.
RelativeLayout root = (RelativeLayout) view.findViewById(R.id.root);
listview = new PullListView(getActivity());
root.addView(listview);
How can I set margin of listview?
RelativeLayout.LayoutParams relativeParams =(RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
relativeParams.setMargins(0, 80, 0, 0); // left, top, right, bottom
relativeLayout.setLayoutParams(relativeParams);
Use LayoutParams
LayoutParams params=(LayoutParams) root.getLayoutParams();
params.leftMargin=0;
params.rightMargin=0;
params.rightMargin=0;
params.leftMargin=0;
root.setLayoutParams(params);

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