In Android, how do I set margins in dp programmatically?
You should use LayoutParams
to set your button margins:
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(params);
Depending on what layout you're using you should use RelativeLayout.LayoutParams
or LinearLayout.LayoutParams
.
And to convert your dp measure to pixel, try this:
Resources r = mContext.getResources();
int px = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
yourdpmeasure,
r.getDisplayMetrics()
);
Margins of a LinearLayout, programmatically with dp
You can use DisplayMetrics and determine the screen density. Something like this:
int dpValue = 5; // margin in dips
float d = context.getResources().getDisplayMetrics().density;
int margin = (int)(dpValue * d); // margin in pixels
As I remember it's better to use flooring for offsets and rounding for widths.
How Can I Set Margin with 'dp' programmatically?
Well you got to know the parent layout first if it's RelativeLayout you'll need:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(this);
and if it is Linear,Make sure you replace Relative with Linear to get the right layoutParams.Now you can use:
layoutParams.setMargins(i1,i2,i3,i4);
all the values should be in pixel so if you need a method,it should convert dp to px for you.Feel free to use mine then:
public int dpToPx(Context context, float dp) {
return Math.round(dp * getDisplayMetrics(context).density);
}
now just set the layoutParams to the child view:
textView.setLayoutParams(layoutParams);
Set margins in a LinearLayout programmatically
Here is a little code to accomplish it:
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Button okButton=new Button(this);
okButton.setText("some text");
ll.addView(okButton, layoutParams);
Change linear layout top margin programmatically android
layout = (LinearLayout) findViewById(R.id.layoutbtnlinear_aboutme);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)layout.getLayoutParams();
params.setMargins(0, 50, 0, 0);
layout.setLayoutParams(params);
How to convert px to dp in margins programmatically?
Multiply your pixel values by the device display density.
final float density = context.getResources().getDisplayMetrics().density;
final int valueInDp = (int)(valueInPixels * density);
In your case would be as next:
ArrowUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final float density = view.getResources().getDisplayMetrics().density;
if(!Switch){
setMargins(AdditionalOperations, 0, 0, 0, 800 * density);
Switch = true;
}
else{
setMargins(AdditionalOperations, 0, 0, 0, 715 * density);
Switch = false;
}
}
});
Tip: As you are using constant values, you may want to cache them instead of computing the values all the time.
android set margin programmatically
Try this code:
parameter = (RelativeLayout.LayoutParams) txtField.getLayoutParams();
parameter.setMargins(leftMargin, parameter.topMargin, parameter.rightMargin, parameter.bottomMargin); // left, top, right, bottom
txtField.setLayoutParams(parameter);
Change the Right Margin of a View Programmatically?
EDIT: A more generic way of doing this that doesn't rely on the layout type (other than that it is a layout type which supports margins):
public static void setMargins (View v, int l, int t, int r, int b) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
p.setMargins(l, t, r, b);
v.requestLayout();
}
}
You should check the docs for TextView. Basically, you'll want to get the TextView's LayoutParams object, and modify the margins, then set it back to the TextView. Assuming it's in a LinearLayout, try something like this:
TextView tv = (TextView)findViewById(R.id.my_text_view);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
params.setMargins(0, 0, 10, 0); //substitute parameters for left, top, right, bottom
tv.setLayoutParams(params);
I can't test it right now, so my casting may be off by a bit, but the LayoutParams are what need to be modified to change the margin.
NOTE
Don't forget that if your TextView is inside, for example, a
RelativeLayout, one should use RelativeLayout.LayoutParams instead of
LinearLayout.LayoutParams
Related Topics
Hide Tablayout on Scroll of Content Instead of Toolbar
Restarting a Device Programmatically
Android Blurmaskfilter Has No Effect in Canvas.Drawoval While Text Is Blurred
Trying to Port Apache Poi to Android
Sending Message to a Handler on a Dead Thread When Getting a Location from an Intentservice
Unfortunately App Is Getting Stopped While Checking for Network
No Resource Found That Matches The Given Name '@Style/ Theme.Holo.Light.Darkactionbar'
Difference Between Getview & Getdropdownview in Spinneradapter
Viewpager with Fragments - Onpause(), Onresume()
Getting Gradle Error "Could Not Reserve Enough Space for Object Heap" Constantly in Intellij Idea
How to Find Memory Leak Class/Activity in Android
How to Set Different Columns for Rows in Android Gridview
Android Device Phone Call Ability
Android - Open Target _Blank Links in Webview with External Browser
Set Animated .Gif as Background Android