0% found this document useful (0 votes)
29 views68 pages

Module-4 Updated

Uploaded by

MANJUNATHA H T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views68 pages

Module-4 Updated

Uploaded by

MANJUNATHA H T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

Module 4:

• Creating the Activity


Life cycle of an Activity
Module-4
Working with views

 Views are the building blocks of Android application’s

View classes represent elements on the screen and are responsible


for interacting with users through events
Every Android screen contains a hierarchical tree of View elements.
These views come in a variety of shapes and sizes
Exploring some common views

A view is a widget that has an appearance on

screen. It is derived from the base class

android.view.View. There are three types ie

Basic views
Picker views
Using a list view
• List and drop-down lists are commonly used in applications.
• Lists and drop-down lists are called ListView and Spinner controls in
Android
• A ListView is used to display a list of vertically scrolling items
• Allowing users to select one or more of them.
The ListView can be populated by one of the following two methods: •
By

• ListView through a string resource


• By ListView through Array Adapter
Add this code to Resource----Values----String.XML
resources>

<string name="app_name">ListViewApp</string>

<string-array name="fruits">

<item>Apple</item>
<item>Mango</item>
<item>Orange</item>
<item>Grapes</item>
<item>Banana</item>
</string-array>

</resources>
1. List the View through String Method
Write this code to Activity.java fiel
TextView selectedOpt=(TextView)findViewById(R.id.selectedopt);

ListView fruitsList = (ListView)findViewById(R.id.fruits_list);

final String[] fruitsArray =


getResources().getStringArray(R.array.fruits);

fruitsList.setOnItemClickListener(new
AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v, int
position,
long id)
{
selectedOpt.setText("You have selected
"+fruitsArray[position]);
}
});
Demonstrating List view through
array adapter
Step
1: Add the code to XML file
Next, we need to write code into the
Java activity.java file
• Create an ArrayAdapter through a string array and assign it to the
ListView for displaying items

• selected item will be displayed in textview from listview

Array adapter takes the parameter (i) layout XML file (ii) Array which
holds the items
final String[] fruits={"Apple", "Mango", "Orange", "Grapes", "Banana"};
ListView fruitsList = (ListView) findViewById(R.id.fruits_list);
TextView selectedOpt = (TextView) findViewById(R.id.selectedopt);

ArrayAdapter arrayAdpt= new ArrayAdapter(this, R.layout.simple_list_item_1, fruits);

fruitsList.setAdapter(arrayAdpt);

fruitsList.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView parent, View v, int position, long id) {

selectedOpt.setText("You have selected "+fruitsArray[position]);


}
});
}
• Demonstration of selecting single items in the
list view through array adapter
import android.os.Bundle;
import android.app.ListActivity;
import android.widget.TextView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;

public class MainActivity extends Activity


{
TextView selectedOpt;

String[] fruits={"Apple", "Mango", "Orange", "Grapes",


"Banana"};

Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)
selectedOpt=(TextView)findViewById(R.id.selectedopt);

Listview fruitlist = (ListView)findViewById(R.id.list);

//Creating an adapter
ArrayAdapter arrayAdpt = new ArrayAdapter(this, android.R.layout.actvity_main,
fruits);

//Selecting single element from the listview

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

fruitlist .setListAdapter(arrayAdpt);

}
//Display the content in the textview after selecting the item from the
listview
@Override
public void onListItemClick(ListView parent, View v, int position, long id)
{
super.onListItemClick(parent, v, position, id);
//item will be displayed in text view
selectedOpt.setText("You have selected "+fruits[position]); } }
Selecting Multiple Items

getListView().setChoiceMode(ListView.CHOICE
_MODE_MULTIPLE);
Spinner
• The Spinner is akin to a drop-down list that displays a list of items,
allowing the user to select the desired item
• Spinner implementation can be achieved in two ways

(i )string method
(ii) Adapter method
Implementation of a Spinner using String Method

Add this code to Resource----Values----String.XML


resources>

<string
name="app_name">ListViewApp</string>

<string-array name="fruits">
<item>Apple</item>
<item>Mango</item>
<item>Orange</item>
<item>Grapes</item>
<item>Banana</item>
</string-array>
</resources>
Insert this code to Activty.java file

public class MainActivity extends Activity


{
Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main)
TextView selectedOpt=(TextView)findViewById(R.id.selectedopt);

Spinner fruitsList = (Spinner)findViewById(R.id.Spinner);

String[] fruitsArray = getResources().getStringArray(R.array.fruits);

