SlideShare a Scribd company logo
1 of 51
Android Mobile App Development with Kotlin
 Kotlin is multi-platform language, i.e. easily executable on a
Java Virtual Machine.
 Kotlin is sponsored by Google, announced as one of the
official languages for Android Development in 2017.
TextView in Kotlin
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.
Following steps are used to create TextView in Kotlin:
1. Add a TextView in activity_main.xml file inside LinearLayout.
2. Add attributes like text, textColor, textSize, textStyle in the
activity_main.xml file.
3. Open MainActivity.kt file and set OnClickListener for the textView to
show the Toast message.
TextView
TextView
TextView
TextView
TextView
TextView
TextView
Modify the strings.xml file
 We can add strings in the strings.xml file and use in the
other files easily by calling them with their names.
<resources>
<string name="app_name">TextViewInKotlin</string>
<string name="text_view">Arsi University</string>
<string name="text_on_click">COMPUTER SCIENCE Department</string>
</resources>
activity_main.xml fil
Open activity_main.xml file and create a TextView using id textView.
<?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">
<!--EditText with id editText-->
<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>
How to reference Textview in .kt file
 Open MainActivity.kt file and get the reference of TextView defined
in the layout file.
// finding the textView
val textView = findViewById(R.id.text_view_id) as TextView
Setting the on click listener to the button
textView?.setOnClickListener { Toast.makeText(this@MainActivity,
"COMPUTER SCIENCE Department", Toast.LENGTH_LONG).show() }
MainActivity.kt file
 Open app/src/main/java/yourPackageName/MainActivity.kt
to get the reference of TextView.
package com.example.myfirstkotlinapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//accessing our textview from layout
val textView = findViewById<TextView>(R.id.text_view_id) as TextView
textView?.setOnClickListener{ Toast.makeText(this@MainActivity,
R.string.text_on_click, Toast.LENGTH_LONG).show() }
}
}
AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.geeksforgeeks.myfirstkotlinapp">
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Button in Android
 In Android applications, 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.
 The Class Hierarchy of the Button Class in Kotlin
XML Attributes of Button Widget
XML Attributes of Button Widget
Step by step demonstration of creating a Button
Step 1: Create a new project
1. Click on File, then New => New Project.
2. Choose “Empty Activity” for the project template.
3. Select language as Kotlin.
4. Select the minimum SDK as per your need.
Step 2: Modify the strings.xml file
Navigate to the strings.xml file under the “values” directory of
the resource folder. This file will contain all strings that are
used in the Application.
Example
<resources>
<string name="app_name">GfG | Button In Kotlin</string>
<string name="btn">Button</string>
<string name="message">Hello IT students!! This is a Button.</string>
</resources>
Step 3: Modify the activity_main.xml file
Add a button widget in the layout of the activity. Below is the code of
the activity_main.xml file which does the same.
Spinner in Kotlin
 It is a view similar to a 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.
 The default value of the android spinner will be the
currently selected value and by using the Adapter we can
easily bind the items to the spinner objects.
 we populate our Spinner control with a list of items by using
an ArrayAdapter in our Kotlin file.
To use Spinner, we create a new project by following the below
steps:
1. Click on File, then New => New Project.
2. After that include the Kotlin support and click on next.
3. Select the minimum SDK as per convenience and click the next button.
4. Then select the Empty activity => next => finish.
Different attributes for Spinner widget
Modify activity_main.xml file- TextView and
Spinner widgets
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linear_layout"
android:gravity = "center">
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select language:"
android:textSize = "20dp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/txtView"/>
</LinearLayout>
Update strings.xml file
 Updating the name of the application using the string tag.
 Creating a list of the items which will be used in the
