What is the Difference Between GRAVITY and LAYOUT_GRAVITY in Android

 Android application created using a layout with class. it is layout content with different layouts, linear layouts, relative layouts, etc. There are common attributes among all the layouts and there are other attributes that are specific to that layout of mobile application.


Different between GRAVITY and LAYOUT_GRAVITY
  1. Gravity doesn’t work well on a relativelayout but can be user full with a linearLayout. 
  2. Layout_gravity two type of attributes center_horizontal and center_vertical.

Step-By-Step Implementation


Step 1:Create a new project in Android or you can use an already created project. 



or



Step 2: Create an XML file named acitivity_main.xml of the layout folder.


Step 3: acitivity_main.xml file continent Design of screen like button, text view, etc.
 <?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">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:baselineAligned="false"
android:orientation="vertical"
android:layout_margin="20dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="gravity"
android:textSize="30sp"
android:textStyle="bold"/>

<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#98FAE1"
android:gravity="left"
android:layout_margin="5dp"
android:text="left"
android:textSize="20sp"/>

<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#DFBFEF"
android:gravity="center_horizontal"
android:layout_margin="5dp"
android:text="center_horizontal"
android:textSize="20sp"/>

<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#D8CAB4"
android:gravity="right"
android:text="right"
android:layout_margin="5dp"
android:textSize="20sp"/>

<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#DE1947"
android:gravity="center"
android:text="center"
android:layout_margin="5dp"
android:textSize="20sp"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_margin="20dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="layout_gravity"
android:textSize="30sp"
android:textStyle="bold"/>

<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="left"
android:background="#97BA90"
android:text="left"
android:layout_margin="5dp"
android:textSize="20sp"/>

<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:background="#DCE38D"
android:layout_margin="5dp"
android:text="center_horizontal"
android:textSize="20sp"/>

<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:background="#49E3F4"
android:text="right"
android:layout_margin="5dp"
android:textSize="20sp"/>

<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#E6A3BA"
android:text="center"
android:layout_margin="5dp"
android:textSize="20sp"/>

</LinearLayout>

 </LinearLayout>
Step 3: Output of above example


Happy coding!













365Bloggy May 21, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive