Mobile Application Development Lab
Mobile Application Development Lab
Application Development
LAB MANUAL
1
Course Title: Mobile
Application Development
Table of Contents
1. Install Android Studio.....................................................................................................................
1.1 System Requirements...............................................................................................................
2. Setup Android Studio..................................................................................................................
2. Hello World App...........................................................................................................................
2.1 Lab Task:.................................................................................................................................
3. Name Input Greeting App.............................................................................................................
3.1 Lab Task..................................................................................................................................
4. User Form with Submit.................................................................................................................
4.1 Lab Task..................................................................................................................................
5. Activity Intent Value Transfer........................................................................................................
6. Send SMS via Intent......................................................................................................................
7. Fragment-based Application.........................................................................................................
8. RadioButton UI App......................................................................................................................
9. Menu-based Application...............................................................................................................
10. User Registration with Database.................................................................................................
2
Course Title: Mobile
Application Development
Lab No.1
3
Course Title: Mobile
Application Development
Once you launched Android Studio, its time to mention JDK7 path or later version
in android studio installer.
4
Course Title: Mobile
Application Development
Need to check the components, which are required to create applications, below the
image has selected Android Studio, Android SDK, Android Virtual Machine and
performance(Intel chip).
5
Course Title: Mobile
Application Development
Need to specify the location of local machine path for Android studio and Android
SDK, below the image has taken default location of windows 8.1 x64 bit
architecture.
Need to specify the ram space for Android emulator by default it would take
512MB of local machine RAM.
6
Course Title: Mobile
Application Development
At final stage, it would extract SDK packages into our local machine, it would take
a while time to finish the task and would take 2626MB of Hard disk space.
7
Course Title: Mobile
Application Development
After done all above steps perfectly, you must get finish button and it gonna be
open android studio project with Welcome to android studio message as shown
below
You can start your application development by calling start a new android studio
project. in a new installation frame should ask Application name, package
information and location of the project.
8
Course Title: Mobile
Application Development
After entered application name, it going to be called select the form factors your
application runs on, here need to specify Minimum SDK, in our tutorial, I have
declared as API23: Android 6.0(Mashmallow)
The next level of installation should contain selecting the activity to mobile, it
specifies the default layout for Applications
9
Course Title: Mobile
Application Development
At the final stage it going to be open development tool to write the application
code.
10
Course Title: Mobile
Application Development
If your AVD is created successfully it means your environment is ready for Android
application development. If you like, you can close this window using top-right
cross button. Better you restart your machine and once you are done with this last
step, you are ready to proceed for your first Android example but before that we
will see few more important concepts related to Android Application Development.
Exercise Program
Write the steps in installing Android Studio in Windows and Linux.
11
Course Title: Mobile
Application Development
Record Notes
12
Course Title: Mobile
Application Development
Lab No. 2
2. Hello World App
AIM: To design an android application to display Hello World
First step is to create a simple Android Application using Android studio. When
you click on Android studio icon, it will show screen as shown below
You can start your application development by calling start a new android studio project.
in a new installation frame should ask Application name, package information and
location of the project.−
13
Course Title: Mobile
Application Development
Change the name of the application. Change the default Project location to your
preferred directory or just leave it as the default location.
14
Course Title: Mobile
Application Development
On the minimum API level, ensure that API 15: Android 4.0.3
IceCreamSandwich is set as the Minimum SDK. This ensures that your
application runs on almost all devices.
The next level of installation should contain selecting the activity to mobile, it specifies
the default layout for Applications.
15
Course Title: Mobile
Application Development
SOURCE CODE :
The Main Activity File The main activity code is a Java file MainActivity.java.
This is the actual application file which ultimately gets converted to a Dalvik
executable and runs your application
package com.example.helloworldapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_conte
nt"
android:layout_height="wrap_cont
ent"
16
Course Title: Mobile
Application Development
android:layout_centerHorizontal="t
rue"
android:layout_centerVertical="true
"
android:padding="@dimen/padding
_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
Plug in your device to your computer with a USB cable. If you're developing on Windows,
you might need to install this universal ADB USB driver or find your specific USB driver
for your device.
The next step is to enable USB debugging so your phone can interact with your computer in a
developer mode.
17
Course Title: Mobile
Application Development
Now, we can launch apps from Android Studio onto our device:
1. Select one of your projects and click "Run" from the toolbar.
2. In the "Choose Device" window that appears, select the "Choose a running device" radio
button, select the device, and click OK.
To run the app from Android studio, open one of your project's activity files and click Run
icon from the tool bar. Android studio installs the app on your AVD and starts it and if
everything is fine with your set-up and application, it will display following Emulator window
−Once Gradle finishes building, Android Studio should install the app on your connected device
and start it.
18
Course Title: Mobile
Application Development
19
Course Title: Mobile
Application Development
Lab No 3
3. Name Input Greeting App
Code for MainActivity.java
package com.example.akshay.mrcet;
editName = (EditText)
findViewById(R.id.editName);
editPassword = (EditText)
findViewById(R.id.editPassword); result =
(TextView) findViewById(R.id.tvResult);
buttonSubmit = (Button)
findViewById(R.id.buttonSubmit);
buttonReset = (Button)
findViewById(R.id.buttonReset);
/*
Submit Button
*/
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
20
Course Title: Mobile
Application Development
}
});
/*
Reset Button
*/
buttonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void
onClick(View v) {
editName.setText(
"");
editPassword.setT
ext("");
result.setText("");
editName.requestF
ocus();
}
});
}
}
activity_main.xml
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andr
oid" 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="#FFFF8D"
tools:context="com.example.akshay.mrcet.MainActivity"
>
<TextView
android:id="@+id/textV
iew"
21
Course Title: Mobile
Application Development
android:layout_width="
wrap_content"
android:layout_height="
wrap_content"
android:layout_alignPar
entLeft="true"
android:layout_alignPar
entStart="true"
android:layout_alignPar
entTop="true"
android:text="NAME"
android:textSize="20sp"
android:layout_margin=
"20dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_
content"
android:layout_height="wrap_
content"
android:textSize="20sp"
android:text="PASSWORD"
android:layout_marginTop="3
8dp"
android:layout_below="@+id/
textView"
android:layout_alignLeft="@+
id/textView"
android:layout_alignStart="@
+id/textView" /> <EditText
android:id="@+id/editName"
android:layout_width="wrap_
content"
android:layout_height="wrap_
content"
android:ems="10"
android:inputType="textPerso
nName"
android:hint="Enter Name"
android:layout_alignParentTop
="true"
android:layout_alignParentRig
22
Course Title: Mobile
Application Development
ht="true"
android:layout_alignParentEn
d="true"
android:layout_alignLeft="@+
id/editPassword"
android:layout_alignStart="@
+id/editPassword" />
<EditText
android:id="@+id/editPasswor
d"
android:layout_width="wrap_c
ontent"
android:layout_height="wrap_c
ontent" android:ems="10"
android:hint="Enter Password"
android:inputType="textPasswo
rd"
android:layout_alignBottom="
@+id/textView2"
android:layout_alignParentRigh
t="true"
android:layout_alignParentEnd
="true"
android:layout_marginRight="1
8dp"
android:layout_marginEnd="18
dp" />
<Button
android:id="@+id/button
Submit"
android:layout_width="wr
ap_content"
android:layout_height="w
rap_content"
android:layout_alignParen
tLeft="true"
android:layout_alignParen
tStart="true"
android:layout_below="@
+id/textView2"
android:layout_marginTo
23
Course Title: Mobile
Application Development
p="20dp"
android:text="SUBMIT" /
>
<Button
android:id="@+id/buttonReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESET"
android:layout_alignBaseline="@+id/button
Submit"
android:layout_alignBottom="@+id/buttonS
ubmit"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/tvRe
sult"
android:layout_width="
wrap_content"
android:layout_height=
"wrap_content"
android:layout_alignPa
rentBottom="true"
android:layout_alignPa
rentLeft="true"
android:layout_alignPa
rentStart="true"
android:layout_margin
Bottom="143dp"
android:textSize="30sp
" />
24
Course Title: Mobile
Application Development
</RelativeLayout>
Record Notes
25
Course Title: Mobile
Application Development
Lab 4
4. User Form with Submit
Code for MainActivity.java
package com.example.nkredy.mrcet;
editName = (EditText)
findViewById(R.id.editName);
editPassword = (EditText)
findViewById(R.id.editPassword);
editAddress = (EditText)
findViewById(R.id.editAddres); editAge =
(EditText) findViewById(R.id.editAge);
radioSexGender = (RadioGroup)
findViewById(R.id.radioGender);
26
Course Title: Mobile
Application Development
result = (TextView)
findViewById(R.id.tvResult); buttonSubmit =
(Button) findViewById(R.id.buttonSubmit);
buttonSubmit.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editName.getText().toString();
String password =
editPassword.getText().toString();
String address=
editAddress.getText().toString(); int selectedId
=
radioGenderGroup.getCheckedRadioButtonId()
;
}
});
}
}
activity_main.xml
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andr
oid" 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="#FFFF8D"
tools:context="com.example.akshay.mrcet.MainActivity"
>
27
Course Title: Mobile
Application Development
<TextView
android:id="@+id/textV
iew"
android:layout_width="
wrap_content"
android:layout_height="
wrap_content"
android:layout_alignPar
entLeft="true"
android:layout_alignPar
entStart="true"
android:layout_alignPar
entTop="true"
android:text="NAME"
android:textSize="20sp"
android:layout_margin=
"20dp" />
<TextView
android:id="@+id/textVi
ew2"
android:layout_width="
wrap_content"
android:layout_height="
wrap_content"
android:textSize="20sp"
android:text="PASSWO
RD"
android:layout_marginTo
p="38dp"
android:layout_below="
@+id/textView"
android:layout_alignLeft
="@+id/textView"
android:layout_alignStar
t="@+id/textView" />
<EditText
android:id="@+id/editName"
android:layout_width="wrap_cont
ent"
android:layout_height="wrap_cont
ent" android:ems="10"
28
Course Title: Mobile
Application Development
android:inputType="textPersonNa
me" android:hint="Enter
Name"
android:layout_alignParentTop="tr
ue"
android:layout_alignParentRight="
true"
android:layout_alignParentEnd="tr
ue"
android:layout_alignLeft="@+id/e
ditPassword"
android:layout_alignStart="@+id/
editPassword" />
<EditText
android:id="@+id/editPasswor
d"
android:layout_width="wrap_c
ontent"
android:layout_height="wrap_c
ontent" android:ems="10"
android:hint="Enter Password"
android:inputType="textPasswo
rd"
android:layout_alignBottom="
@+id/textView2"
android:layout_alignParentRigh
t="true"
android:layout_alignParentEnd
="true"
android:layout_marginRight="1
8dp"
android:layout_marginEnd="18
dp" />
<EditText
android:id="@+id/editAd
dress"
android:layout_width="wr
ap_content"
android:layout_height="w
rap_content"
android:layout_marginRig
29
Course Title: Mobile
Application Development
ht="18dp"
android:layout_marginEn
d="18dp" />
<EditText
android:id="@+id/editA
ge"
android:layout_width="w
rap_content"
android:layout_height="
wrap_content"
android:layout_marginRi
ght="18dp"
android:layout_marginEn
d="18dp" />
<RadioGroup
android:id="@+id/radioG
ender"
android:layout_width="wr
ap_content"
android:layout_height="w
rap_content" >
<RadioButton
android:id="@+id/radioM
ale"
android:layout_width="wr
ap_content"
android:layout_height="w
rap_content"
android:text="@string/rad
io_male"
android:checked="true" />
<RadioButton
android:id="@+id/radioFemal
e"
android:layout_width="wrap_
content"
android:layout_height="wrap_
content"
android:text="@string/radio_f
emale" />
30
Course Title: Mobile
Application Development
</RadioGroup>
<Button
android:id="@+id/button
Submit"
android:layout_width="wr
ap_content"
android:layout_height="w
rap_content"
android:layout_alignParen
tLeft="true"
android:layout_alignParen
tStart="true"
android:layout_below="@
+id/textView2"
android:layout_marginTo
p="20dp"
android:text="SUBMIT" /
>
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap
_content"
android:layout_height="wrap
_content"
android:layout_alignParentB
ottom="true"
android:layout_alignParentL
eft="true"
android:layout_alignParentSt
art="true"
android:layout_marginBotto
m="143dp"
android:textSize="30sp"
/></RelativeLayout>
31
Course Title: Mobile
Application Development
Excersise program: Create a screen that has input boxes for User Name, Password,
Address, Gender(radio buttons for male and female), Age (numeric), Date of Birth
(Date Picket), State (Spinner) and a Submit
button. On clicking the submit button, print all the data below the Submit Button
(use any layout)
Record Notes:
32
Course Title: Mobile
Application Development
Lab 5
5. Activity Intent Value Transfer
AIM: To design an android application to design a page using Intent and one Button
and pass the Values from one Activity to second Activity.
Main Activity Java File package com.example.transferofdata; import
android.os.Bundle; import android.app.Activity; import android.content.Intent; import
android.view.Menu; import android.view.View;
import
android.view.View.OnClic
kListener; import
android.widget.Button;
import
android.widget.EditText;
public class MainActivity
extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText
et=(EditText)findViewById(R.id.editText1);
final EditText
et1=(EditText)findViewById(R.id.editText2)
; Button
send=(Button)findViewById(R.id.button1);
send.setOnClickListener(new
OnClickListener()
{
@Override
public void onClick(View arg0)
33
Course Title: Mobile
Application Development
{
String s=et.getText().toString();
String s1=et1.getText().toString();
Intent it=new Intent(MainActivity.this,Second.class);
it.putExtra("uname", s);
it.putExtra("pwd", s1);
startActivity(it);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.mai
n, menu); return true;
}
}
SecondActivity.java
package
com.example.transfer
ofdata; import
android.os.Bundle;
import
android.app.Activity;
import
android.view.Menu;
import
android.widget.TextV
iew; public class
34
Course Title: Mobile
Application Development
Second extends
Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView
tv=(TextView)findViewById(R.id.textView1);
TextView
tv1=(TextView)findViewById(R.id.textView2);
tv.setText(getIntent().getExtras().getString("una
me"));
tv1.setText(getIntent().getExtras().getString("p
wd"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.secon
d, menu); return true;
}
}
MainActivity.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
35
Course Title: Mobile
Application Development
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="78dp"
android:layout_marginTop="154dp"
android:text="send" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content
"
android:layout_height="wrap_content
"
android:layout_alignParentTop="true
"
android:layout_alignRight="@+id/bu
tton1"
android:layout_marginTop="18dp"
android:ems="10" ><requestFocus
/></EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content
"
android:layout_height="wrap_content
36
Course Title: Mobile
Application Development
"
android:layout_alignRight="@+id/bu
tton1"
android:layout_below="@+id/editTe
xt1"
android:layout_marginTop="37dp"
android:ems="10"
android:inputType="textPassword" /></RelativeLayout>
SecondActivity.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Second" >
<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="42dp"
android:layout_marginTop="70dp"
android:text="uname" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
37
Course Title: Mobile
Application Development
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="15dp"
android:layout_marginTop="37dp"
android:text="password" />
</RelativeLayout>
OUTPUT:
38
Course Title: Mobile
Application Development
Lab 6
6. Send SMS via Intent
AIM : To design an android application Send
SMS using Intent. MainActivity.java
package com.example.sms; import
android.os.Bundle; import
android.app.Activity; import
android.telephony.gsm.SmsManager; import
android.view.Menu; import
android.view.View; import
android.view.View.OnClickListener; import
android.widget.Button; public class
MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState
{
super.onCreate(savedInstanceSt
ate);
setContentView(R.layout.activit
y_main); Button
bt=(Button)findViewById(R.id.
button1);
bt.setOnClickListener(new
OnClickListener()
{
@Overrid
e public
void
onClick(V
iew v)
39
Course Title: Mobile
Application Development
{
// TODO Auto-generated method stub
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage("5554", null, "hai", null, null);
}
});
}
MainActivity.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
40
Course Title: Mobile
Application Development
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="54dp"
android:layout_marginTop="166dp"
android:text="send" />
</RelativeLayout>
OUTPUT:
41
Course Title: Mobile
Application Development
42
Course Title: Mobile
Application Development
Record Notes:
43
Course Title: Mobile
Application Development
Lab 7
44
Course Title: Mobile
Application Development
7. Fragment-based Application
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="fill_parent"
android:layout_height="fill_parent"
tools:context="example.javatpoint.com.fragmentexample.MainActivity">
<fragment
android:id="@+id/fragment1"
android:name="example.javatpoint.com.fragmentexample.F
ragment1" android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<fragment
android:id="@+id/fragment2"
android:name="example.javatpoint.com.fragmentexample.F
ragment2" android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
fragment_fragment1.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F5F5DC"
tools:context="example.javatpoint.com.fragmentexample.Fragment1">
</FrameLayout>
File: fragment_fragment2.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0FFFF"
tools:context="example.javatpoint.com.fragmentexample.Fragment2">
</FrameLayout>
MainActivity class
File: MainActivity.java
package example.javatpoint.com.fragmentexample;
import
android.support.v7.app.AppCompatActivity
; import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
File: Fragment1.java
package example.javatpoint.com.fragmentexample;
import
android.os.Bundle;
import
android.support.v4.app.Fr
agment; import
android.view.LayoutInfla
ter; import
android.view.View;
46
Course Title: Mobile
Application Development
import android.view.ViewGroup;
@Override
public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle
savedInstanceState) { //
Inflate the layout for this
fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
}
File: Fragment2.java
package example.javatpoint.com.fragmentexample;
import
android.os.Bundle;
import
android.support.v4.app.Fr
agment; import
android.view.LayoutInfla
ter; import
android.view.View;
import android.view.ViewGroup;
@Override
public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
47
Course Title: Mobile
Application Development
Bundle
savedInstanceState) { //
Inflate the layout for this
fragment
return inflater.inflate(R.layout.fragment_fragment2, container, false);
}
}
Output:
Lab 8
48
Course Title: Mobile
Application Development
8. RadioButton UI App
AIM: To design an android application
Using Radiobuttons. MainActivity.java
package com.example.radiobutton; import
android.os.Bundle; import
android.app.Activity; import
android.view.View; import
android.view.View.OnClickListener; import
android.widget.Button; import
android.widget.RadioButton; import
android.widget.RadioGroup;
import
android.widget.RadioGroup.OnCheckedChang
eListener; import android.widget.TextView;
import android.widget.Toast; public class
MainActivity extends Activity
{
private RadioGroup
radioGroup; private
RadioButton sound,
vibration, silent; private
Button button; private
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
49
Course Title: Mobile
Application Development
@Override
public void onClick(View v) {
int selectedId = radioGroup.getCheckedRadioButtonId();
MainActivity.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
51
Course Title: Mobile
Application Development
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RadioGroup
android:id="@+id/myRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="27dp"
android:layout_marginTop="28dp" >
<RadioButton
android:id="@+id/soun
d"
android:layout_width="
wrap_content"
android:layout_height=
"wrap_content"
android:checked="true"
android:text="Sound" /
> <RadioButton
android:id="@+id/vibr
ate"
android:layout_width="
wrap_content"
android:layout_height=
"wrap_content"
android:text="Vibration
" /> <RadioButton
android:id="@+id/silen
t"
android:layout_width="
wrap_content"
52
Course Title: Mobile
Application Development
android:layout_height=
"wrap_content"
android:text="Silent" />
</RadioGroup> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Choose one of the following
modes"
android:textAppearance="?android:attr/textAppe
aranceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_co
ntent"
android:layout_height="wrap_c
ontent"
android:layout_alignLeft="@+i
d/myRadioGroup"
android:layout_below="@+id/m
yRadioGroup"
android:layout_marginLeft="42
dp"
android:layout_marginTop="53d
p" android:text="choose" />
</RelativeLayout>
OUTPUT:
53
Course Title: Mobile
Application Development
Lab 9
54
Course Title: Mobile
Application Development
9. Menu-based Application
AIM: To design an
application options menu.
MainActivity.java package
com.javatpoint.optionmenu;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import
android.view.MenuItem;
import android.widget.Toast;
public class MainActivity
extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);//Menu
Resource, Menu return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
55
Course Title: Mobile
Application Development
case R.id.item1:
Toast.makeText(getApplicationContext(),"Item 1
Selected",Toast.LENGTH_LONG).show(); return true; case R.id.item2:
Toast.makeText(getApplicationContext(),"Item 2
Selected",Toast.LENGTH_LONG).show(); return true; case R.id.item3:
Toast.makeText(getApplicationContext(),"Item 3
Selected",Toast.LENGTH_LONG).show(); return true; default:
return super.onOptionsItemSelected(item);
}
}
}
MainActivity.xml
<RelativeLayout
xmlns:androclass="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
SecondActivity.xml
<menu xmlns:androclass="http://schemas.android.com/apk/res/android" >
<item
android:id="@+
56
Course Title: Mobile
Application Development
id/item1"
android:title="It
em 1"/> <item
android:id="@+
id/item2"
android:title="It
em 2"/> <item
android:id="@+
id/item3"
android:title="It
em 3"/>
</menu>
OUTPUT:
Lab 10
57
Course Title: Mobile
Application Development
/**
* Created by tutlane on 06-01-2018.
*/
59
Course Title: Mobile
Application Development
}
return userList;
}
// Get User Details based on userid
public ArrayList<HashMap<String, String>> GetUserByUserId(int userid){
SQLiteDatabase db = this.getWritableDatabase();
ArrayList<HashMap<String, String>> userList = new ArrayList<>();
60
Course Title: Mobile
Application Development
61
Course Title: Mobile
Application Development
If you observe above code, we implemented all SQLite Database related activities to
perform CRUD operations in android application.
Now open activity_main.xml file from \res\layout folder path and write the code like as
shown below.
activity_main.xml
<?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">
<TextView
android:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
android:text="Name" />
<EditText
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10"/>
<TextView
android:id="@+id/secTxt"
62
Course Title: Mobile
Application Development
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/txtLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<TextView
android:id="@+id/thirdTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Designation"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/txtDesignation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Save" />
</LinearLayout>
63
Course Title: Mobile
Application Development
Now we will create another layout resource file details.xml in \res\layout path to show the
details in custom listview from SQLite Database for that right click on your layout folder à
Go to New à select Layout Resource File and give name as details.xml.
Once we create a new layout resource file details.xml, open it and write the code like as
shown below
details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/user_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp" />
<Button
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Back" />
</LinearLayout>
64
Course Title: Mobile
Application Development
Create an another layout file (list_row.xml) in /res/layout folder to show the data in
listview, for that right click on layout folder à add new Layout resource file à Give name as
list_row.xml and write the code like as shown below.
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" android:padding="5dip" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textStyle="bold"
android:textSize="17dp" />
<TextView
android:id="@+id/designation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_marginTop="7dp"
android:textColor="#343434"
android:textSize="14dp" />
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/desig
nation"
android:layout_alignBottom="@+id/design
ation"
65
Course Title: Mobile
Application Development
android:layout_alignParentRight="true"
android:textColor="#343434"
android:textSize="14dp" />
</RelativeLayout>
MainActivity.java
package
com.tutlane.sqliteexample;
import
android.content.Intent;
import
android.support.v7.app.AppCompat
Activity; import android.os.Bundle;
import android.view.View; import
android.widget.Button; import
android.widget.EditText; import
android.widget.Toast;
66
Course Title: Mobile
Application Development
name =
(EditText)findViewById(R.id.txtName);
loc =
(EditText)findViewById(R.id.txtLocation);
desig =
(EditText)findViewById(R.id.txtDesignation
); saveBtn =
(Button)findViewById(R.id.btnSave);
saveBtn.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
String username = name.getText().toString()+"\n";
String location = loc.getText().toString();
String designation = desig.getText().toString();
DbHandler dbHandler = new
DbHandler(MainActivity.this);
dbHandler.insertUserDetails(username,location,designation);
intent = new Intent(MainActivity.this,DetailsActivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Details Inserted
Successfully",Toast.LENGTH_SHORT).show();
}
});
}
}
OUTPUT:
67
Course Title: Mobile
Application Development
68