set layout margin programmatically android

Solutions on MaxInterview for set layout margin programmatically android by the best coders in the world

showing results for - "set layout margin programmatically android"
Marie
06 Feb 2018
1LinearLayout ll = new LinearLayout(this);
2ll.setOrientation(LinearLayout.VERTICAL);
3
4LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
5     LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
6
7layoutParams.setMargins(30, 20, 30, 0);
8
9Button okButton=new Button(this);
10okButton.setText("some text");
11ll.addView(okButton, layoutParams);
12
Cassandra
23 May 2020
1int sizeInDP = 16;
2
3int marginInDp = (int) TypedValue.applyDimension(
4            TypedValue.COMPLEX_UNIT_DIP, sizeInDP, getResources()
5                    .getDisplayMetrics());
6setMargins(view, sizeInDP, sizeInDP, sizeInDP, sizeInDP);
7
8
9void setMargins (View view, int left, int top, int right, int bottom) {
10  if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
11    ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
12    p.setMargins(left, top, right, bottom);
13    view.requestLayout();
14  }
15}
16
Irene
18 Feb 2017
1ViewGroup.LayoutParams p = this.getLayoutParams();
2    if (p instanceof LinearLayout.LayoutParams) {
3        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)p;
4        if (_default) lp.setMargins(mc.oml, mc.omt, mc.omr, mc.omb);
5        else lp.setMargins(mc.ml, mc.mt, mc.mr, mc.mb);
6        this.setLayoutParams(lp);
7    }
8    else if (p instanceof RelativeLayout.LayoutParams) {
9        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)p;
10        if (_default) lp.setMargins(mc.oml, mc.omt, mc.omr, mc.omb);
11        else lp.setMargins(mc.ml, mc.mt, mc.mr, mc.mb);
12        this.setLayoutParams(lp);
13    }
14    else if (p instanceof TableRow.LayoutParams) {
15        TableRow.LayoutParams lp = (TableRow.LayoutParams)p;
16        if (_default) lp.setMargins(mc.oml, mc.omt, mc.omr, mc.omb);
17        else lp.setMargins(mc.ml, mc.mt, mc.mr, mc.mb);
18        this.setLayoutParams(lp);
19    }
20}
21
queries leading to this page
how to add top margin programmatically androidset margin dynamically androidset margin by code androidhow to give a margin from right in linearlayout androidandroid margin starthow to set margin to match parentandroid set layoutmargin progrmatically javandroid studio linearlayout set margin programmaticallyhow to give margin programmatically in androidandroid view set margin programmaticallyhow to add margin programmatically in androidandroid studio layout marginlinearlayout margin programmatically androidandroid get margin programmaticallyandroid java set margin of view programatticalyhow to set below margin programetically in android javget view margin from top screen androidset margins javahow to set margin programmatically in androidset margin to constraintslayout programmatically androidandroid params leftmargin in kotlinandroid view set margin programmatically and refresh layout margin androidandroid set layout margin top programmaticallyset auto margin in androidandroid studio margin programmaticallyhow to set margin in viewpager elements in androidhow to set margins to 0 in android studiochange margin programmatically androidadd margin bottom programmatically androidandroid image button set margin programmaticallymargin top programmatically androidset margin constraintlayout programmatically androidandroid layout margin topset layout margin programmatically androidlayout margin programmatically androidandroid how to set margin programmaticallygive margin to layout in androidset margin top programmatically androidandroid studio set marginsset marginbottom programmatically androidandroid layout weight with marginandroid edit margin view programmaticallychange margin top programmatically androidset margin programmatically constraint layout in androidadd margin to view programmatically androidset margins programmatically androidandroid margin top programmaticallyprogrammatically set margin androidview set margin programmatically androidandroid change margin top programmatically of viewandroid layout margingive margin programmaticaly java androidlayout params margin androidhow to assign text to bottom of layout linearlayout in androidchange layout margin programmatically androidset layout margin start programmaticallyhow to leftmargin in java androidhow to set margin of a view programmatically in androidjava android get margin programmaticallyandroid studio change button margin programmaticallyandroid set margin dp programmaticallymargin left programmatically androidandroid set margin programmaticallyandroid add margin programmaticallyset margin programmatically constraint layout androidandroid layout default marginandroid view get margin programmaticallyandroid add marginandroid set view margin programmaticallyandroid studio linearlayout set marginandroid linear layout marginset margin linearlayout programmatically androidset top margin programmatically androidhow to programatically add margin androidhow to set margin in android programmaticallyset margin from code androidset margin layout programmatically android 27fab 27 set margin program androidmargin start androidmargin style androidandroid margin autofab set margin program androidset textview margin programmatically androidandroid layout margintop programmaticallymargin padding androidadd margin programmatically androidandroid set margin searchviewandroid java adapter set margin programmaticallyset start margin programmatically androidandroid studio params set marginsset margin constrainlayout programmatically androidstart margin in programmatically in androidandroid manual layout with marginandroid studio set margin bottom programmaticallymargin programically androidandroid set layoutmargin progrmaticallyhow to set margin bottom programmatically in androidadd margin programatically in androidset margin with style androidlayout margin in android studio programmaticallyset margin programticly androidandroid add layout margin programmaticallyset margin bottom programmatically androidandroid studio set margin programmaticallyandroid layout margin leftset margin in layout androidandroid margin 25layout margin javarelativelayout layoutparams marginendandroid edit margin view to programmaticallyset marginprogrammatically androidandroid java set layout margingive margin programmatically androidlinear layout set marginandroid add margin to view programmaticallyandroid change margin top programmaticallywhen to use margin and when padding androidchange margin of view programmatically androidset layout margin from javaandroid linearlayout marginandroid studio set margin end starthow to give margin androidhow to set the layout margin for androidandroid code viewgroup padding margniandroid marginset margins android programmaticallylayout params horizontal margin androidset margin start to bottom programmatically androidset item margin programmatically android kotlinset margin end programmatically androidmargin androidjva program set margin numbers to 10set margin when gone programmaticallyandroid 3aset margin top programmaticallyandroid set margin programmatically constraintlayouthow to set margin to view programmatically in androidandroid set margin top programmaticallyandroid java set margin startset margin view programmatically androidlayout margin is changed androidset margin programitically androidhow to dynamically set margin of a view programmatically in androidandroid programatically set marginmargin programmatically androidandroid studio set layout margin programmaticallyandroid studio change margin from javaset view margin programmatically androidmargin in androidhow to add layout margindefault margin linearlayout android studiohow to set margin in android xmlset margin left programmatically androidset layout margin dynamically androidset margin in android programaticallyandroid set layoutmargin progrmatically javamargin top vs margin end androidset margin programmaticallyandroid linearlayout set height programmaticallyandroid how to change layout margin top programmaticallyandroid java set margin programmaticallyandroid margin left programmaticallyandroid linearlayout set margin programmaticallyset margin cardview programmatically androidandroid programmatically set marginset margin right programmatically androidhow to set view margin horizontal programmaticallyedittext android layout margin programmatically 22kotlin 22setmargins float androidandroid margin programmaticallyandroid set margin start programmaticallyandroid set margin to layouthow to give onidiom for android and ios margin set margin programmatically androidhow to set layout margin horizontal programmaticallyandroid setmargins programmaticallylayout margin android programmaticallyset bottom margin programaticalyandroid programmatically viewgroup marginandroid set margin programmatically dphow to set layout margin programmatically in androidandroid change margin programmaticallyhow to set margin using code in androidmargin view in androidset margin java androidset margin of view programmatically androidset layout margin programmatically android