Change linear layout top margin programmatically android
Asked Answered
G

7

41

i have two linear layouts in one frame layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="left"
    android:orientation="vertical">

     <FrameLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/image12">
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">   

                <LinearLayout
                    android:id="@+id/layoutbtnlinear_aboutme"
                    android:layout_width="fill_parent"
                    android:layout_height="55dp"
                    android:gravity="bottom"
                    android:layout_marginTop="10dp"
                     android:background="#b2b2b2" 
                    android:orientation="horizontal" >

                    <ImageView
                        android:id="@+id/imgShare_layout_aboutme"
                        android:layout_width="wrap_content"
                        android:layout_height="55dp"
                        android:layout_gravity="right|center|end"
                        android:layout_weight="1.63"
                        android:src="@drawable/ic_share" />

                    <TextView
                        android:id="@+id/txtTitle_layout_aboutme"
                        android:layout_width="wrap_content"
                        android:layout_height="55dp"
                        android:layout_gravity="left"
                        android:layout_weight="0.3"
                        android:fontFamily="Times New Roman"
                        android:text="About Me"
                        android:textColor="@android:color/black"
                        android:textSize="35sp"
                        android:textStyle="italic" />
                </LinearLayout>

            <LinearLayout
                android:id="@+id/content"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <ImageButton
                       android:id="@+id/btnSlidingDrawerHandler_layout_aboutme"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_gravity="center"
                       android:background="@drawable/ic_1_navigation_collapse" />


               <ListView
                    android:id="@+id/listView_layout_aboutme"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:footerDividersEnabled="true"
                    android:dividerHeight="4px"
                    android:isScrollContainer="true" 
                    android:scrollbarAlwaysDrawVerticalTrack="true" 
                    android:scrollbarStyle="outsideInset" 
                    android:scrollbars="vertical">
                </ListView>
            </LinearLayout>
        </LinearLayout>         
     </FrameLayout>  

</LinearLayout>

Here I'm seting top margin of linear layout with id layoutbtnlinear_aboutme to 10dp but in code I want to change this 10dp to 50dp on some condition how can I change this top margin programatically?

Grivet answered 18/6, 2013 at 12:11 Comment(1)
take a look on google, there are many other questions similar to this, like #9679285Darnel
D
109
   layout = (LinearLayout) findViewById(R.id.layoutbtnlinear_aboutme);
   LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)layout.getLayoutParams();
   params.setMargins(0, 50, 0, 0); 
   layout.setLayoutParams(params);
Desex answered 18/6, 2013 at 12:16 Comment(1)
setMargins take this parameters (left,top,right,bottom) , if anyone wants to knowTega
J
29

LayaoutParams usually create confusion while setting margin because of their parent layout... So this MarginLayoutParams is very useful which works with all layouts.

Java Code

MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
params.width = 200; //Ths value 200 is in px... Please convert in DP
params.leftMargin = 100; 
params.topMargin = 200;

Kotlin code

val params: MarginLayoutParams = view!!.layoutParams as MarginLayoutParams
params.width = 200 
params.leftMargin = 100 
params.topMargin = 200
Jeneejenei answered 26/2, 2016 at 9:35 Comment(5)
This is the best solution as it allows you to set individual margins correctly. For instance, if you have to only set topMargin, just use params.topMargin = yourValue. That way the other margins will NOT be affected. If you use Riser's solution, you will have to use setMargins() which requires you to set ALL 4 margins.Capuchin
You might have to call view.invalidate() and view.requestLayout() after changing these values.Ninebark
view.layoutParams = params this line is more important one.Meaningful
very important to mention that the units are in PIXELS and not in DP.Harod
Perfect solutionOrientalism
W
9

This updates the top margin without the need to update the other margin values.

LinearLayout layout = (LinearLayout) findViewById(R.id.your_linear_layout);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams();
layoutParams.topMargin = 200;
layout.setLayoutParams(layoutParams);
Warm answered 4/2, 2016 at 8:32 Comment(0)
C
4

use this

    layout = (LinearLayout) findViewById(R.id.layuout);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    layoutParams.setMargins(30, 20, 30, 0);
layout.setLayoutParams(layoutParams );
Clardy answered 18/6, 2013 at 12:16 Comment(1)
thanks but in this code where are we going to specify the id of linear layout. that on which layout we have to set marginGrivet
P
2

Use this method to set margin in dp

private void setMargins (View view, int left, int top, int right, int bottom) {
    if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();

        final float scale = getBaseContext().getResources().getDisplayMetrics().density;
        // convert the DP into pixel
        int l =  (int)(left * scale + 0.5f);
        int r =  (int)(right * scale + 0.5f);
        int t =  (int)(top * scale + 0.5f);
        int b =  (int)(bottom * scale + 0.5f);

        p.setMargins(l, t, r, b);
        view.requestLayout();
    }
}

Call the method :

setMargins(linearLayout,0,0,5,0);

https://mcmap.net/q/73539/-in-android-how-do-i-set-margins-in-dp-programmatically

Prakrit answered 20/6, 2018 at 15:35 Comment(0)
B
2

Kotlin solution:

fun LinearLayout.setMargins(left: Int? = null, top: Int? = null, right: Int? = null, bottom: Int? = null) {
    val params: LinearLayout.LayoutParams = this.layoutParams as LinearLayout.LayoutParams

    val left = left?.let { it } ?: params.leftMargin
    val top = top?.let { it } ?: params.topMargin
    val right = right?.let { it } ?: params.rightMargin
    val bottom = bottom?.let { it } ?: params.bottomMargin

    params.setMargins(left, top, right, bottom)
    this.requestLayout()
}

then you just need to:

layoutbtnlinear_aboutme.setMargins(top = 10)
Brighten answered 19/10, 2018 at 16:50 Comment(0)
R
1

I have set up margins directly using below code (I tried using LinearLayout.LayoutParams but is was not working for me)

LinearLayout layout = (LinearLayout)findViewById(R.id.yourrelative_layout);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(3, 300, 3, 3); 
layout.setLayoutParams(params);

Only this here is to notice that LayoutParams should be imported for following package android.widget.RelativeLayout.LayoutParams unless you will hit error.

Rhythmandblues answered 27/8, 2014 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.