0% found this document useful (0 votes)
16 views50 pages

Mobile OS and Applications

TextView 6 android:id- 6 android: layout_height- 6 Height is the attribute which is used to set the height of the TextView. It can be like wrap_content that means TextView will be the same size as the text that is given by the user, match_parent that means TextView will consume the size of the whole screen. It is not related to the text of the user, and we can also define the size of the text according to the user in DPs. 6 android:layout_width- 6 android:layout_centerInParent- 6 android: text-

Uploaded by

20bcm020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views50 pages

Mobile OS and Applications

TextView 6 android:id- 6 android: layout_height- 6 Height is the attribute which is used to set the height of the TextView. It can be like wrap_content that means TextView will be the same size as the text that is given by the user, match_parent that means TextView will consume the size of the whole screen. It is not related to the text of the user, and we can also define the size of the text according to the user in DPs. 6 android:layout_width- 6 android:layout_centerInParent- 6 android: text-

Uploaded by

20bcm020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

20BCM020 ​ MOSA Assignment -1

Mobile OS and
Applications
CSI0802

Assignment -1 :

Made by:
NAME :
ROLL NUMBER :
20BCM020 ​ MOSA Assignment -1

TextView
TextView is the user interface which displays the text message on the screen to
the user. It is based on the layout size, style, and color, etc. TextView is used to set
and display the text according to our specifications. Android TextView is the
subclass of View class.

In Android, when we create a new project with a specific name Android provides
us a TextView in our activity_main.xml file, and the second file is the
MainActivity.java file for the coding of Java programming language.
Properties of TextView:

android:id-
ID is an attribute used to define a TextView uniquely.

android: layout_height-

Height is the attribute which is used to set the height of the TextView. It can
be like wrap_content that means TextView will be the same size as the text
that is given by the user, match_parent that means TextView will consume
the size of the whole screen. It is not related to the text of the user, and we
can also define the size of the text according to the user in DPs.

android:layout_width-
Width is the attribute which is used to set the width of the TextView. Like height
attribute, it also can be like wrap_content, which means TextView will be the
same size as the text that is given by the user, match_parent which means
TextView will consume the size of the whole screen. It is not related to the text of
the user, and we can also define the size of the text according to the user in DPs.

android:layout_centerInParent-
centerInParent is the attribute which is used to set the text in the center of the
screen. It is an optional attribute of the TextView. It is set as true and false by the
user.

android: text-
This attribute handles the text that is given by the user in the form of the string.
20BCM020 ​ MOSA Assignment -1

android: hint-
This attribute is used when there is no text is given by the user, in this hint
attribute will show how the text will display on the screen.

android:inputType-
This attribute is used to set the type of the TextView. The type of text can be
phone number, eMail, and Password, etc.

android:textSize-
This attribute is used to set the size of the TextView. The size of the TextView
given in the size of sp.

android:textStyle-
This attribute is used to set the style of the TextView. The style of the TextView
given as bold, italic, etc.

android:textColorHighlight-
This attribute is used to set the color of the text selection highlight.

XML file:

<TextView
android:id="@+id/tview"
android:layout_width="391dp"
android:layout_height="89dp"
android:text="MOSA ASSIGNMENT"
android:textColor="#D61717"
android:textColorHighlight="#3F51B5"
android:textSize="40dp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.431"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
20BCM020 ​ MOSA Assignment -1

app:layout_constraintVertical_bias="0.175" />

Java file:

public class MainActivity extends AppCompatActivity {


TextView tvw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvw = (TextView)findViewByID(R.id.tview);

}
}
20BCM020 ​ MOSA Assignment -1

Plain Text
EditText (or PlainText) is used in the app whenever you need input from the user
side and proceed with its text (or value) in your app.

EditText (or PlainText) is not only used to get plain text in your application, but
even you can use it to get values such as email, number, password, etc. To get the
value of each type in EditText, you must specify its input type in its inputType
attribute.
For example, to input plain text, set the inputType attribute to "text", and to
input only numeric values, set the inputType attribute to "number". It is the
predefined subclass of TextView that includes rich editing capabilities.

Attributes:

Android:background:

This is a drawable to use as the background.

android:contentDescription:
This defines text that briefly describes content of the view.

Android:id

This supplies an identifier name for this view.