dropdown menu.
<resources>
<string name="app_name">SpinnerInKotlin</string>
<string name="selected_item">Selected item:</string>
<string-array name="Languages">
<item>Java</item>
<item>Kotlin</item>
<item>Swift</item>
<item>Python</item>
<item>Scala</item>
<item>Perl</item>
</string-array>
</resources>
Access Spinner in MainActivity.kt file
First, we declare a variable languages to access the strings items from the
strings.xml file.
val languages = resources.getStringArray(R.array.Languages)
then, we access the spinner and set ArrayAdaptor to control the list of
items.
val spinner = findViewById(R.id.spinner)
if (spinner != null) {
val adapter = ArrayAdapter(this,
android.R.layout.simple_spinner_item, languages)
spinner.adapter = adapter
Access Spinner in MainActivity.kt file
package com.examples.myfirstkotlinapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// access the items of the list
val languages =
resources.getStringArray(R.array.Languages)
// access the spinner
val spinner = findViewById<Spinner>(R.id.spinner)
if (spinner != null) {
val adapter = ArrayAdapter(this,
android.R.layout.simple_spinner_item,
languages)
spinner.adapter = adapter
spinner.onItemSelectedListener = object :
AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent:
AdapterView<*>,
view: View,
position: Int, id: Long) {
Toast.makeText(this@MainActivity,
getString(R.string.selected_item) + " " +
"" + languages[position],
Toast.LENGTH_SHORT).show()
}
override fun onNothingSelected(parent:
AdapterView<*>) {
// write code to perform some action
}
}
}
}
}
AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.geeksforgeeks.myfirstkotlinapp">
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output for Spinner
Step 3: Modify the activity_main.xml file
<?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"
android:background="#168BC34A"
tools:context=".MainActivity">
<!-- Button added in the activity -->
<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"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 4: Accessing the button in the MainActivity
file
Add functionality of button in the MainActivity file. Here describe the
operation to display a Toast message when the user taps on the button.
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// storing ID of the button
// in a variable
val button = findViewById<Button>(R.id.button)
// operations to be performed
// when user tap on the button
button?.setOnClickListener()
{
// displaying a toast message
Toast.makeText(this@MainActivity, R.string.message, Toast.LENGTH_LONG).show() }
}
}
Android UI Layout
 Android Layout defines the user interface that holds the UI
controls or widgets that will appear on the screen of an
android application or activity screen.
 Widget is an application or a component of an interface that
enables a user to perform a function or access a service.
 Every application is a combination of View and ViewGroup.
 An android application contains a large number of activities.
 Each activity is one page of the application.
 Each activity contains multiple user interface components
and those components are the instances of the View and
ViewGroup.
 All the elements in a layout are built using a
hierarchyof View and ViewGroup objects.
View
 A View is defined as the user interface which is used to
create interactive UI components such as
 TextView, ImageView, EditText, RadioButton
 It is responsible for event handling and drawing, called
Widgets.
View
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:- called layouts.
UI Elements
 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
Layouts in Android UI Design
 Layout Managers (or simply layouts) are extensions of the
ViewGroup class.
 They are used to set the position of child Views within the
UI we are building.
 By nesting layouts, we can create arbitrarily complex UIs
using a combination of layouts.
 Layout classes in the Android SDK can be used, modified or
can create your own to make the UI for your Views,
Fragments and Activities.
 You can display your contents effectively by using the right
combination of layouts.
 The most commonly used layout classes that are found in
Android SDK includes LinearLayout, Relative Layout,
Linear Layout
 It is a ViewGroup subclass, used to provide child View elements one by
one either in a particular direction either horizontally or vertically
based on the orientation property.
 A vertical layout has a column of Views, whereas in a horizontal layout
there is a row of Views.
 It supports a weight attribute for each child View that can control the
relative size of each child View within the available space.
Android Relative Layout
 It is a ViewGroup subclass, used to specify the position of
child View elements relative to each other like (A to the
right of B) or relative to the parent (fix to the top of the
parent).
 It is flexible than other native layouts as it lets us to define
the position of each child View relative to the other views
and the dimensions of the screen.
Types of Android Layout
 Android Frame Layout: FrameLayout is a ViewGroup
subclass, used to specify the position of View elements it
contains on the top of each other to display only a single
View inside the FrameLayout.
Android Grid Layout
 GridView is a ViewGroup that is used to display a scrollable
list of items in a grid view of rows and columns.
 It was introduced in Android 4.0 (API level 14).
 The Grid Layout used a rectangular grid of infinitely thin
lines to lay out Views in a series of rows and columns.
 The Grid Layout is incredibly flexible and can be used to
greatly simplify layouts and reduce or eliminate the complex
nesting often required to construct UIs using the layouts
described before.
Types of Android Layout
 Android Constraint Layout is a ViewGroup subclass, used to specify
the position of layout constraints for every child View relative to other
views present. A ConstraintLayout is similar to a RelativeLayout, but
having more power
 Android Table Layout: TableLayout is a ViewGroup subclass, used to
display the child View elements in rows and columns.
 Android Web View: WebView is a browser that is used to display the
web pages in our activity layout.
 Android ListView: ListView is a ViewGroup, used to display scrollable
lists of items in a single column.
 Each of above-mentioned layouts is designed to scale to suit the screen
