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() }
}
}
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
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.
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
}
}
}
}
}
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()
}
}
}
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.