unit 3
unit 3
UNIT-3
ViewGroup: A ViewGroup act as a base class for layouts and layouts parameters that hold other Views or
ViewGroups and to define the layout properties. They are Generally Called layouts.
The Android framework will allow us to use UI elements or widgets in two ways:
• Use UI elements in the XML file
• Create elements in the Kotlin file dynamically
• Android ListView: ListView is a ViewGroup, used to display scrollable lists of items in a single
column.
• Android GridView: GridView is a ViewGroup that is used to display a scrollable list of items in a
grid view of rows and columns.
Mobile Computing and App Development
• Android CardView: CardView is a new widget in Android that can be used to display any sort of
data by providing a rounded corner layout along with a specific elevation.
• Android TextView: Android TextView is simply a view that are used to display the text to the user
and optionally allow us to modify or edit it.
• Android Button: A Button is a user interface that is used to perform some action when clicked or
tapped.
• Android ToggleButton: ToggleButton is just like a switch containing two states either ON or OFF
which is represented using boolean values true and false respectively.
• Android CheckBox: A CheckBox is a special kind of button in Android which has two states either
checked or unchecked.
ListView:
Android ListView is a ViewGroup which is used to display the list of items in multiple rows and contains an
adapter which automatically inserts the items into the list.
The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into
the list for the desired result. So, it is main source to pull data from strings.xml file which contains all the
required strings in Kotlin or xml files.
Android Adapter:
Adapter holds the data fetched from an array and iterates through each item in data set and generates the
respective views for each item of the list. So, we can say it act as an intermediate between the data sources
and adapter views such as ListView, Gridview.
• ArrayAdapter: It always accepts an Array or List as input. We can store the list items in the
strings.xml file also.
• CursorAdapter: It always accepts an instance of cursor.
Mobile Computing and App Development
• SimpleAdapter: It mainly accepts a static data defined in the resources like array or database.
• BaseAdapter: It is a generic implementation for all three adapter types and it can be used in the
views according to our requirements.
activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" android:orientation="vertical">
<ListView
android:id="@+id/userlist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
GridView:
A Grid View is a type of adapter view that is used to display the data in the grid layout format. For setting the
data to the grid view adapter is used with the help of the setAdapter() method. This adapter helps to set the
data from the database or array list to the items of the grid view. In this article, we will take a look at How to
implement Grid View in Android using Kotlin language. Working with the activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_margin="5dp"
app:cardCornerRadius="5dp" app:cardElevation="5dp">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
Mobile Computing and App Development
android:id="@+id/idIVCourse"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_margin="5dp" android:padding="4dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/idTVCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp" android:padding="4dp"
android:text="@string/app_name"
android:textAlignment="center"
android:textColor="@color/black" />
</LinearLayout>
</androidx.cardview.widget.CardView>
RecyclerView:
A RecyclerView is an advanced version of ListView with improved performance. When you have a long list
of items to show you can use RecyclerView. It has the ability to reuse its views. In RecyclerView when the
View goes out of the screen or not visible to the user it won’t destroy it, it will reuse these views. This
feature helps in reducing power consumption and providing more responsiveness to the application.
• onCreateViewHolder(): This function sets the views to display the items.
• onBindViewHolder(): This function is used to bind the list items to our widgets such as TextView,
ImageView, etc.
• getItemCount(): It returns the count of items present in the list.
<LinearLayout
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"
android:orientation="vertical" tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:itemCount="5"
tools:listitem="@layout/card_view_design" />
</LinearLayout>
CardView:
CardView is a new widget in Android that can be used to display any sort of data by providing a rounded
corner layout along with a specific elevation. CardView is the view that can display views on top of each
other. The main usage of CardView is that it helps to give a rich feel and look to the UI design. This widget
can be easily seen in many different Android Apps. CardView can be used for creating items in listview or
inside Recycler View. The best part about CardView is that it extends Framelayout and it can be displayed on
all platforms of Android.
Some important attributes of Cardview are :
1.cardBackgroundColor : Used for setting up the background-color of the card.
2.cardElevation : Defines the elevation (the process of moving to a higher place or more important
position) of the card. It’s value should not be quite large else the design may not look good.
3.cardCornerRadius : It sets radius around the corners of the card. More the value of this attribute more
circular the edges would appear in the card.
4.cardUseCompactPadding : It has two values true and false. By default, the cardview is set to (0,0) top left
corner of the screen. And if this attribute is set to true then the card will set padding for itself so that our UI
looks good. This case is helpful in the scenarios when our gravity is not set to center or any other parameters.
Working with XML:
<?xml version="1.0" encoding="utf-8"?>
Mobile Computing and App Development
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="10dp" app:cardCornerRadius="20dp"
android:layout_margin="10dp"
app:cardBackgroundColor="@color/white"
app:cardMaxElevation="12dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:src="@drawable/gfgimage"
android:layout_margin="10dp"
android:id="@+id/img"
android:contentDescription="@string/app_name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:textSize="20sp" android:textStyle="bold"
/>
</androidx.cardview.widget.CardView>
Mobile Computing and App Development
</RelativeLayout>
TextView:
Android TextView is simply a view that are used to display the text to the user and optionally allow us to
modify or edit it.
Different attributes of TextView:
Attributes Description
ndroid:text Sets text of the Textview
android:id Gives a unique ID to the Textview
android:cursorVisible Use this attribute to make cursor visible or invisible. Default value is
visible.
android:autoLink This attribute is used to automatically detect url or emails and show
it as clickable link.
activity_main.xml file;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<TextView
android:id="@+id/text_view_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/text_view"
android:textColor="#008000"
android:textSize="40dp"
android:textStyle="bold"/>
</LinearLayout>
Mobile Computing and App Development
Button:
A Button is a user interface that is used to perform some action when clicked or tapped. It is a very common
widget in Android and developers often use it.
Used to the display style of the text like Bold, Italic, etc.
android:textStyle
android:gravity Used to specify the gravity of the view like center, top, bottom, etc
activity_main.xml file :
<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"
android:background="#168BC34A" tools:context=".MainActivity">
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#4CAF50"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:text="@string/btn"
android:textColor="@android:color/background_light"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
Mobile Computing and App Development
app:layout_constraintEnd_toEndOf="parent"
</androidx.constraintlayout.widget.ConstraintLayout>
RadioGroup:
RadioGroup class of Kotlin programming language is used to create a container which holds multiple
RadioButtons. The RadioGroup class is beneficial for placing a set of radio buttons inside it because this class
adds multiple-exclusion scope feature to the radio buttons. This feature assures that the user will be able to
check only one of the radio buttons among all which belongs to a RadioGroup class. If the user checks another
radio button, the RadioGroup class unchecks the previously checked radio button.
android:onClick It’s a name of the method to invoke when the radio button clicked.
android:checkedButton Stores id of child radio button that needs to be checked by default within this
radio group
activity_main.xml file:
<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"
android:background="#168BC34A" tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
Mobile Computing and App Development
android:text="@string/Heading"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark
" android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.19" />
<RadioGroup android:id="@+id/radioGroup1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#024CAF50"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.24000001">
<RadioButton
android:id="@+id/radioButton1"
Mobile Computing and App Development
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="@string/RadioButton1"
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="@string/radioButton2"
android:id="@+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="@string/radioButton3"
android:id="@+id/radioButton4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
Mobile Computing and App Development
android:text="@string/radioButton4"
android:textSize="24sp" />
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="@string/radioButton5"
android:textSize="24sp" />
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
ToggleButton:
ToggleButton is just like a switch containing two states either ON or OFF which is represented using boolean
values true and false respectively. ToggleButton unlike switch does not have a slider interface i.e we cannot
slide to change the states.
Attribute Description
Attribute Description
Mobile Computing and App Development
activity_main.xml:
<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" tools:context=".MainActivity">
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
CheckBox:
A CheckBox is a special kind of button in Android which has two states either checked or unchecked. The
Checkbox is a very common widget to be used in Android and a very good example is the “Remember me”
Mobile Computing and App Development
option in any kind of Login activity of an app which asks the user to activate or deactivate that service. There
are many other uses of the CheckBox widget like offering a list of options to the user to choose from and the
options are mutually exclusive i.e., the user can select more than one option. This feature of the CheckBox
makes it a better option to be used in designing multiple-choice questions application or survey application in
android.
android:padding Used to adjust the left, right, top and bottom padding of the CheckBox
Mobile Computing and App Development
activity_main.xml file:
<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"
<TextView android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="@string/Heading"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="36sp" android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent
" app:layout_constraintEnd_toEndOf="parent"
Mobile Computing and App Development
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17000002" />
<LinearLayout
android:id="@+id/checkBox_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.18">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="@string/checkBox1_text"
android:textSize="18sp" android:padding="7dp"/>
Mobile Computing and App Development
<CheckBox android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="@string/checkBox2_text"
android:textSize="18sp"
android:padding="7dp"/>
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="@string/checkBox3_text"
android:textSize="18sp" android:padding="7dp"/>
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="@string/checkBox4_text" android:textSize="18sp"
android:padding="7dp"/>
Mobile Computing and App Development
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="@string/checkBox5_text"
android:textSize="18sp" android:padding="7dp"/>
</LinearLayout>
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#AB4CAF50"
android:fontFamily="@font/roboto"
android:text="@string/submitButton"
android:textSize="18sp" android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox_container"
app:layout_constraintVertical_bias="0.23000002" />
Mobile Computing and App Development
</androidx.constraintlayout.widget.ConstraintLayout>
Android devices have two main screen orientations: portrait (vertical) and landscape (horizontal).
To change the screen orientation on your Android device:
Open the Settings app.
Go to Accessibility.
Turn on Auto-rotate screen.
You can also lock the screen so it stays in either portrait or landscape mode.
When the screen orientation changes, the current screen (or activity) closes and reopens to fit the new
direction. Here are two ways to make your app look good in both orientations:
Anchor controls: Position buttons, images, and other elements so they stay in specific spots on the
screen, no matter which way it’s rotated.
Define separate layouts: Create two different layouts—one for portrait mode and one for
landscape mode—to make sure everything looks nice in each direction.
Example :
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-Define potrait orientation for Main activity-->
<activity
android:name="com.geeksforgeeks.screenorientation.MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Mobile Computing and App Development
ViewGroup objects programmatically in the Kotlin file. Below is the example of creating a layout
using LinearLayout to hold an EditText and a Button in an activity programmatically. Kotlin import
android.appcompat.app.AppCompatActivity
android:layout_width Used to declare the width of View and ViewGroup elements in the
layout.
android:layout_height Used to declare the height of View and ViewGroup elements in the
layout.
android:layout_marginLeft Used to declare the extra space used on the left side of View and
ViewGroup elements.
android:layout_marginRight Used to declare the extra space used on the right side of View and
ViewGroup elements.
android:layout_marginTop Used to declare the extra space used in the top side of View and
ViewGroup elements.
android:layout_marginBottom Used to declare the extra space used in the bottom side of View and
ViewGroup elements.
android:layout_gravity Used to define how child Views are positioned in the layout.