Saturday, September 21, 2013

ANDROID PROGRAMMING 3

1. Sound

1.1. Put the audio file into res/raw. 
1.2. Declaration
MediaPlayer mp;
mp = MediaPlayer.create(MainActivity.this, R.raw.btnsound); 
1.3. Call
mp.start();
2. Timer
2.1. Declaration 
Timer T=new Timer();
2.2. Schedule a task to do for every 1000ms (with 0ms delay)
T.scheduleAtFixedRate(new TimerTask() {         
 @Override
 public void run() {
             second++;
 }
}, 0, 1000); 
3. ProgressBar
3.1. Declaration in res/layout
 <ProgressBar
        android:id="@+id/pbtientrinh"
        style="?android:attr/progressBarStyleHorizontal" />
3.2. Set progress at 50%
pbtientrinh.setProgress(50);
4. Menu
4.1. Declaration in onCreateOptionsMenu method
public boolean onCreateOptionsMenu(Menu menu) {
 super.onCreateOptionsMenu(menu);
 menu.add(Menu.NONE,Menu.FIRST,0,R.string.strbabout).setIcon(R.drawable.softauthor);
 menu.add(Menu.NONE,Menu.FIRST+1,1,R.string.strbquit).setIcon(R.drawable.exit);
 return true;
}
Note: The menu.add(GroupID, ItemID, Order, Title) methods:
   + GroupID: groups related menu items.
   + ItemID: identifies the menu item.
   + Order: controls the order of menu items in a menu. There are some constants defined for sort order for different kinds of menu items Like Menu.CATEGORY_SECONDRY for secondry menu items, Menu.CATEGORY_SYSTEM for system menu items and Menu.CATEGORY_ALTERNATIVE for altenative menu items..
   + Title: the text of the menu item.
4.2. Set behavior for each menus in onOptionsItemSelected method
public boolean onOptionsItemSelected(MenuItem item){
 switch(item.getItemId()){
  case MENU_ABOUT:
   about();
   break;
  case MENU_RESTART:
   restart();
   break;
  case MENU_QUIT:
   finish();
  break;
 }
 return super.onOptionsItemSelected(item);
}
5. AlertDialog
Example:
new AlertDialog.Builder(this)
 .setTitle("Author")
 .setMessage("Ngoc-My Nguyen\n(阮玉美-0260827)")
 .setPositiveButton("OK",new DialogInterface.OnClickListener(){
  public void onClick(DialogInterface dialoginterface, int i){}
 })
 .show();

0 comments:

Post a Comment