android:onClick:

This is the name of the method in this View's context to invoke when the
view is clicked.

Android:visibility:
This controls the initial visibility of the view.

Android:editable:
If set, specifies that this TextView has an input method.
20BCM020 ​ MOSA Assignment -1

Android:text :
This is the Text to display.

XML file:
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="389dp"
android:layout_height="63dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Enter subject here:"
android:textColor="#4CAF50"
android:textSize="30sp"
android:textStyle="bold|italic"
tools:layout_editor_absoluteX="9dp"
tools:layout_editor_absoluteY="47dp" />

Java file:
public class MainActivity extends AppCompatActivity {
EditText edt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt = (TextView)findViewByID(R.id.editTextTextPersonName);
edt.setOnClickListener(new View.OnClickListener() {
20BCM020 ​ MOSA Assignment -1

Password
An input field which can accept alphanumeric as well as only numeric value as an
input for password. It is the predefined subclass of TextView that includes rich
editing capabilities.

XML file:
<EditText
android:id="@+id/editTextTextPassword"
android:layout_width="273dp"
android:layout_height="105dp"
android:textSize="40dp"
android:ems="10"
android:inputType="textPassword"
android:text="Password:"
tools:layout_editor_absoluteX="66dp"
tools:layout_editor_absoluteY="105dp" />
20BCM020 ​ MOSA Assignment -1

Java file:
public class MainActivity extends AppCompatActivity {
EditText pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pass = (TextView)findViewByID(R.id.editTextTextPassword);
pass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Button
A user interface element the user can tap or click to perform an action. Button is
subclass of TextView class and CompoundButton is the subclass of Button class.
To display a button in an activity, add a button to the activity's layout XML file:
<Button
android:id="@+id/button"
android:layout_width="118dp"
android:layout_height="63dp"
android:text="Click"
android:textColor="#F8E00F"
android:textColorHighlight="#E91E63"
android:textColorHint="#9A5B5B"
android:textStyle="bold"
20BCM020 ​ MOSA Assignment -1

tools:layout_editor_absoluteX="142dp"
tools:layout_editor_absoluteY="362dp" />

Toggle Button
Toggle Button allows the user to change the setting between two states. Android
ToggleButton is the subclass of CompoundButton class.

XML file:
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="150dp"
android:layout_height="64dp"
android:text="ToggleButton"
tools:layout_editor_absoluteX="146dp"
tools:layout_editor_absoluteY="474dp" />

Java file:
public class MainActivity extends AppCompatActivity {
ToggleButton tgb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tgb = (TextView)findViewByID(R.id.toggleButton);
tgb.setOnCheckedChangeListener(CompoundButton::setChecked);
20BCM020 ​ MOSA Assignment -1
20BCM020 ​ MOSA Assignment -1

ImageView:
ImageView class is used to display any kind of image resource in the android
application either it can be android. graphics. Bitmap or android. Graphics.

android:maxHeight - An optional argument to supply a maximum


height for this view. ​
android:maxWidth- An optional argument to supply a maximum width
for this view. ​
android:scaleType - Controls how the image should be resized or moved
to match the size of this ImageView. ​
Android:src - Sets a drawable as the content of this ImageView. ​
Android:tint- The tinting color for the image. ​
android:tintMode- Blending mode used to apply the image tint.

XML file:

Output:

CheckBox:
Checkboxes allow the user to select one or more options from a set. Because a set
of checkbox options allows the user to select multiple items, each checkbox is
managed separately and you must register a click listener for each one.
20BCM020 ​ MOSA Assignment -1

android: id – It set a unique identity​


android: checked – It specifies the current state of the CheckBox.​
android: gravity – It sets the alignment of the CheckBox.​
android: text – It sets the text for the Radio CheckBox.​
android:textColor – It sets the color of the text in the CheckBox.

XML file:

Output:

Event listener on Checkbox


20BCM020 ​ MOSA Assignment -1

RadioButton:
RadioButton is a two states button which is either checked or unchecked. If a
single radio button is unchecked, we can click it to make checked radio button.
Once a radio button is checked, it cannot be marked as unchecked by user.
RadioButton is generally used with RadioGroup.
20BCM020 ​ MOSA Assignment -1

android: id – It set a unique identity.​


android: checked – It specifies the current state of the button.​
android: gravity – It sets the alignment of the text.​
android: text – It sets the text for the Radio Button.​
android:textColor – It sets the color of the text.

XML file:

Output:

Spinner:
Android Spinner is a view similar to the dropdown list which is used to select one
option from the list of options. It provides an easy way to select one item from the
list of items and it shows a dropdown list of all values when we click on it.
20BCM020 ​ MOSA Assignment -1

android:id - Used to specify the id of the view.​


android:textAlignment - Used to the text alignment in the dropdown
list.​
android:background - Used to set the background of the view.​
android:padding - Used to set the padding of the view.​
android:visibility - Used to set the visibility of the view.​
android:gravity​ - Used to specify the gravity of the view like center,
top, bottom, etc

XML file:

Output:

Event listener on spinner


20BCM020 ​ MOSA Assignment -1

Picker:
Android provides controls for the user to pick a time or pick a date as
ready-to-use dialogs. Each picker provides controls for selecting each part of the
time (hour, minute, AM/PM) or date (month, day, year).

android:id - It is used to uniquely identify the control​


android:datePickerMode - It is used to specify datepicker mode either
spinner or calendar​
android:background - It is used to set the background color for the date
picker.​
android:padding - It is used to set the padding for left, right, top or
bottom of the date picker.

XML file:
20BCM020 ​ MOSA Assignment -1

JAVA file

Output:

ScrollView
In Android, a ScrollView is a view group that is used to make vertically scrollable
views. A scroll view contains a single direct child only. In order to place multiple
views in the scroll view, one needs to make a view group as a direct child and then
we can define many views inside it. A ScrollView supports Vertical scrolling only,
so in order to create a horizontally scrollable view, HorizontalScrollView is used.
20BCM020 ​ MOSA Assignment -1

Attributes:

android:fillViewport-
Defines whether the scrollview should stretch its content to fill the viewport.

android:measureAllChildren-
Determines whether to measure all children or just those in the VISIBLE or
INVISIBLE state when measuring

android:layoutMode-
Defines the layout mode of this ViewGroup.

android:persistentDrawingCache-
Defines the persistence of the drawing cache.

android:splitMotionEvents-’
Sets whether this ViewGroup should split MotionEvents to separate child views
during touch event dispatch.

XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context="com.example.assignment.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Vertical ScrollView example"
20BCM020 ​ MOSA Assignment -1

android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
<ScrollView android:layout_marginTop="30dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 3" />
20BCM020 ​ MOSA Assignment -1

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 4" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 5" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 6" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 7" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 8" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 9" />
<Button
20BCM020 ​ MOSA Assignment -1

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 10" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 11" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 12" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 13" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 14" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 15" />
<Button
android:layout_width="fill_parent"
20BCM020 ​ MOSA Assignment -1

android:layout_height="wrap_content"
android:text="Button 16" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 17" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 18" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 19" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 20" />
</LinearLayout>
</ScrollView>
</RelativeLayout>

JAVA CODE:
package com.example.assignment;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
20BCM020 ​ MOSA Assignment -1

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Output:
20BCM020 ​ MOSA Assignment -1

ImageView:
ImageView class is used to display any kind of image resource in the android
application either it can
be android.graphics.Bitmap or android.graphics.drawable.Drawable (it is a
general abstraction for anything that can be drawn in Android).

To add an ImageView to the application we first need to add that file in resources
Open app/res/drawable and paste the image in that folder

Attributes:
android:adjustViewBounds-Set this to true if you want the ImageView to
adjust its bounds to preserve the aspect ratio of its drawable.

android:baseline-The offset of the baseline within this view.

android:maxHeight-An optional argument to supply a maximum height for


this view.

android:maxWidth-An optional argument to supply a maximum width for this


view.

android:src-Sets a drawable as the content of this ImageView.

android:tint-The tinting color for the image.

android:tintMode-Blending mode used to apply the image tint.

XML CODE:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
20BCM020 ​ MOSA Assignment -1

tools:context=".MainActivity">
<ImageView
android:id="@+id/GfG_full_logo"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.201"
app:srcCompat="@drawable/zoro" />
</androidx.constraintlayout.widget.ConstraintLayout>

JAVA FILE:
package com.example.assignment;
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);
20BCM020 ​ MOSA Assignment -1

}
}

OUTPUT:

ListView:
A ListView is a type of AdapterView that displays a vertical list of scroll-able
views and each view is placed one below the other. Using adapter, items are
inserted into the list from an array or database.
20BCM020 ​ MOSA Assignment -1

Attributes:

android:divider-A colour or drawable to separate list items.

android:dividerHeight-Divider’s height.

android:entries-Reference to an array resource that will populate the


ListView.

android:footerDividersEnabled-When set to false, the ListView will


not draw the divider before each footer view.

android:headerDividersEnabled-When set to false, the ListView will


not draw the divider before each header view.

XML FILE:
<?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">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
20BCM020 ​ MOSA Assignment -1

JAVA FILE:
package com.example.assignment;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView l;
String games[]
= { "GTA 5", "WITCHER 3",
"DOTA2", "LOL",
"CSGO", "CALL OF DUTY",
"UNCHARTED", "INFAMOUS",
"GOD OF WAR" };
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = findViewById(R.id.list);
ArrayAdapter<String> arr;
arr
= new ArrayAdapter<String>(
20BCM020 ​ MOSA Assignment -1

this,
androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,games
);
l.setAdapter(arr);
}
}

