0% found this document useful (0 votes)
3 views10 pages

Theoretical Assignment Kotlin

The document provides an overview of key concepts in Android development using Kotlin, including abstract classes, interfaces, activity lifecycle, fragments, and the steps to publish an Android application. It explains how abstract classes and interfaces function in Kotlin, details the lifecycle stages of an Android activity, and describes the fragment lifecycle and types. Additionally, it outlines a step-by-step process for publishing an app on the Google Play Store, emphasizing the importance of following guidelines and completing necessary steps for a successful launch.

Uploaded by

anshvekariya333
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)
3 views10 pages

Theoretical Assignment Kotlin

The document provides an overview of key concepts in Android development using Kotlin, including abstract classes, interfaces, activity lifecycle, fragments, and the steps to publish an Android application. It explains how abstract classes and interfaces function in Kotlin, details the lifecycle stages of an Android activity, and describes the fragment lifecycle and types. Additionally, it outlines a step-by-step process for publishing an app on the Google Play Store, emphasizing the importance of following guidelines and completing necessary steps for a successful launch.

Uploaded by

anshvekariya333
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/ 10

Theoretical Assignment

Sub: Android using Kotlin


1) Write a note on abstract classes and interfaces in Kotlin.
Ans:
Kotlin Abstract class:

In Kotlin, an abstract class is a class that cannot be instantiated and is meant to be subclassed.
An abstract class may contain both abstract methods (methods without a body) and concrete methods
(methods with a body).
An abstract class is used to provide a common interface and implementation for its subclasses. When a
subclass extends an abstract class, it must provide implementations for all of the abstract methods
defined in the abstract class.
In Kotlin, an abstract class is declared using the abstract keyword in front of the class. An abstract
class can not instantiate means we can not create object for the abstract class.

Points to remember:
1. We can’t create an object for abstract class.
2. All the variables (properties) and member functions of an abstract class are by default non-abstract.
So, if we want to override these members in the child class then we need to use open keyword.
3. If we declare a member function as abstract then we does not need to annotate with open keyword
because these are open by default.
4. An abstract member function doesn’t have a body, and it must be implemented in the derived class.

An abstract class can contain both abstract and non-abstract members as shown below:
abstract class className(val x: String) { // Non-Abstract Property

abstract var y: Int // Abstract Property

abstract fun method1() // Abstract Methods

fun method2() { // Non-Abstract Method


println("Non abstract function")
}
}
Example:
//abstract class
abstract class Employee(val name: String,val experience: Int) { // Non-Abstract

// Property
// Abstract Property (Must be overridden by Subclasses)
abstract var salary: Double

// Abstract Methods (Must be implemented by Subclasses)


abstract fun dateOfBirth(date:String)

// Non-Abstract Method
fun employeeDetails() {
println("Name of the employee: $name")
println("Experience in years: $experience")
println("Annual Salary: $salary")
}
}
// derived class
class Engineer(name: String,experience: Int) : Employee(name,experience) {
override var salary = 500000.00
override fun dateOfBirth(date:String){
println("Date of Birth is: $date")
}
}
fun main(args: Array<String>) {
val eng = Engineer("Praveen",2)
eng.employeeDetails()
eng.dateOfBirth("02 December 1994")
}

Kotlin Interfaces:
In Kotlin, an interface is a collection of abstract methods and properties that define a common
contract for classes that implement the interface. An interface is similar to an abstract class, but it can
be implemented by multiple classes, and it cannot have state.
Interfaces are custom types provided by Kotlin that cannot be instantiated directly. Instead, these
define a form of behavior that the implementing types have to follow. With the interface, you can define
a set of properties and methods, that the concrete types must follow and implement.

Creating Interfaces –
The interface definition in Kotlin begins with the interface keyword followed by the name of the
interface, followed by the curly braces within which the members of the interface reside. The difference
is that the members will have no definition of their own. These definitions will be provided by the
conforming types.
Example:
interface Vehicle {
fun start()
fun stop()
}
class Car : Vehicle {
override fun start()
{
println("Car started")
}
override fun stop()
{
println("Car stopped")
}
}
fun main()
{
val obj = Car()
obj.start()
obj.stop()
}

