How to Set Padding in LinearLayout in Android Programmatically

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

Below is a step by step source code to set padding in LinearLayout in Android programmatically.

activity_layout3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearlayout"
    android:background="@color/colorPrimary"
    tools:context=".layout.Layout3">

    <ImageView
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:src="@drawable/ext"/>

    <ImageView
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:src="@drawable/ext"/>

</LinearLayout>

Layout3.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;

public class Layout3 extends AppCompatActivity {

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

        LinearLayout linearLayout = findViewById(R.id.linearlayout);
        linearLayout.setPadding(50, 10,50, 10);
    }
}

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