Module-4 Updated
Module-4 Updated
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
<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);
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
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);
fruitsList.setAdapter(arrayAdpt);
fruitsList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)
selectedOpt=(TextView)findViewById(R.id.selectedopt);
//Creating an adapter
ArrayAdapter arrayAdpt = new ArrayAdapter(this, android.R.layout.actvity_main,
fruits);
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
<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
setContentView(R.layout.activity_main)
TextView selectedOpt=(TextView)findViewById(R.id.selectedopt);
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
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;
getWindow().setFormat(PixelFormat.TRANSLUCENT);
• 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:
•
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.
• showDialog()—Displays a dialog and creates a dialog if one does not exist. Each dialog
• onCreateDialog()—The callback method that executes when the dialog is created for
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