2) Write a note on Activity life cycle with example.


Ans: In Android, an activity is referred to as one screen in an application. It is very similar to a single
window of any desktop application. An Android app consists of one or more screens or activities.
Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when a
new activity starts, the previous one always remains below it. There are four stages of an activity.
1. If an activity is in the foreground of the screen i.e at the top of the stack, then it is said to be active
or running. This is usually the activity that the user is currently interacting with.
2. If an activity has lost focus and a non-full-sized or transparent activity has focused on top of your
activity. In such a case either another activity has a higher position in multi-window mode or the
activity itself is not focusable in the current window mode. Such activity is completely alive.
3. If an activity is completely hidden by another activity, it is stopped or hidden. It still retains all the
information, and as its window is hidden thus it will often be killed by the system when memory is
needed elsewhere.
4. The system can destroy the activity from memory by either asking it to finish or simply killing its
process. When it is displayed again to the user, it must be completely restarted and restored to its
previous state.
For each stage, android provides us with a set of 7 methods that have their own significance for each
stage in the life cycle. The image shows a path of migration whenever an app switches from one state
to another.

1. onCreate()
It is called when the activity is first created. This is where all the static work is done like creating views,
binding data to lists, etc. This method also provides a Bundle containing its previous frozen state, if
there was one.

2. onStart()
It is invoked when the activity is visible to the user. It is followed by onResume() if the activity is invoked
from the background. It is also invoked after onCreate() when the activity is first started.

3. onRestart()
It is invoked after the activity has been stopped and prior to its starting stage and thus is always
followed by onStart() when any activity is revived from background to on-screen.

4. onResume()
It is invoked when the activity starts interacting with the user. At this point, the activity is at the top of
the activity stack, with a user interacting with it. Always followed by onPause() when the activity goes
into the background or is closed by the user.
5. onPause()
It is invoked when an activity is going into the background but has not yet been killed. It is a counterpart
to onResume(). When an activity is launched in front of another activity, this callback will be invoked on
the top activity (currently on screen). The activity, under the active activity, will not be created until the
active activity’s onPause() returns, so it is recommended that heavy processing should not be done in
this part.
6. onStop()
It is invoked when the activity is not visible to the user. It is followed by onRestart() when the activity is
revoked from the background, followed by onDestroy() when the activity is closed or finished, and
nothing when the activity remains on the background only. Note that this method may never be called,
in low memory situations where the system does not have enough memory to keep the activity’s
process running after its onPause() method is called.
7. onDestroy()
The final call received before the activity is destroyed. This can happen either because the activity is
finishing (when finish() is invoked) or because the system is temporarily destroying this instance of the
activity to save space. To distinguish between these scenarios, check it with isFinishing() method.

Example:
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toast = Toast.makeText(applicationContext, "onCreate Called",
Toast.LENGTH_LONG).show()
}
override fun onStart() {
super.onStart()
val toast = Toast.makeText(applicationContext, "onStart Called",
Toast.LENGTH_LONG).show()
}
override fun onRestart() {
super.onRestart()
val toast = Toast.makeText(applicationContext, "onRestart Called",
Toast.LENGTH_LONG).show()
}
override fun onPause() {
super.onPause()
val toast = Toast.makeText(applicationContext, "onPause Called",
Toast.LENGTH_LONG).show()
}
override fun onResume() {
super.onResume()
val toast = Toast.makeText(applicationContext, "onResume Called",
Toast.LENGTH_LONG).show()
}
override fun onStop() {
super.onStop()
val toast = Toast.makeText(applicationContext, "onStop Called",
Toast.LENGTH_LONG).show()
}

override fun onDestroy() {


super.onDestroy()
val toast = Toast.makeText(applicationContext, "onDestroy Called",
Toast.LENGTH_LONG).show()
}
}