size of the host device by avoiding the used of absolute co-ordinates of
the positions or predetermined pixel values. This makes the app
suitable for the diverse set of Android devices.
Use UI Elements in the XML file
 It is a layout similar to web pages.
 The XML layout file contains at least one root element in
which additional layout elements or widgets can be added to
build a View hierarchy.
Use UI Elements in the 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">
<!--EditText with id editText-->
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Input"
android:inputType="text"/>
<!--Button with id showInput-->
<Button
android:id="@+id/showInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="show"
android:backgroundTint="@color/colorPrimary"
android:textColor="@android:color/white"/>
</LinearLayout>
Load XML Layout File and its elements from an
Activity
 When we have created the layout, we need to load the XML
layout resource from our activity onCreate() callback method and
access the UI element from the XML using findViewById.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// finding the button
val showButton = findViewById<Button>(R.id.showInput)
// finding the edit text
val editText = findViewById<EditText>(R.id.editText)
Load XML Layout File and its elements from
an Activity
 You can see from code on slide 11 and finds out that we are
calling our layout using the setContentView method in the
form of R.layout.activity_main.
 During the launch of our activity, the onCreate() callback
method will be called by the android framework to get the
required layout for an activity.
Create elements in the Kotlin file Dynamically
 We can create or instantiate UI elements or widgets during runtime
by using the custom View and ViewGroup objects programmatically in
the Kotlin file.
 To create a layout using LinearLayout to hold an EditText and a
Button in an activity programmatically:-
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Toast
import android.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// create the button
val showButton = Button(this)
showButton.setText("Submit")
// create the editText
val editText = EditText(this)
val linearLayout = findViewById<LinearLayout>(R.id.l_layout)
linearLayout.addView(editText)
linearLayout.addView(showButton)
// Setting On Click Listener
showButton.setOnClickListener
{
// Getting the user input
val text = editText.text
// Showing the user input
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}
}
}
Different Attribute of the Layouts
Different Attribute of the Layouts
• All activities must also implement the onCreate() method. This
method gets called when the activity object gets created, and it’s
used to perform basic setup such as what layout the activity is
associated with. This is done via a call to setContentView(). In the
example above, the code:
setContentView(R.layout.activity_main)
tells Android that this activity uses activity_main.xml as its layout.
AndroidLab_IT.pptx

More Related Content

Similar to AndroidLab_IT.pptx

Basics and different xml files used in android
Basics and different xml files used in androidBasics and different xml files used in android
Basics and different xml files used in androidMahmudul Hasan
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesAhsanul Karim
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesAhsanul Karim
 
Android Development Made Easy - With Sample Project
Android Development Made Easy - With Sample ProjectAndroid Development Made Easy - With Sample Project
Android Development Made Easy - With Sample ProjectJoemarie Amparo
 
Lec005 android start_program
Lec005 android start_programLec005 android start_program
Lec005 android start_programEyad Almasri
 
Android Development for Beginners with Sample Project - Day 1
Android Development for Beginners with Sample Project - Day 1Android Development for Beginners with Sample Project - Day 1
Android Development for Beginners with Sample Project - Day 1Joemarie Amparo
 
Basic android workshop
Basic android workshopBasic android workshop
Basic android workshopThagatpam Tech
 
Data Transfer between activities and Database
Data Transfer between activities and Database Data Transfer between activities and Database
Data Transfer between activities and Database faiz324545
 
Build Your Own Instagram Filters
Build Your Own Instagram FiltersBuild Your Own Instagram Filters
Build Your Own Instagram FiltersTJ Stalcup
 
Android Application Development - Level 1
Android Application Development - Level 1Android Application Development - Level 1
Android Application Development - Level 1Isham Rashik
 
Mobile Application Development -Lecture 09 & 10.pdf
Mobile Application Development -Lecture 09 & 10.pdfMobile Application Development -Lecture 09 & 10.pdf
Mobile Application Development -Lecture 09 & 10.pdfAbdullahMunir32
 
What is Android?
What is Android?What is Android?
What is Android?ndalban
 
Android Tutorial For Beginners Part-1
Android Tutorial For Beginners Part-1Android Tutorial For Beginners Part-1
Android Tutorial For Beginners Part-1Amit Saxena
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android DevelopmentAly Abdelkareem
 
Android tutorial
Android tutorialAndroid tutorial
Android tutorialAbid Khan
 
Training Session 2 - Day 2
Training Session 2 - Day 2Training Session 2 - Day 2
Training Session 2 - Day 2Vivek Bhusal
 
