Documente Academic
Documente Profesional
Documente Cultură
Practical
1:
Introduction to Android, Introduction to Android Studio IDE,
Application Fundamentals:
Creating a
Project:
Select the form factors your app will be run on. Here, you can select device type
for your android application. And at the same time, you select Minimum SDK
which will be supported by your android application.
When you select Minimum SDK, Android studio will give you some active
android devices percentage that will support your android application.
Otherwise, you can also click on "Help me choose" button for more information
about android platform versions.
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
Hit "Next" button once you are
done.
Step 4: Add
activity
This screen suggests you add any activity at first. If you don’t want to add any
activity at starting, you can select Add no activity option. If you have selected
Phone and Tablet in option in the last screen then it will display the only phone
related suggestions. Select Activity as per your project requirements.
For example, select Google Maps Activity for google map project, Blank
Activity for blank project etc.
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
If you are done with it, click "Next"
button.
Here, you can customize your selected activity. You can change your Activity
name from here. And also you can change Layout name (that will be attached to
your activity), Title (Title of the activity), Menu Resource Name (Menu’s resource
file name).If you want to add fragment to your activity then select Use a fragment
option given below.
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
Step 6:
Finish
Finish the
process.
Activity_Main.Kt
package
com.example.nikita.broadcast
import
android.support.v7.app.AppCompatActivity
import android.os.Bundle
} Activity_Main.xml
<TextVie
w
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="par
ent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
</android.support.constraint.ConstraintLay
out>
Apk in
avd:
Android
Components:
Components of an Android
Application
There are some necessary building blocks that an Android application consists
of. These loosely coupled components are bound by the application manifest file
which contains description of each component and how they interact. The
manifest file also contains the app’s metadata, its hardware configuration and
platform requirements, external libraries and required permissions.
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
Activities
-
Activities are said to be the presentation layer of our applications. The UI of our
application is build around one or more extensions of the Activity class. By using
Fragments and Views, activities set the layout and display the output and also
respond to the users actions. An activity is implemented as a subclass of class
Activity.
Service
s-
These are like invisible workers of our app. These components run at backend,
updating your data sources and Activities, triggering Notification and also
broadcast Intents. They also perform some tasks when applications are not
active. A service can be used as a subclass of class Service:
public class ServiceName extends
Service {
} Content
Providers-
public void
onCreate(){}
Broadcast
Receivers-
They are known to be intent listeners as they enable your application to listen
the Intents that satisfy the matching criteria specified by us. Broadcast Receivers
make our application to react to any received Intent thereby making them perfect
for creating event driven applications.
For Android 4.2 and newer, Developer options is hidden by default; use the
following steps:
Tap the Build number seven times to make Settings > Developer options
available.
Tip: You might also want to enable the Stay awake option, to prevent your
Android device from sleeping while plugged into the USB port.
________________________________________________________________
________
BroadCast
Receiver
mainActivity.k
t
import
android.support.v7.app.AppCompatActivity
import
android.os.Bundle
super.onCreate(savedInstanceS
tate)
setContentView(R.layout.activity_ma
in)
}
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
Activity_main.xm
<TextVie
w
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="par
ent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLay
out>
MyReceiver.
kt
package
com.example.nikita.broadcast
import android.content.BroadcastReceiver
import android.content.Context import
android.content.Intent import
android.support.v7.app.AppCompatActivity
import android.os.Bundle import
android.widget.Toast
class MyReceiver :
BroadcastReceiver() {
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
} } AndroidManifest.xml
<applicatio
n
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_roun
d" 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> <receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter> </receiver> </application>
</manifest
>
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
Practical
2:
Programming
Resources
colo
r/
XML files that define a state list of colors. They are saved in res/color/ and
accessed from the R.color class.
The value always begins with a pound (#) character and then followed by the
Alpha-Red- Green-Blue information in one of the following formats:
#RG
B
#ARG
B
#RRGGB
B
#AARRGGB
B
Drawable
s
Image files like .png, .jpg, .gif or XML files that are compiled into bitmaps,
state lists, shapes, animation drawable. They are saved in res/drawable/ and
accessed from the R.drawable class.
Dimensio
n
d
p
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
pt Points - 1/72 of an inch based on the physical size of the screen, assuming a
not recommended
because the actual representation can vary across devices;
each devices may have a different number of pixels per inch and may have
more or fewer total pixels available on the screen.
the screen.
screen.
String
resources
A string resource provides text strings for your application with optional text
styling and formatting. There are three types of resources that can provide your
application with strings:
Strin
g
String
Array
THEME
In this task, you will add the "night mode" theme to your app, which will allow
the users to use a low contrast version of your app that is easier on the eyes at
night time, as well as make a few polishing touches to the User Interface.
MainAcivity.kt
package com.example.nikita.practical2_res
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" android:background="@drawable/download">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="@string/hello"
android:textColor="@color/red"
style="@style/custom"/>
</android.support.constraint.ConstraintLayout>
Styles.x
ml
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
<resources>
</resources>
Colors.x
ml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#168500</color> <color
name="colorPrimaryDark">#00574B</color> <color
name="colorAccent">#D81B60</color> <color
name="red">#8adcd1</color> </resources>
Strings.x
ml
<resources>
<string name="app_name">practical2_res</string> <string
name="hello">Hello!###</string>
</resources>
Output
:
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
Method Description
onResume called when activity will start interacting with the user.
Android Activity
Lifecycle
By the help of activity, you can place all your UI components or widgets in a
single screen.
The 7 lifecycle method of Activity describes how activity will behave at different
states.
onPause() { super
.onPause()
onStop() { super.onStop()
print("onStop") } override fun
onRestart() { super
.onRestart()
onDestroy() { super.onDestroy()
print("onDestroy") } fun
print(msg: String){
B) Multiple
Activities:
MainActivity.k
t
package com.example.nikita.multipleactivity
import android.content.Intent import
android.support.v7.app.AppCompatActivity import
android.os.Bundle import android.view.View import
android.widget.Button
mButton.setOnClickListener {
//intent to start NewActivity startActivity(Intent(this@MainActivity, NewActivity::class.java)) } } }
activity_main.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Open
New Activity" /> </LinearLayout>
NewActivity.
kt
package com.example.nikita.multipleactivity
activity_new.x
ml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NewActivity">
<TextView
android:text="Welcome on 2nd Activity" android:layout_width="wrap_content"
android:layout_height="wrap_content" tools:layout_editor_absoluteY="137dp"
tools:layout_editor_absoluteX="163dp" android:id="@+id/textView"/> </LinearLayout>
<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> <activity android:name=".NewActivity"> </activity> </application>
</manifest>
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
PRACTICAL 4
LinearLayo
ut
Android LinearLayout is used for application UI. Its a view group that aligns and
contain nested widget or layout in a single direction , Vertically or horizontally.
You have to specify the layout orientation with the android:orientation attribute. If you
don’t add orientation then default Horizontal will works.
Activity_main.xm
l
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"/” horizontal”
tools:context=".MainActivity">
<Button android:id="@+id/btnPauseService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="pause_service"/>
<Button android:id="@+id/btnStopService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="stop_service"/>
</LinearLayout>
RelativeLayout:
Android RelativeLayout is provide flexibility to arrange a child views relatively to each
others. Means you can align child view left, right , below and above to relative another
views or parent layout. RelativeLayout is a very important component in term of design
user interface in android app. Because it can eliminate nested views and groups. Its
Keep Layout Hierarchy flat, which help to improve performance of android app.
Relative.xml: (for this create new xml file. Give Root tag as
”RelativeLayout” )
<EditText
android:id="@+id/et_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_below="@+id/tv_label"
android:ems="10" android:hint="Enter Id"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/et_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_id"
android:layout_margin="8dp"
android:ems="10" android:hint="Enter
password"
android:inputType="textPassword" />
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_pass"
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
android:layout_margin="18dp"
android:text="Login" />
<Button
android:id="@+id/btn_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_pass"
android:layout_margin="18dp"
android:layout_toRightOf="@+id/btn_login"
android:text="Register" />
</RelativeLayout>
MainActivity.kt(No need to change) package
com.example.nikita.diffactivity
</TableRow>
<TableRow >
<Button
android:text="Button" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/button2"/> <Button
android:text="Button" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/button3"/> <Button
android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/button4"/> </TableRow> </TableLayout> MainActivity.kt
package com.example.nikita.diffactivity
btn2.setOnClickListener {
Toast.makeText(this,"2",Toast.LENGTH_LONG) .show(); }
btn3.setOnClickListener {
Toast.makeText(this,"3",Toast.LENGTH_LONG) .show(); } } }
Output:
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
Android FrameLayout
Android FrameLayout is one of the useful layouts provided by android system, which
allows User Interface widgets to be overlapped with each other. It’s used in cases such
as placing a TextView over an ImageView. This becomes quite difficult to implement
using LinearLayout or RelativeLayout since they place widgets adjacent to each other.
FrameLayout is designed to display a single item at a time. We can have multiple
elements within a framelayout but each element will be positioned based on the top left
of the screen. In FrameLayout, all the child views added are placed like stack. The
most recent added are shown on top.
Activity_main.xm
l:
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="495dp"
android:scaleType="fitXY"
app:srcCompat="@drawable/download" />
<TextView
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:gravity="center_horizontal"
android:text="Hello World!"
android:textColor="@color/black"
android:textSize="40sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="80dp" android:text="Go
Book" />
</FrameLayout>
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
ActivityMain.kt(no need to
change)
package com.example.nikita.frame
ListView
:
ListView is a view group that displays a list of scrollable items. The list
items are automatically inserted to the list using an Adapter that pulls
content from a source such as an array or database query and converts
each item result into a view that's placed into the list.
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn" android:text="Click
me to view list"
android:layout_marginTop="200dp"
android:layout_marginLeft="90dp"/>
</android.support.constraint.ConstraintLayout>
String.xml:
<resources>
<string name="app_name">Listview</string> <array
name="insert_list">
<item>one</item>
<item>two</item>
<item>three</item>
<item>four</item>
<item>five</item>
<item>six</item>
<item>seven</item>
<item>eight</item>
<item>nine</item>
<item>ten</item> </array>
</resources>
MainActivity.kt
:
package com.example.nikita.listview
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn.setOnClickListener {
val intent = Intent(this, List_view::class.java) startActivity(intent)
} } }
xml
activity_list_view.xml: <?
version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".List_view">
<ListView
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp"
tools:context=".list_view"
android:entries="@array/insert_list"
tools:ignore="MissingConstraints"/>
</android.support.constraint.ConstraintLayout>
List_view.kt:
package com.example.nikita.listview
setContentView(R.layout.activity_list_view) } }
Output of
ListView:
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
Grid Layout: Android GridView shows items in two-dimensional scrolling
grid (rows & columns)
Grid.xm
l
<?xml version="1.0" encoding="utf-8"?> <GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="3" android:columnCount="3"
android:padding="20dp" >
<Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="1"/>
<Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="2"/> <Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="3"/> <Button
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
android:layout_width="110dp"
android:layout_height="100dp"
android:text="4"/>
<Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="5"/> <Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="6"/> <Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="7"/>
<Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="8"/> <Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="9"/>
</GridLayout> No
need to change in
MainActivity.kt
Output
:
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
PRACTICAL 7 Programs on
Intents, Events Listeners and Adapters
Note: Refer Table layout (practical 4)code
for Events Listeners and for Intent
GUI(Practical 5)code
PRACTICAL 8
PRACTICAL 9
• Constructor : This takes the Context (e.g., an Activity), the name of the database, an
optional cursor factory (we’ll discuss this later), and an integer representing the version of
the database schema you are using (typically starting from 1 and increment later).
• onCreate(SQLiteDatabase db) : It’s called when there is no database and the app
needs one. It passes us a SQLiteDatabase object, pointing to a newly-created database,
that we can populate with tables and initial data.
• onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) : It’s called when
the schema version we need does not match the schema version of the database, It
passes us a SQLiteDatabase object and the old and new version numbers. Hence we
can figure out the best way to convert the database from the old schema to the new one.
<EditText
android:id="@+id/editText_firstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:ems="10"
android:inputType="textPersonName"
android:hint="f_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText_lastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
android:inputType="textPersonName" android:hint="l_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText_firstName" />
<Button
android:id="@+id/button_save" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginStart="16dp"
android:layout_marginLeft="16dp" android:layout_marginTop="32dp"
android:text="save" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText_lastName" />
<Button
android:id="@+id/button_show" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginEnd="16dp"
android:layout_marginRight="16dp" android:layout_marginTop="32dp"
android:text="show" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText_lastName" />
<TextView
android:id="@+id/textView_show" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_marginEnd="8dp"
android:layout_marginStart="8dp" android:layout_marginTop="136dp"
android:text="empty" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText_lastName" />
</android.support.constraint.ConstraintLayout>
Users.
kt
DatabaseHandler.kt
package com.example.student.prac9sqlite
package com.example.student.prac9sqlite
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
if (success){
val toast = Toast.makeText(this,"Saved Successfully",
Toast.LENGTH_LONG) .show()
}}
if (!editText_firstName.text.toString().equals("") &&
!editText_lastName.text.toString().equals("")){ validate = true }else{
validate = false val toast = Toast.makeText(this,"Fill all details",
Toast.LENGTH_LONG) .show()
} return validate } }
Output:
PRACTICAL 10
thread. In android we cant directly touch background thread to main thread in android
development.
Synta
x:
Methods of
AsyncTask
Activity_main.x
ml
<?xml version="1.0" encoding="utf-8"?> <
RelativeLayout
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">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ImageView>
<Button
android:id="@+id/button"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_below="@+id/image"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Download image"
android:background="@color/colorAccent"/>
</RelativeLayout>
MainActivity.
kt
package com.example.nikita.asyncimage
import android.os.AsyncTask
import android.widget.Button
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
import android.widget.ImageView
utton =
// Locate the Button in activity_main.xml b
findViewById<View>(R.id.button) as Button
String): Bitmap? {
onPostExecute(result: Bitmap) {
// Set the bitmap into ImageView
image.setImageBitmap(result) // Close progressdialog
mProgressDialog.dismiss() } } }
AndroidManifest.xml
Add Following
Permission:
<uses-permission android:name="android.permission.INTERNET"/>
Output
:
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
Conclusio
n:
<Button
android:id="@+id/pauseBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:enabled="false"
android:text="Pause"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/playBtn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/playBtn" android:layout_width="88dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:text="Play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/stopBtn"
app:layout_constraintStart_toEndOf="@+id/pauseBtn"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="8dp" android:enabled="false"
android:text="Stop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RelativeLayout
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="76dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
<TextView
android:id="@+id/tv_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_due"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_pass"
android:saveEnabled="false" /> </RelativeLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.k
t
package com.example.nikita.media
= MediaPlayer.create(applicationContext, R.raw.iphone)
mediaPlayer.start() Toast.makeText(this,"media
playing",Toast.LENGTH_SHORT) .show()
} initializeSeekBar()
playBtn.isEnabled = false
pauseBtn.isEnabled = true
stopBtn.isEnabled = true
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
mediaPlayer.setOnCompletionListener {
playBtn.isEnabled = true pauseBtn.isEnabled = false stopBtn.isEnabled =
false
Toast.makeText(this,"end",Toast.LENGTH_SHORT).show() } } // Pause the
Method to initialize seek bar and audio stats private fun initializeSeekBar() {
seek_bar.max = mediaPlayer.seconds
runnable = Runnable {
seek_bar.progress = mediaPlayer.currentSeconds
handler.postDelayed(runnable,1000) }
handler.postDelayed(runnable, 1000) }
} // Creating an extension property to get the media player time duration in seconds val
MediaPlayer.seconds: Int
get() {
return this.duration / 1000 } // Creating an extension property to get media player current position in
seconds v al MediaPlayer.currentSeconds:Int
get() {
return this.currentPosition/1000 }
output
:
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
Telephone
API:
The android.telephony.TelephonyManager class provides information about the
telephony services such as subscriber id, sim serial number, phone network type etc.
Moreover, you can determine the phone state etc.
activityMain.x
ml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout
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">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="38dp"
android:layout_marginTop="30dp"
android:text="Phone Details:" />
</RelativeLayout>
MainActivity.k
t
package com.example.nikita.phone
when (phoneType) {
TelephonyManager.PHONE_TYPE_CDMA - > strphoneType = "CDMA"
TelephonyManager.PHONE_TYPE_GSM -> strphoneType = "GSM"
TelephonyManager.PHONE_TYPE_NONE - > strphoneType = "NONE" } //getting
}}
add permission in
androidmanifest.xml
<uses-permission android:name=" android.permission.READ_PHONE_STATE"/>
Output:
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
PRACTICAL 12
On all versions of Android, to declare that your app needs a permission, put a <uses-
manifest> e
permission>element in your app manifest, as a child of the top-level < lement.
Activity_main.x
ml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/> <Button
android:text="Button" android:layout_width="wrap_content"
android:layout_height="wrap_content" tools:layout_editor_absoluteY="182dp"
tools:layout_editor_absoluteX="141dp" android:id="@+id/button"/>
</android.support.constraint.ConstraintLayout>
Android Manifest.xml <?x ml version="1.0" encoding="utf-8"?> <
manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nikita.pract12permission"> <uses-permission
android:name="android.permission.INTERNET" /> <uses-permission
android:name="android.permission.CAMERA"/> <uses-permission
android:name="android.permission.READ_CONTACTS"/> <uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
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"/>
</manifest>
ManagePermissions
.kt
package com.example.nikita.pract12permission
true return
false } } MainActivity.kt
package com.example.nikita.pract12permission
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.SEND_SMS,
android.Manifest.permission.READ_CALENDAR
if(isPermissionsGranted){
// Do the task now t oast( "Permissions granted.") }else{
PRACTICAL 13
Programming Network Communications and Services
(JSON)
Retrofit android is a type-safe HTTP client for Android and Java. Retrofit is a REST Client for Java,
Android, and Kotlin by Square inc under Apache 2.0 license. With this library or module used the in-app
app for server smooth connection, like sing in sing up or downloading-uploading data.
Why Retrofit library? Because it will save your development time and also you can keep your code in
developer friendly. Retrofit has given almost all the API’s to make a server call and to receive a
response. Internally they also use GSON to do the parsing.
</manifest>
build.gradle(Module:ap
p)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation
'com.android.support:appcompat-v7:28.+' implementation
'com.android.support.constraint:constraint- layout:1.0.2'
implementation 'com.squareup.retrofit2:retrofit:2.3.0' implementation
'com.squareup.retrofit2:converter-gson:2.2.0'
<ListView
android:id="@+id/listViewHeroes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent" />
</RelativeLayout>
Hero.kt package
com.example.student.retrofit13
class Hero (
val name: String, val realname:
String, val team: String, val
firstappearance: String, val
createdby: String, val publisher:
String, val imageurl: String, val bio:
String)
Api.kt(Interface file)
package com.example.student.retrofit13
interface Api {
companion object {
MainActivity.kt package
com.example.student.retrofit13
import android.support.v7.app.AppCompatActivity import
android.os.Bundle import android.view.View import
android.widget.ListView
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
import android.widget.ArrayAdapter
import android.widget.Toast
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
call.enqueue(object : Callback<List<Hero>> {
override fun onResponse(call: Call<List<Hero>>, response:
Response<List<Hero>>) {
val heroList = response.body()
al heroes =
//Creating an String array for the ListView v
arrayOfNulls< String>(heroList!!.size)
//looping through all the heroes and inserting the names inside the string array
for (i in heroList.indices) {
heroes[i] = heroList[i].name } //displaying the string array into listview listView.adapter =
ArrayAdapter<String>(applicationContext, android.R.layout.simple_list_item_1, heroes)
}
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.
Output
:
Notes By: Prof. Nikita M. Chaudhari Advanced Mobile Programming
S.N.College,Bhayandar(E). T.Y B.Sc. I.T.