Spinner.setOnItemSelectedListener(new
AdapterView.OnItemClickListener(){
@Override
public void onItemSelected (AdapterView<?> parent, View v, int
position,
long id)
{
selectedOpt.setText("You have selected
"+fruitsArray[position]);
}
});
}
Multimedia

• Probably the most basic need for multimedia on a cell phone is the
ability to play audio files, whether new ringtones, MP3s, or quick
audio notes. Android’s Media Player is easy to use.
PLAYING AUDIO
• At a high level, all you need to do to play an MP3 file is follow these
steps

STEP 1 :Put the MP3 in the res/raw directory in a project

STEP 2: Create a new instance of the MediaPlayer, and reference the


MP3 by calling MediaPlayer.create()

STEP 3: Call the MediaPlayer methods prepare() and start()


XML File - Actvity _Main
)
public class MainActvity extends Activity {
public void onCreate(Bundle InstanceState) {
super.onCreate(icicle);
setContentView(R.layout.main);

Button mybutton = (Button) findViewById(R.id.playsong);

//when button is clicked the following function invokes


mybutton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
MediaPlayer mp = `
MediaPlayer.create(MainActvity.this, R.raw.halotheme);

mp.start();

mp.setOnCompletionListener(new OnCompletionListener(){
public void
Out put for playing audio project
Playing video
• For playing a video we are using “VideoView” widget
XML File
public class SimpleVideo extends Activity {
private VideoView myVideo;
private MediaController mc;

public void onCreate(Bundle icicle) {


super.onCreate(SavedInstanceState);
setContentView(R.layout.Activity_main);

myVideo = (VideoView) findViewById(R.id.video);

getWindow().setFormat(PixelFormat.TRANSLUCENT);

File pathToTest= new File


(Environment.getExternalFileDirectory(),"test.mp4");
mc = new MediaController(this);
mc.setMediaPlayer(myVideo);
myVideo.setMediaController(mc);
myVideo.requestFocus();
}
}
Playing video
• setMediaController() method to perform a callback to the VideoView
to notify it when our video is finished playing.

• setMediaPlayer() which sets the media player to VideoView and it


creates three buttons (i)forward (ii)backward (iii )play;
output for playing video
NETWORKING(Internet )
• One can communicate with the external world via SMS, email or using
HTTP protocol.
• Using the HTTP protocol, you can perform a wide variety of tasks,
such as downloading web pages from a web server, downloading
binary data, and so on

• Lets see how to connect to the network(web) through HTTP


STEP 1: Create a new android project and name it as “Networking”

STEP 2: Add the following line in AndroidManifest.xml file:

STEP 3: Write the code for JAVA file


Downloading the Binary
Data(Image)
• XML File
JAVA file
Output for the downloaded image
CREATING YOUR OWN SERVICES
• A service is an application in Android that runs in the background
without needing to interact with the user

• For example, while using an application, you may want to play some
background music at the same time. In this case, the code that is
playing the background music has no need to interact with the user,
Following are the steps involved in creating own
service.
• Add a new class file to the project and name it MyService.java. Write
the following code in it:

It binds Activity and services

Called when you start the service


What Are Dialogs?
• We usually create a new activity or screen for interacting with users,
• but when we want only a little information, or want to display an
essential message, dialogs are preferred
• Dialog—The basic class for all dialog types.

• AlertDialog—A dialog with one, two, or three Button controls.

• CharacterPickerDialog—A dialog that enables you to select an


accented character associated with a regular character source.


DatePickerDialog—A dialog that enables you to set and
select a date with a DatePicker control.


ProgressDialog—A dialog that displays a ProgressBar control showing
the progress of a designated operation.

TimePickerDialog—A dialog that enables you to set and select a time


with a TimePicker control
• The following is a list of the Activity class dialog methods:

• showDialog()—Displays a dialog and creates a dialog if one does not exist. Each dialog

has a special dialog identifier that is passed to this method as a parameter.

• onCreateDialog()—The callback method that executes when the dialog is created for

the first time. It returns the dialog of the specified type.

• onPrepareDialog()—The callback method used for updating a dialog.

• dismissDialog()—Closes the dialog whose dialog identifier is supplied to this method.

The dialog can be displayed again through the showDialog() method. removeDialog()

—The dismissDialog() method doesn’t destroy a dialog. The dismissed dialog can be

redisplayed from the cache. If we do not want to display a dialog, we can remove it
Getting the input from dialog box
Output
Fragments
Fragments and its life cycle

You might also like