Beginning Native Android Apps
Beginning Native Android AppsBeginning Native Android Apps
Beginning Native Android AppsGil Irizarry
 

Similar to AndroidLab_IT.pptx (20)

Basics and different xml files used in android
Basics and different xml files used in androidBasics and different xml files used in android
Basics and different xml files used in android
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through Activities
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through Activities
 
Android Development Made Easy - With Sample Project
Android Development Made Easy - With Sample ProjectAndroid Development Made Easy - With Sample Project
Android Development Made Easy - With Sample Project
 
Lec005 android start_program
Lec005 android start_programLec005 android start_program
Lec005 android start_program
 
Android Development for Beginners with Sample Project - Day 1
Android Development for Beginners with Sample Project - Day 1Android Development for Beginners with Sample Project - Day 1
Android Development for Beginners with Sample Project - Day 1
 
Twitter trends
Twitter trendsTwitter trends
Twitter trends
 
Basic android workshop
Basic android workshopBasic android workshop
Basic android workshop
 
Data Transfer between activities and Database
Data Transfer between activities and Database Data Transfer between activities and Database
Data Transfer between activities and Database
 
Build Your Own Instagram Filters
Build Your Own Instagram FiltersBuild Your Own Instagram Filters
Build Your Own Instagram Filters
 
Android Application Development - Level 1
Android Application Development - Level 1Android Application Development - Level 1
Android Application Development - Level 1
 
Lesson 10
Lesson 10Lesson 10
Lesson 10
 
Mobile Application Development -Lecture 09 & 10.pdf
Mobile Application Development -Lecture 09 & 10.pdfMobile Application Development -Lecture 09 & 10.pdf
Mobile Application Development -Lecture 09 & 10.pdf
 
What is Android?
What is Android?What is Android?
What is Android?
 
Android Tutorial For Beginners Part-1
Android Tutorial For Beginners Part-1Android Tutorial For Beginners Part-1
Android Tutorial For Beginners Part-1
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
Android tutorial
Android tutorialAndroid tutorial
Android tutorial
 
Training Session 2 - Day 2
Training Session 2 - Day 2Training Session 2 - Day 2
Training Session 2 - Day 2
 
Beginning Native Android Apps
Beginning Native Android AppsBeginning Native Android Apps
Beginning Native Android Apps
 
XCode8.0
XCode8.0XCode8.0
XCode8.0
 

