i tried so many example but those answers are not giving me any clarity. any help appriciated
Asked
Active
Viewed 96 times
-3
-
7Possible duplicate of [In Android, how do I set margins in dp programmatically?](https://stackoverflow.com/questions/12728255/in-android-how-do-i-set-margins-in-dp-programmatically) – Gautam Chibde Mar 09 '18 at 06:36
-
@GautamChibde I didn't understand that answer, that why i raised question. and rajan answer is specific to my problem – Mohd Qasim Mar 09 '18 at 07:06
2 Answers
2
Yes, you can set margin programatically using
ProgressBar view;
RelativeLayout.LayoutParams params = view.getLayoutParams();
int marginInPx = convertDpToPx(20);
params.setMargins(0,0,0,marginInPx);
view.setLayoutParams(params);
private int convertDpToPx(float dp){
DisplayMetrics metrics = getResources().getDisplayMetrics();
return (int)((int)dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
}
Rajan Kali
- 11,436
- 3
- 24
- 35
0
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 20);
progressBar.setLayoutParams(layoutParams);
Note: The margin I added is in px you may need to convert it into dp programmatically.
CopsOnRoad
- 175,842
- 51
- 533
- 380