Translate

Sunday, December 30, 2012

Android App Development 101 3rd tutorial; MediaPlayer Basics

In this tutorial, I will be be making a basic android media player which streams a mp3 file from a url. Just like previous tutorial, this app has two buttons, one to play the music and the second to stop the music, release the media player object and exit the app. Here is the code:

main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/Button1"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:text="Start"
        android:gravity="left|center"
        android:onClick="playMusic"/>
   
    <Button
        android:id="@+id/Button2"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:text="Stop"
        android:gravity="left|center"
        android:onClick="stopMusic"/>
 

</LinearLayout>

MediaBasic.java


package com.peshal.mediaplayerbasics;

import java.io.IOException;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;

public class MediaBasic extends Activity {
MediaPlayer ourmediaPlayer = new MediaPlayer();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


}
public void playMusic(View view) {
String url="http://archive.org/download/testmp3testfile/mpthreetest.mp3";
ourmediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
ourmediaPlayer.setDataSource(url);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
ourmediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ourmediaPlayer.start();

}
public void stopMusic(View view) {
ourmediaPlayer.stop();
ourmediaPlayer.release();
this.finish();

}

}


Tuesday, December 25, 2012

Android App Development 101 2nd tutorial; xml, buttons, toast, switch case statement

In this tutorial i will be using the xml attributes that i discussed in the last tutorial. The goal of this tutorial is to familiarize with with some of the key concepts that are very common in android app development. We will be making an app that displays two buttons and when these buttons are clicked, a toast message with show up corresponding to that button. Toast messages are used in android when we have to display some message quick. For instance "push back again to exit" is one common toast message. Here is our code:

main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/Button1"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:text="Button1"
        android:gravity="left|center"
        android:onClick="displayToast"/>
   
    <Button
        android:id="@+id/Button2"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:text="Button2"
        android:gravity="left|center"
        android:onClick="displayToast"/>
 

</LinearLayout>

Buttons.java


package com.peshal.testappbuttons;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Toast;

public class Buttons extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void displayToast(View view) {


switch(view.getId()) {
case R.id.Button1:
Toast.makeText(getApplicationContext(), "Button 1 is Pressed", Toast.LENGTH_SHORT).show();
break;
case R.id.Button2:
Toast.makeText(getApplicationContext(), "Button 2 is Pressed", Toast.LENGTH_SHORT).show();
break;
}
}


}




Saturday, December 1, 2012

Android App Development 101 1st tutorial, Learning xml attributes


Xml's are huge in android. You can accomplish lots of things using just xmls from animations to layouts. Besides xml lets you create your layout without writing any java code and see how it looks on a emulator or an actual device. In this post i will be talking about some of the basic xml attributes. These attributes will be frequently used in upcoming tutorials.

android:id="@+id/uniqueid"

The ids should be unique for each object whether its a button, textView, imageView or videoView. Unique ids are important when we have to reference that particular object in our java class.

android:height="x" and android:width="x"
x= xdp, wrap_content, fill_parent etc

wrap_content as the name suggest wraps the object and fill_parent fills the whole layout.

android:gravity = "x"
x = top, bottom,center,left,right etc

gravity attribute is used to locate the object in the layout.

android:onClick ="yourMethod"

onClick is another important attribute. For eg a button is given an onClick attribute. What is does is when the user clicks on that button, it runs the method that was listed in the xml in your java class file.

android:text="yourtext"

android:background="your color"

your color can use various formats. I like using the html color codes. For eg white would be #FFFFFF

you can find other color codes here.

There are lot more attributes, i will explain those as they come along.




Thursday, September 13, 2012

Bouncing Particle Live Wallpaper

My first android live wallpaper is live on android market. It uses a lot of physics components like speed, rotation, gravity, scale and transparancy with a huge customization features. Users have the ability to select the number of particles they desire, speed of the particles, background color and particle type with more features coming soon. check out the video demonstration of the live wallpaper in action below
http://www.youtube.com/watch?v=ezHNGqXfJRI&feature=youtu.be

Thursday, July 26, 2012

Android 4.1 Jelly Bean: What Changed

Android Jelly Bean brought a lot of new feature and enhancements to already popular android ice cream sandwich. Jelly bean makes the whole UI very fluid removing some lagginess that existed even with ice cream sandwich. Thanks to Project Butter for this. Besides Project Butter, Google now was also introduced with jelly bean which gives voice feedback to users question and requests. Google now also shows relevant notification based on users preferences if the user opts to use it. Overall the UI did not change much. Some changes are seen in the google search widget, lockscreen and camera app.