JNTUA Mobile App Development Manual
JNTUA Mobile App Development Manual
LAB MANUAL
Prepared By [Link]
INDEX
[Link]();
Log.d(msg, "The onPause() event");
}
/** Called when the activity is no longer visible. */
@Override
protected void onStop() {
[Link]();
Log.d(msg, "The onStop() event");
}
/** Called just before the activity is destroyed. */
@Override
public void onDestroy() {
[Link]();
Log.d(msg, "The onDestroy() event");
}
}
An activity class loads all the UI component using the XML file available in res/layout folder
of the project. Following statement loads UI components from res/layout/activity_main.xml
file:
setContentView([Link].activity_main);
Aim: Create Application by Using Building Blocks for Android Application Design (Absolute
Layout).
Theory:
An Absolute Layout lets you specify exact locations (x/y coordinates) of its children. Absolute
layouts are less flexible and harder to maintain than other types of layouts without absolute
positioning.
Following is the content of the modified main activity
file src/[Link]/[Link].
This file can include each of the fundamental lifecycle methods.
Source Code:
[Link]
package [Link];
import [Link];
import [Link];
}
Following will be the content of res/layout/activity_main.xml file –
<AbsoluteLayout xmlns:android="[Link]
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="OK"
android:layout_x="50px"
android:layout_y="361px" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_x="225px"
android:layout_y="361px" />
</AbsoluteLayout>
Following will be the content of res/values/[Link] to define two new constants −
Output:
Let's write the code of to play the audio file. Here, we are going to play maine.mp3 file
located inside the sdcard/Music directory.
Source code:
File: [Link]
package [Link].audiomediaplayer1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}catch(Exception e){[Link]();}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate([Link].activity_main, menu);
return true;
}
<RelativeLayout xmlns:androclass="[Link]
xmlns:tools="[Link]
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_alignParentTop="true"
android:text="Audio Controller" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="48dp"
android:text="start" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="pause" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="stop" />
</RelativeLayout>
Let's write the code to start, pause and stop the audio player.
File: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
start=(Button)findViewById([Link].button1);
pause=(Button)findViewById([Link].button2);
stop=(Button)findViewById([Link].button3);
//creating media player
final MediaPlayer mp=new MediaPlayer();
try{
//you can change the path, here path is external direc
tory(e.g. sdcard) /Music/maine.mp3
[Link]([Link]().get
Path()+"/Music/maine.mp3");
[Link]();
}catch(Exception e){[Link]();}
[Link](new OnClickListener() {
@Override
public void onClick(View v) {
[Link]();
}
});
[Link](new OnClickListener() {
@Override
public void onClick(View v) {
[Link]();
}
});
Output:
Theory:
Source code:
File: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link] xmlns:android="[Link]
.com/apk/res/android"
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pizza"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coffee"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Burger"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox2" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox3" />
</[Link]>
File: [Link]
package [Link];
});
}
}
Output:
Theory:
Android LinearLayout is a view group that aligns all children in
either vertically or horizontally.
Source code:
[Link]
package [Link];
import [Link];
import [Link];
<Button android:id="@+id/btnStartService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="start_service"/>
<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>
Output:
If we group Radio Buttons using RadioGroup, at a time only one item can be selected from the group of
radio buttons. In case, if we select one radio button that belongs to a radio group will unselect all other
previously selected radio buttons within in the same group.
Initially, all the radio buttons of radio group are in unchecked state, once we select a radio button then it’s
not possible for us to uncheck it like CheckBox control.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent" android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"/>
</RadioGroup>
</RelativeLayout>
Source code:
Now open an activity_main.xml file from \res\layout path and write the code like as
shown below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="100dp"
android:textSize="18dp"
android:text="Select Your Course"
android:textStyle="bold"
android:id="@+id/txtView"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/rdGroup"
android:layout_below="@+id/txtView">
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void onClick(View v) {
String result = "Selected Course: ";
result+=
([Link]())?"Android":([Link]())?"AngularJS":([Link]())?"Java":([Link]
Checked())?"Python":"";
[Link](getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
});
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
String str="";
// Check which radio button was clicked
switch([Link]()) {
case [Link]:
if(checked)
str = "Android Selected";
break;
case [Link]:
if(checked)
str = "AngularJS Selected";
case [Link]:
if(checked)
str = "Java Selected";
break;
case [Link]:
if(checked)
str = "Python Selected";
break;
}
[Link](getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
Output:
Theory:
Android RelativeLayout enables you to specify how child views are positioned relative to each
other. The position of each view can be specified as relative to sibling elements or relative to the
parent.
Procedure:
Step 1 You will use Android Studio IDE to create an Android application and name it
as demo under a package [Link] as explained in the Hello World
Example chapter.
Step 4 Run the application to launch Android emulator and verify the result of the
changes done in the application.
Source Code :
[Link]
package [Link];
import [Link];
import [Link];
}
Following will be the content of res/layout/activity_main.xml file −
<RelativeLayout xmlns:android="[Link]
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder" />
<LinearLayout
Theory:
Android Spinner is like the combox box of AWT or Swing. It can be used to display the
multiple options to the user in which only one item can be selected by the user.
Android spinner is like the drop down menu with multiple values from which the end user
can select only one value.
Android spinner is associated with AdapterView. So you need to use one of the adapter
classes with spinner.
Source code:
File: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link] xmlns:android="[Link]
[Link]/apk/res/android"
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]">
<Spinner
android:id="@+id/spinner"
android:layout_width="149dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
</[Link]>
File: [Link]
package [Link];
Output:
By the help of MediaController and VideoView classes, we can play the video files in android.
MediaController class
The [Link] is a view that contains media controls like
play/pause, previous, next, fast-forward, rewind etc.
VideoView class
The [Link] class provides methods to play and control the video
player.
activity_main.xml
Drag the VideoView from the pallete, now the activity_main.xml file will like this:
Source Code:
File: activity_main.xml
<RelativeLayout xmlns:androclass="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<VideoView
android:id="@+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
</RelativeLayout>
File: [Link]
package [Link].video1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate([Link].activity_main, menu);
return true;
}
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/name"
android:inputType="textPersonName"
android:selectAllOnFocus="true" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/password_0_9"
android:inputType="numberPassword" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/e_mail"
android:inputType="textEmailAddress" />
<EditText
<EditText
android:id="@+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/contact_number"
android:inputType="phone" />
<Button
android:id="@+id/button"
style="@android:style/[Link]"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/submit"
android:textSize="16sp"
android:textStyle="normal|bold" />
</RelativeLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Button submit;
EditText name, password, email, contact, date;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
name = (EditText) findViewById([Link].editText1);
password = (EditText) findViewById([Link].editText2);
email = (EditText) findViewById([Link].editText3);
date = (EditText) findViewById([Link].editText4);
contact = (EditText) findViewById([Link].editText5);
submit = (Button) findViewById([Link]);
[Link](new [Link]() {