Develop a program to implement linear layout and absolute layout.
Layouts which are subclasses of View Group class and a typical layout defines the visual structure for an Android user interface and can be created either at run time using View/View Group objects or you can declare your layout using simple XML file main_layout.xml which is located in the res/layout folder of your project. layouts defined in XML file. A layout may contain any type of widgets such as buttons, labels, textboxes etc. Layout Attributes Each layout has a set of attributes which define the visual properties of that layout. There are few common attributes among all the layouts and there are other attributes which are specific to that layout. Types of layouts are Linear and Absolute layouts.
1. Write a program to place Name, Age and mobile number linearly (Vertical) on the display screen using Linear layout.
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student
Name"
android:textSize="25dp"
android:textStyle="bold"
android:layout_margin="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yash Sandip
Aher"
android:layout_margin="20dp"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:textSize="25dp"
android:textStyle="bold"
android:layout_margin="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="19"
android:layout_margin="20dp"
android:textSize="20dp"
android:paddingLeft="170dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile no"
android:textSize="25dp"
android:textStyle="bold"
android:layout_margin="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9552713621"
android:layout_margin="20dp"
android:textSize="20dp"
android:paddingLeft="60dp"/>
</LinearLayout>
</LinearLayout>
Java -
package
com.example.practical_5_name_age_mobile_display;
import
androidx.appcompat.app.AppCompatActivity;
import
android.os.Bundle;
public
class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
OUTPUT -
2. Write a program to place Name, Age and mobile number centrally on the display screen using Absolute layout.
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center">
<AbsoluteLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Name"
android:layout_x="20dp"
android:layout_y="50dp"
android:textSize="25dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yash Sandip
Aher"
android:layout_x="220dp"
android:layout_y="55dp"
android:textSize="22dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:layout_x="70dp"
android:layout_y="100dp"
android:textSize="25dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="19"
android:layout_x="280dp"
android:layout_y="105dp"
android:textSize="22dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile No"
android:layout_x="40dp"
android:layout_y="160dp"
android:textSize="25dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9552713621"
android:layout_x="240dp"
android:layout_y="165dp"
android:textSize="22dp"/>
</AbsoluteLayout>
</LinearLayout>
JAVA –
package
com.example.practical_5_absolute_layout;
import
androidx.appcompat.app.AppCompatActivity;
import
android.os.Bundle;
public
class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Tags -
"Practical No. 5: Develop a program to implement linear layout and absolute layout."
"Develop a program to implement linear layout and absolute layout"
"Absolute Layout in Android with Example"
"MAD Practical No 5"
"Linear Layout Tutorial With Examples In Android"
"Linear Layout in Android with Program"
"linear layout and absolute layout in android"