3) Explain Fragement in detail.


Ans:
Fragment:
Fragment is a piece of an activity that enables a more modular activity design. A fragment
encapsulates functionality so that it is easier to reuse within activities and layouts. Android devices
exist in a variety of screen sizes and densities. Fragments simplify the reuse of components in
different layouts and their logic. You can build single-pane layouts for handsets (phones) and multi-
pane layouts for tablets. You can also use fragments also to support different layouts for landscape
and portrait orientation on a smartphone.
Fragment Lifecycle
Android fragments have their own lifecycle very similar to an android activity.
Looking to become an expert in Android App Development? Whether you're a student or a professional
aiming to advance your career in mobile app development, our course, "Android App Development
with Kotlin," available exclusively on GeeksforGeeks, is the perfect fit for you.

 onAttach() : The fragment instance is associated with an activity instance. The fragment and the
activity is not fully initialized. Typically you get in this method a reference to the activity which uses
the fragment for further initialization work.
 onCreate() : The system calls this method when creating the fragment. You should initialize
essential components of the fragment that you want to retain when the fragment is paused or
stopped, then resumed.
 onCreateView() : The system calls this callback when it’s time for the fragment to draw its user
interface for the first time. To draw a UI for your fragment, you must return a View component from
this method that is the root of your fragment’s layout. You can return null if the fragment does not
provide a UI.
 onActivityCreated() : The onActivityCreated() is called after the onCreateView() method when the
host activity is created. Activity and fragment instance have been created as well as the view
hierarchy of the activity. At this point, view can be accessed with the findViewById() method.
example. In this method you can instantiate objects which require a Context object
 onStart() : The onStart() method is called once the fragment gets visible.
 onResume() : Fragment becomes active.
 onPause() : The system calls this method as the first indication that the user is leaving the
fragment. This is usually where you should commit any changes that should be persisted beyond
the current user session.
 onStop() : Fragment going to be stopped by calling onStop()
 onDestroyView() : Fragment view will destroy after call this method
 onDestroy() :called to do final clean up of the fragment’s state but Not guaranteed to be called by
the Android platform.
Types of Fragments
 Single frame fragments : Single frame fragments are using for hand hold devices like mobiles,
here we can show only one fragment as a view.
 List fragments : fragments having special list view is called as list fragment
 Fragments transaction : Using with fragment transaction. we can move one fragment to another
fragment.
Handling the Fragment Lifecycle
A Fragment exist in three states:
 Resumed : The fragment is visible in the running activity.
 Paused : Another activity is in the foreground and has focus, but the activity in which this fragment
lives is still visible (the foreground activity is partially transparent or doesn’t cover the entire screen).
 Stopped : The fragment is not visible. Either the host activity has been stopped or the fragment has
been removed from the activity but added to the back stack. A stopped fragment is still alive (all
state and member information is retained by the system). However, it is no longer visible to the user
and will be killed if the activity is killed.
Fragment Transaction:
While for an dynamic activity we are set buttons for an interactive UI. If we are set after clicking the
button the fragment should appear then we have to get help from Fragment Manager. It handle all the
fragment in an activity. We need to set fragment transaction with the help of fragment manager and and
begin transaction, and then simply replace the layout of the fragment with desired place. Below is the
code that shows how to implement a fragment transaction.

Example:
package org.geeksforgeeks.demo
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class BlankFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_blank, container, false)
}
}

4) Write down steps to publish android applications


Ans: Building a mobile app simply means you’re half done. But one must concern about the launch of
the application. It’s very confusing because as a beginner, they are not friendly with the google play
store guidelines. So let’s understand step by step process to publish your android app on google play
store.

Step 1: Make a Developer Account


A developer account is must be needed to upload an app on the Google Play Store, and the process is
very simple. Just go through Google Play Store and do as instructed.
Step 2: After you completed step 1 you will be redirected to this page where you have to click on
the CREATE APPLICATION button.