OUTPUT:
20BCM020 ​ MOSA Assignment -1

TableLayout:
Android TableLayout is going to arrange groups of views into rows and columns.
You will use the <TableRow> element to build a row in the table. Each row has
zero or more cells; each cell can hold one View object.

Attributes:
android:id-This is the ID which uniquely identifies the layout.
android:collapseColumns
This specifies the zero-based index of the columns to collapse. The column
indices must be separated by a comma: 1, 2, 5.
android:shrinkColumns
The zero-based index of the columns to shrink. The column indices must be
separated by a comma: 1, 2, 5.
android:stretchColumns
The zero-based index of the columns to stretch. The column indices must be
separated by a comma: 1, 2, 5.

XML FILE:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
20BCM020 ​ MOSA Assignment -1

android:layout_column="1" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textClock"
android:layout_column="2" />
</TableRow>
<TableRow>
<TextView
android:text="First Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="200px"
tools:ignore="SpeakableTextPresentCheck" />
</TableRow>
<TableRow>
<TextView
android:text="Last Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />
20BCM020 ​ MOSA Assignment -1

<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button"
android:layout_column="2" />
</TableRow>
</TableLayout>

JAVA FILE:
package com.example.assignment;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
20BCM020 ​ MOSA Assignment -1

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

OUTPUT:
20BCM020 ​ MOSA Assignment -1