Recently uploaded

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Recently uploaded (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

AndroidLab_IT.pptx

  • 1. Android Mobile App Development with Kotlin  Kotlin is multi-platform language, i.e. easily executable on a Java Virtual Machine.  Kotlin is sponsored by Google, announced as one of the official languages for Android Development in 2017.
  • 2. TextView in Kotlin 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. Following steps are used to create TextView in Kotlin: 1. Add a TextView in activity_main.xml file inside LinearLayout. 2. Add attributes like text, textColor, textSize, textStyle in the activity_main.xml file. 3. Open MainActivity.kt file and set OnClickListener for the textView to show the Toast message.
  • 10. Modify the strings.xml file  We can add strings in the strings.xml file and use in the other files easily by calling them with their names. <resources> <string name="app_name">TextViewInKotlin</string> <string name="text_view">Arsi University</string> <string name="text_on_click">COMPUTER SCIENCE Department</string> </resources>
  • 11. activity_main.xml fil Open activity_main.xml file and create a TextView using id textView. <?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"> <!--EditText with id editText--> <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>
  • 12. How to reference Textview in .kt file  Open MainActivity.kt file and get the reference of TextView defined in the layout file. // finding the textView val textView = findViewById(R.id.text_view_id) as TextView Setting the on click listener to the button textView?.setOnClickListener { Toast.makeText(this@MainActivity, "COMPUTER SCIENCE Department", Toast.LENGTH_LONG).show() }
  • 13. MainActivity.kt file  Open app/src/main/java/yourPackageName/MainActivity.kt to get the reference of TextView. package com.example.myfirstkotlinapp import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.TextView import android.widget.Toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //accessing our textview from layout val textView = findViewById<TextView>(R.id.text_view_id) as TextView textView?.setOnClickListener{ Toast.makeText(this@MainActivity, R.string.text_on_click, Toast.LENGTH_LONG).show() } } }
  • 14. AndroidManifest.xml file <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.geeksforgeeks.myfirstkotlinapp"> <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"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
  • 15. Button in Android  In Android applications, 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.  The Class Hierarchy of the Button Class in Kotlin
  • 16. XML Attributes of Button Widget
  • 17. XML Attributes of Button Widget
  • 18. Step by step demonstration of creating a Button Step 1: Create a new project 1. Click on File, then New => New Project. 2. Choose “Empty Activity” for the project template. 3. Select language as Kotlin. 4. Select the minimum SDK as per your need. Step 2: Modify the strings.xml file Navigate to the strings.xml file under the “values” directory of the resource folder. This file will contain all strings that are used in the Application.
  • 19. Example <resources> <string name="app_name">GfG | Button In Kotlin</string> <string name="btn">Button</string> <string name="message">Hello IT students!! This is a Button.</string> </resources> Step 3: Modify the activity_main.xml file Add a button widget in the layout of the activity. Below is the code of the activity_main.xml file which does the same.
  • 20. Spinner in Kotlin  It is a view similar to a 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.  The default value of the android spinner will be the currently selected value and by using the Adapter we can easily bind the items to the spinner objects.  we populate our Spinner control with a list of items by using an ArrayAdapter in our Kotlin file.
  • 21. To use Spinner, we create a new project by following the below steps: 1. Click on File, then New => New Project. 2. After that include the Kotlin support and click on next. 3. Select the minimum SDK as per convenience and click the next button. 4. Then select the Empty activity => next => finish.
  • 22. Different attributes for Spinner widget
  • 23. Modify activity_main.xml file- TextView and Spinner widgets <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/linear_layout" android:gravity = "center"> <TextView android:id="@+id/txtView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select language:" android:textSize = "20dp" /> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/txtView"/> </LinearLayout>
  • 24. Update strings.xml file  Updating the name of the application using the string tag.  Creating a list of the items which will be used in the dropdown menu. <resources> <string name="app_name">SpinnerInKotlin</string> <string name="selected_item">Selected item:</string> <string-array name="Languages"> <item>Java</item> <item>Kotlin</item> <item>Swift</item> <item>Python</item> <item>Scala</item> <item>Perl</item> </string-array> </resources>
  • 25. Access Spinner in MainActivity.kt file First, we declare a variable languages to access the strings items from the strings.xml file. val languages = resources.getStringArray(R.array.Languages) then, we access the spinner and set ArrayAdaptor to control the list of items. val spinner = findViewById(R.id.spinner) if (spinner != null) { val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, languages) spinner.adapter = adapter
  • 26. Access Spinner in MainActivity.kt file package com.examples.myfirstkotlinapp import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // access the items of the list val languages = resources.getStringArray(R.array.Languages) // access the spinner val spinner = findViewById<Spinner>(R.id.spinner) if (spinner != null) { val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, languages) spinner.adapter = adapter spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { Toast.makeText(this@MainActivity, getString(R.string.selected_item) + " " + "" + languages[position], Toast.LENGTH_SHORT).show() } override fun onNothingSelected(parent: AdapterView<*>) { // write code to perform some action } } } } }
  • 27. AndroidManifest.xml file <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.geeksforgeeks.myfirstkotlinapp"> <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"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
  • 29. Step 3: Modify the activity_main.xml file <?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" android:background="#168BC34A" tools:context=".MainActivity"> <!-- Button added in the activity --> <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" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
  • 30. Step 4: Accessing the button in the MainActivity file Add functionality of button in the MainActivity file. Here describe the operation to display a Toast message when the user taps on the button. import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.Toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // storing ID of the button // in a variable val button = findViewById<Button>(R.id.button) // operations to be performed // when user tap on the button button?.setOnClickListener() { // displaying a toast message Toast.makeText(this@MainActivity, R.string.message, Toast.LENGTH_LONG).show() } } }
  • 31. Android UI Layout  Android Layout defines the user interface that holds the UI controls or widgets that will appear on the screen of an android application or activity screen.  Widget is an application or a component of an interface that enables a user to perform a function or access a service.  Every application is a combination of View and ViewGroup.  An android application contains a large number of activities.  Each activity is one page of the application.  Each activity contains multiple user interface components and those components are the instances of the View and ViewGroup.  All the elements in a layout are built using a hierarchyof View and ViewGroup objects.
  • 32. View  A View is defined as the user interface which is used to create interactive UI components such as  TextView, ImageView, EditText, RadioButton  It is responsible for event handling and drawing, called Widgets. View
  • 33. 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:- called layouts.
  • 34. UI Elements  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
  • 35. Layouts in Android UI Design  Layout Managers (or simply layouts) are extensions of the ViewGroup class.  They are used to set the position of child Views within the UI we are building.  By nesting layouts, we can create arbitrarily complex UIs using a combination of layouts.  Layout classes in the Android SDK can be used, modified or can create your own to make the UI for your Views, Fragments and Activities.  You can display your contents effectively by using the right combination of layouts.  The most commonly used layout classes that are found in Android SDK includes LinearLayout, Relative Layout,
  • 36. Linear Layout  It is a ViewGroup subclass, used to provide child View elements one by one either in a particular direction either horizontally or vertically based on the orientation property.  A vertical layout has a column of Views, whereas in a horizontal layout there is a row of Views.  It supports a weight attribute for each child View that can control the relative size of each child View within the available space.
  • 37. Android Relative Layout  It is a ViewGroup subclass, used to specify the position of child View elements relative to each other like (A to the right of B) or relative to the parent (fix to the top of the parent).  It is flexible than other native layouts as it lets us to define the position of each child View relative to the other views and the dimensions of the screen.
  • 38. Types of Android Layout  Android Frame Layout: FrameLayout is a ViewGroup subclass, used to specify the position of View elements it contains on the top of each other to display only a single View inside the FrameLayout.
  • 39. Android Grid Layout  GridView is a ViewGroup that is used to display a scrollable list of items in a grid view of rows and columns.  It was introduced in Android 4.0 (API level 14).  The Grid Layout used a rectangular grid of infinitely thin lines to lay out Views in a series of rows and columns.  The Grid Layout is incredibly flexible and can be used to greatly simplify layouts and reduce or eliminate the complex nesting often required to construct UIs using the layouts described before.
  • 40. Types of Android Layout  Android Constraint Layout is a ViewGroup subclass, used to specify the position of layout constraints for every child View relative to other views present. A ConstraintLayout is similar to a RelativeLayout, but having more power  Android Table Layout: TableLayout is a ViewGroup subclass, used to display the child View elements in rows and columns.  Android Web View: WebView is a browser that is used to display the web pages in our activity layout.  Android ListView: ListView is a ViewGroup, used to display scrollable lists of items in a single column.  Each of above-mentioned layouts is designed to scale to suit the screen size of the host device by avoiding the used of absolute co-ordinates of the positions or predetermined pixel values. This makes the app suitable for the diverse set of Android devices.
  • 41.
  • 42. Use UI Elements in the XML file  It is a layout similar to web pages.  The XML layout file contains at least one root element in which additional layout elements or widgets can be added to build a View hierarchy.
  • 43. Use UI Elements in the 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"> <!--EditText with id editText--> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:hint="Input" android:inputType="text"/> <!--Button with id showInput--> <Button android:id="@+id/showInput" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="show" android:backgroundTint="@color/colorPrimary" android:textColor="@android:color/white"/> </LinearLayout>
  • 44. Load XML Layout File and its elements from an Activity  When we have created the layout, we need to load the XML layout resource from our activity onCreate() callback method and access the UI element from the XML using findViewById. override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // finding the button val showButton = findViewById<Button>(R.id.showInput) // finding the edit text val editText = findViewById<EditText>(R.id.editText)
  • 45. Load XML Layout File and its elements from an Activity  You can see from code on slide 11 and finds out that we are calling our layout using the setContentView method in the form of R.layout.activity_main.  During the launch of our activity, the onCreate() callback method will be called by the android framework to get the required layout for an activity.
  • 46. Create elements in the Kotlin file Dynamically  We can create or instantiate UI elements or widgets during runtime by using the custom View and ViewGroup objects programmatically in the Kotlin file.  To create a layout using LinearLayout to hold an EditText and a Button in an activity programmatically:- import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.LinearLayout import android.widget.Toast import android.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // create the button val showButton = Button(this) showButton.setText("Submit") // create the editText val editText = EditText(this) val linearLayout = findViewById<LinearLayout>(R.id.l_layout) linearLayout.addView(editText) linearLayout.addView(showButton) // Setting On Click Listener showButton.setOnClickListener { // Getting the user input val text = editText.text // Showing the user input Toast.makeText(this, text, Toast.LENGTH_SHORT).show() } } }
  • 47. Different Attribute of the Layouts
  • 48. Different Attribute of the Layouts
  • 49.
  • 50. • All activities must also implement the onCreate() method. This method gets called when the activity object gets created, and it’s used to perform basic setup such as what layout the activity is associated with. This is done via a call to setContentView(). In the example above, the code: setContentView(R.layout.activity_main) tells Android that this activity uses activity_main.xml as its layout.