Step 3: Store listing


After you completed step 2 you will be redirected to this page where you have to provide the Short
description and Full description of your App.
o Then you scroll down the page and now you have to add the Hi-res icon of your app.
o Then you have to provide the Screenshots of your app.
o Ant next thing you have to provide is the Feature Graphic of your app. Note that this graphic is
then used everywhere your app is featured on Google Play.
o Then come to Categorization part where you have to provide your Application
type and Category of your app.
o Then come to Contact details part where you have to provide your Website(if
any), email, and Phone of yours.
o And finally when you click on SAVE DRAFT button you can see that Store listing tab is now
become turned to green and you are done for Store listing.
Step 4: App release
After completing step 3 go to App releases then scroll down to Production track and click
on MANAGE button.
o After redirecting to the next page click on the CREATE RELEASE button
o After that on the next page, you have to upload your APK file in Android App
Bundles and APKs to add section.
o After that simply click on the SAVE button.
Step 5: Content rating
Now after completing step 4 go to Content rating and click on CONTINUE button.
o After that fill your email address as well as confirm the email address.
o And then Select your app category.
o After selecting your app category make sure that you read all of these and answer them
correctly.
o And after answering them correctly don’t forget to click on SAVE QUESTIONNAIRE button.
o Once you saved all those things then click on CALCULATE RATING button.
o When you redirected to another page scroll down and click on APPLY RATING button. And you are
done for Content rating section. Don’t forget to notice that Content rating section is now become
turned to green.
Step 6: Pricing & distribution
Then go to the Pricing & distribution section. Then select the country in which you want to available
your app.

Step 7: App content


o Then come to the App content section. And in the Privacy policy section click on the Start button.
o And then provide a valid Privacy policy URL. Note that google will check this.
o Then go back and continue further steps by clicking start button in Ads section.
o Then select does your app contain ads or not? And click on SAVE button.
o Then again go back and continue further steps by clicking start button in Target audience and
content section.
o In the next page select the Target age group and scroll down and click on the Next button.
o Then check the Appeal to children section. And click on the Next button.
o On the next page click on the Save button and you are done for App content section.
Step 8: App releases
Again go back to the App releases section. And in the Production track click on the EDIT RELEASE button.
Then on the next page go down and down and click on the REVIEW button.
And finally, on the next page click on the START ROLLOUT TO PRODUCTION button to send your
app to review. And you are finally done.
After usually 4 to 5 days they will review your app and let you know to either approve or reject your
app.

5) Exlain different types of Layout in detail.


Ans:
Layouts in Android is used to define the user interface that holds the UI controls or widgets that
will appear on the screen of an android application or activity screen. Generally, every application is a
combination of View and ViewGroup. As we know, an android application contains a large number of
activities and we can say each activity is one page of the application. So, 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 hierarchy of 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, etc., and is responsible for event handling and
drawing. They are Generally Called Widgets.

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
Types of Android Layout
 Android Linear Layout: LinearLayout 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.
 Android Relative Layout: RelativeLayout 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).
 Android Constraint Layout: ConstraintLayout 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 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 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.
 Android Grid View: GridView is a ViewGroup that is used to display a scrollable list of items in a grid view of
rows and columns.

Use UI Elements in the XML file


Here, we can create 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.
Following is the example:

<?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>

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. Below is the example of creating 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


XML attributes Description

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

Used to declare the width of View and ViewGroup elements in the


android:layout_width
layout.

Used to declare the height of View and ViewGroup elements in the


android:layout_height
layout.

Used to declare the extra space used on the left side of View and
android:layout_marginLeft
ViewGroup elements.

Used to declare the extra space used on the right side of View and
android:layout_marginRight
ViewGroup elements.

Used to declare the extra space used in the top side of View and
android:layout_marginTop
ViewGroup elements.

Used to declare the extra space used in the bottom side of View and
android:layout_marginBottom
ViewGroup elements.

android:layout_gravity Used to define how child Views are positioned in the layout.

You might also like