FrameLayout:
It is designed to block out an area on the screen to display a single item.
Generally, FrameLayout should be used to hold a single child view, because it can
be difficult to organize child views in a way that's scalable to different screen sizes
without the children overlapping each other.

Attributes:
android:id
This is the ID which uniquely identifies the layout.
android:foreground
This defines the drawable to draw over the content and possible values may be a
color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
android:foregroundGravity
Defines the gravity to apply to the foreground drawable. The gravity defaults to
fill. Possible values are top, bottom, left, right, center, center_vertical,
center_horizontal etc.
android:measureAllChildren
Determines whether to measure all children or just those in the VISIBLE or
INVISIBLE state when measuring. Defaults to false.

XML FILE:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ImageView
android:src="@drawable/zoro"
android:scaleType="fitCenter"
android:layout_height="550px"
android:layout_width="550px"/>
20BCM020 ​ MOSA Assignment -1

<TextView
android:text="Frame Demo"
android:textSize="30px"
android:textStyle="bold"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
</FrameLayout>

JAVA FILE:

package com.example.assignment;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {


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

}
20BCM020 ​ MOSA Assignment -1

OUTPUT:

Linear layout:
LinearLayout is a view group that aligns all children in a single direction,
vertically or horizontally.

Basic Code:
<?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">

<Button
20BCM020 ​ MOSA Assignment -1

android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
</LinearLayout>

Output:

Attributes:

android:id
Uniquely identifies each layout.

android:orientation
We can specify the layout direction with this attribute.
It can be set as horizontal or vertical. By default it is horizontal.
20BCM020 ​ MOSA Assignment -1

Horizontal and Vertical-

