How to Set LinearLayout Margins in Android Programmatically

In this android programming source code example, we going to explore how to set LinearLayout Margins in android programmatically.

Below is a step by step source code to set LinearLayout Margins in android programmatically.

package com.inducesmile.inducesmile_button.layout;

import android.os.Bundle;
import android.widget.LinearLayout;

public class Layout2 extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {


        LinearLayout linearLayout = new LinearLayout(getApplicationContext());
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        params.setMargins(50, 20, 50, 50);


If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.


Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

50% Discounts On Projects

Do you have a custom project with a clear requirements, we are currently giving a 50% discount on all projects.

We will work with you to ensure we deliver what you need.

If you have questions or want to chat with us, you can reach us on Skype (henry.tochi)

Languages and technologies we use - Java, Kotlin, Laravel, Vue, React, Flutter, Swift and more.