Saturday, November 30, 2013

zipalign

Before an application is uploaded into Google, it must be aligned to the 32bit boundary. The alignment will be checked by the Google during the upload process. If it fails, a pop up message will say that the .apk is not aligned and reject the upload.

The zipalign tool is located to /tools. You can call it directly or add it to your PATH environment variable.

To execute it, use the following

zipalign -v 4 < outfile.apk>

The outfile is now ready for upload into Google.


Saturday, November 16, 2013

Simple usage of MediaPlayer on Android

In the side project, I wanted to play a sound that I have embedded as part of the resources. After a short research, Android provides MediaPlayer to help you play sounds and videos. I am only going to use it playback some sounds.

Before I started, I browse over to the MediaPlayer docs on Google. The most important is the state diagram that it is displaying. From here, it is required for states of your implementation of the player to follow.

To start with, you will need to create your instance of MediaPlayer.

try {
    mp = MediaPlayer.create(getApplicationContext(), R.raw.whitenoise_30s);
} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
mp.setLooping(true);
mp.start(); 

When create() is called, the MediaPlayer enters into Prepared state. This means that we can immediately call the start()  and the MediaPlayer transitioned into the Started state. The audio will start to play on  the speaker.

To stop the playing, stop() can be called. When stop() is called, the MediaPlayer transitioned into Stopped. The audio will stop to play and be silent on the speaker.

To playback, instead of calling create() again, it is better to call prepare(). From the state diagram, the MediaPlayer will transition from Stopped to Prepared. A boolean could be used to track if the MediaPlayer has been created or not. This is to indicate whether create() or prepare() should be called. 

Saturday, November 02, 2013

Setting up the menu on an Android App.



As I am developing the Android App, I need to add a simple menu for popping up a dialog box to show the version of the application. As far as I know, the only menu that can be popped up is the Menu button. On a phone, this soft button is usually located on the bottom left hand corner.

I wanted to have a single menu item in the list. It is simple "About"

Firstly, I create a menu.xml and placed it in under /res/menu.

xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
   
    <item android:id="@+id/menu_about"
          android:title="About" />

</menu>

Secondly, I added the code that adds this menu. This code resides in onCreateOptionMenu() which is in MainActivity.java.


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        getMenuInflater().inflate(R.menu.menu, menu);
        super.onCreateOptionsMenu(menu);
        return true;
    }

In the snippet of code, I have also shown the code for the action bar. However, the line of the that creates the option menu the call to getMenuInflater(). Be sure that the ID of the xml file is referenced correctly.

When the menu button is clicked, a menu will pop up with only the "About" option displaying.

Thirdly, I created code to handle the event when the "About" option is selected. This is handled in the onOptionsItemSelected() method. So I have the following implementation.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_search:
        Toast.makeText(getApplication(), "search is selected",
Toast.LENGTH_SHORT).show();
        
            openSearch();
                return true;
            case R.id.action_settings:
            
        Toast.makeText(getApplication(), "settings is selected",
Toast.LENGTH_SHORT).show();
        
                openSettings();
                return true;
                
            case R.id.menu_about:
                // Single menu item is selected do something
                // Ex: launching new activity/screen or show alert message
                //Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
                
            String message = getString(R.string.app_name) + "\n" + getString(R.string.app_version);
            
    // pop up a very simple About dialog box.
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder
    .setMessage(message)
    .setPositiveButton("OK", null)
        .show();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

This implementation also includes actions for when items from the action bar is also selected.

So when the "About" is selected, a simple dialog box will pop up.

This code also abstract the strings to strings.xml so that it be changed easily. A snippet is shown below.


    <string name="app_name">BackgroundNoise</string>
    <string name="app_version">version 0.01</string>