Setting the background color to understand the boundaries-


20BCM020 ​ MOSA Assignment -1

android:layout_weight
This assigns importance on how much screen a View can relatively occupy as
compared to others.
The default weight of all the elements is 1.
If the weight of some elements is specified and of some is not specified, then the
elements without specification are given the minimum amount of space that they
need and the rest of the space is given to the element with specified weight.
20BCM020 ​ MOSA Assignment -1

When weight is specified to all the elements, it is divided in the specified ratio.

Assigning weights as 0.5, 0.5 and 50,50 will yield the same results.

Weights in vertical orientation-


20BCM020 ​ MOSA Assignment -1

Nesting a horizontal and vertical linear layout inside a vertically oriented linear
layout-

android:gravity
Specifies how an object should position its content, on both the X and Y axes,
within its own bounds.
Normal gravity-
20BCM020 ​ MOSA Assignment -1

gravity-right

gravity-center vertical
20BCM020 ​ MOSA Assignment -1

gravity-center

android:divider
Drawable to use as a vertical divider between buttons.
20BCM020 ​ MOSA Assignment -1

Relative layout:
RelativeLayout lets child views specify their position relative to the parent view or
to each other (specified by ID). So you can align two elements by right border, or
make one below another, centered in the screen, centered left, and so on.

By default, all child views are drawn at the top-left of the layout, so you must
define the position of each view using the various layout properties available from
RelativeLayout.LayoutParams.
20BCM020 ​ MOSA Assignment -1

Attributes:

android:layout_below
Positions the top edge of this view below the given anchor view ID and must be a
reference to another resource, in the form "@[+][package:]type:name".

Similarly we can work with android:layout_above.

android:layout_centerHorizontal
If true, centers this child horizontally within its parent. Must be a boolean value,
either "true" or "false".
20BCM020 ​ MOSA Assignment -1

android:layout_toRightOf
Positions the left edge of this view to the right of the given anchor view ID and
must be a reference to another resource, in the form "@[+][package:]type:name".

Similarly we can work with android:layout_toLeftOf, android:layout_toStartOf


and android:layout_toEndOf
20BCM020 ​ MOSA Assignment -1

android:layout_alignParentBottom
If true, makes the bottom edge of this view match the bottom edge of the parent.
Must be a boolean value, either "true" or "false".

Similarly we can work with android:layout_alignParentTop,


android:layout_alignParentLeft, android:layout_alignParentRight,
android:layout_alignParentStart and android:layout_alignParentEnd.

android:gravity
This specifies how an object should position its content, on both the X and Y axes.
Possible values are top, bottom, left, right, center, center_vertical,
center_horizontal etc.

android:ignoreGravity
This indicates what view should not be affected by gravity.

Constraint Layout:
ConstraintLayout allows you to create large and complex layouts with a flat view
hierarchy (no nested view groups).

It's similar to RelativeLayout in that all views are laid out according to
relationships between sibling views and the parent layout, but it's more flexible
than RelativeLayout and easier to use with Android Studio's Layout Editor.
20BCM020 ​ MOSA Assignment -1

Attributes:

android:layout_margin​
Specifies extra space on the left, top, right and bottom sides of this view.

android:layout_marginBottom​
Specifies extra space on the bottom side of this view.

android:layout_marginEnd​
Specifies extra space on the end side of this view.

android:layout_marginHorizontal​
Specifies extra space on the left and right sides of this view.

android:layout_marginLeft​
Specifies extra space on the left side of this view.

android:layout_marginRight​
Specifies extra space on the right side of this view.

android:layout_marginStart​
Specifies extra space on the start side of this view.

android:layout_marginTop​
Specifies extra space on the top side of this view.

android:layout_marginVertical​
Specifies extra space on the top and bottom sides of this view.
20BCM020 ​ MOSA Assignment -1

Chains

Chains are one of the coolest features of the constraint layout. They simplify the
alignment of views and help them utilize the space available effectively as desired.
There are four chain styles, compressed, spread, weighted, and packed.
20BCM020 ​ MOSA Assignment -1

Guideline
A guideline is an invisible helper that simplifies the layout by offering a central
line of alignment especially if the views have duplicate margin values.

Its position on screen can be specified in DP (start or end) or percentage


regardless of its orientation.

You might also like