protected void loadStateNavBar(int pageId, final String stateKey) {
ImageView btnStates1 = (ImageView) findViewById(R.id.btnStates1);
btnStates1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent4 = new Intent(getApplicationContext() ,StateDetails.class);
intent4.putExtra(GlobalConstants.EXTRA_STATE_KEY, stateKey);
startActivity(intent4);
finish();
}
});
ImageView btnStates2 = (ImageView) findViewById(R.id.btnStates2);
btnStates2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent4 = new Intent(getApplicationContext() ,StateContacts.class);
intent4.putExtra(GlobalConstants.EXTRA_STATE_KEY, stateKey);
startActivity(intent4);
finish();
}
});
ImageView btnStates3 = (ImageView) findViewById(R.id.btnStates3);
btnStates3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent4 = new Intent(getApplicationContext() ,StateLinks.class);
intent4.putExtra(GlobalConstants.EXTRA_STATE_KEY, stateKey);
startActivity(intent4);
finish();
}
});
btnStates1.setImageResource(R.drawable.statedetails_off);
btnStates2.setImageResource(R.drawable.statecontacts_off);
btnStates3.setImageResource(R.drawable.statelinks_off);
switch (pageId)
{
case 1:
btnStates1.setClickable(false);
btnStates1.setImageResource(R.drawable.statedetails_on);
break;
case 2:
btnStates2.setClickable(false);
btnStates2.setImageResource(R.drawable.statecontacts_on);
break;
case 3:
btnStates3.setClickable(false);
btnStates3.setImageResource(R.drawable.statelinks_on);
break;
default:
break;
}
}
Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts
Tuesday, April 3, 2012
Replicating Tab Functionality
4/5/2012- Doing some research for another client, it turns out that in Android 3.0, TabActivity was deprecated and should not be used moving forward.
The Android tab activity is generally not what my clients want. It is hard to customize and clunky. My current client wants an Android Activity with a tab-like gui. Rather than using the Android tab activityI'm replicating by doing a linear layout of image tabs at the top of the screen with on-off states. One additional bit of tab functionality that I added is how to handle the back button to avoid adding the individual activities to the stack.
Most clients want the back button to exit all the clicked tabs while the Android stack keeps adding child activities to the stack.
For example if you have a list screen State List that goes to a "tabbed details screen" consisting of three tabs,
State Details, State Contacts and State Links.
If you click all the tabs, your activity stack would be State List-->State Details--> State Contacts-->State Links. Clicking back from State Links would take you to the state Contacts screen, when you really want to go to the State List screen. This is easily fixed by adding a finish(); after each startActivity like in my loadStateNavBar method below.
Monday, April 2, 2012
Streaming Media Files using Media Player Example
A recent client for an Android project asked for an Android activity that would display an image that you can click to play a sound. Starting with Android 2.2, streaming mp3 from a url became much easier and supported within the Android base code. Unfortunately, it doesn't work reliably. However, downloading the mp3 sound files and playing them locally is very reliable. So an Android background service (not shown) downloads files as needed when the app starts and stores them locally. The mp3 sound files are small and this method pretty much ensures that they can be played i.e. doesn't depend on Internet connectivity and enables offline mode. Be sure to release the media player when you're done with the Android activity.
if (mAudioURL!=null) {
try
{
String localFilePath = cacheDirectory
+ FileUtils.getFileNameForUrl( mAudioURL);
if (CustomApplication.DEBUG)
{
Log.d(TAG, "Looking for Local Image File is at: "
+ filePath);
}
if (filePath != null && !filePath.equals(cacheDirectory)
&& FileUtils.doesFileExist(filePath))
{
mMediaPlayer.setDataSource(filePath);
mMediaPlayer.prepare();
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});;
mMediaPlayer.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mp) {
mMediaPlayer.reset();
}
});
}
} catch (IllegalStateException e)
{
// e.printStackTrace();
}
catch (IllegalArgumentException e)
{
// e.printStackTrace();
}
catch (IOException e)
{
// e.printStackTrace();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
Subscribe to:
Posts (Atom)