How to change the margin of an "included" layout programatically

5.3k Views Asked by At

I have a LinearLayout inside which I am including a different layout. Something like this

<LinearLayout
                    android:id="@+id/linear_layout"
                    android:layout_width="match_parent"
                    android:layout_height="20dp"
                    android:visibility="gone"
                    tools:visibility="visible">

                    <include
                        layout="@layout/new_layout"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="4dp"
                        android:layout_marginLeft="4dp"
                        android:layout_marginStart="4dp"
                        android:layout_weight="1"/>

</LinearLayout>

Now what I would like to do is, to programmatically change the "marginBottom" of the included layout (new_layout). How can I do that?

I tried calling various LayoutParams and tried changing the margin, but not sure if it is the right approach. Any help greatly appreciated.

Thanks

5

There are 5 best solutions below

0
On

Try this.. Make the Linear layout using your id

    LinearLayout ll =(LinearLayout) findViewById(R.id.linear_layout);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, 20);

    layoutParams.setMargins(left,top,right,bottom);

where you can pass the value in setMargins

and if you want to input value in dp.. Try this

public void setMargin(Context con,ViewGroup.LayoutParams params,int dp) {

    final float scale = con.getResources().getDisplayMetrics().density;
    // convert the DP into pixel
    int pixel =  (int)(dp * scale + 0.5f); 

    ViewGroup.MarginLayoutParams s =(ViewGroup.MarginLayoutParams)params;
    s.setMargins(pixel,pixel,pixel,pixel);

    yourView.setLayoutParams(params);
}

where you can pass your layout params

1
On

I think this code helpful...

include.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" 
 android:gravity="center">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" >
</ImageView>

</LinearLayout>

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<include
    android:id="@+id/include_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="4dp"
    android:layout_marginStart="4dp"
     layout="@layout/include" />

 </LinearLayout>

MainActivity

package com.example.stackdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;


 public class MainActivity extends Activity {

 LinearLayout include_layout;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    include_layout = (LinearLayout) findViewById(R.id.include_layout);

    @SuppressWarnings("deprecation")
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);

    layoutParams.setMargins(0, 0, 0, 100);
    include_layout.setLayoutParams(layoutParams);
 }

  }
0
On

include.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:id="@+id/include_layout">

<ImageView android:layout_width="90dp"
    android:layout_height="90dp"
    android:src="@drawable/cross" />

</LinearLayout>

activity_main.xml

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

<include layout="@layout/include"/>

<TextView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello"
    android:padding="10dp" />

</LinearLayout>

MainActivity.class

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    LinearLayout included=(LinearLayout)findViewById(R.id.include_layout);
    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    params.setMargins(Math.round(10), Math.round(10), Math.round(10), Math.round(10));

    //set margin...
    included.setLayoutParams(params);
}
}
0
On

You should not directly change or modify margins in the <include> tag. This tag is just adding the layout resource in the current view hierarchy without adding an extra parent view container.

So essentially what you want to do is set margins on the root view container of the "included layout". That is you should directly call findViewById() for the root view and set all of your margins on it pro grammatically and as the included view's parent is a Linear-layout you should use

   LinerLayout.LayoutParams params = (LinerLayout.LayoutParams) includedRootView.getLayoutParams();
   params.setMargins();
   includedViewRoot.setLayoutParams(params);` 
0
On

You can change it by dimensions also

res/values/dimens.xml    
res/values-small/dimens.xml    
res/values-normal/dimens.xml    
res/values-xlarge/dimens.xml

enter image description here