***Last updated on 11 May 2013 ***

Wednesday, 17 April 2013

How to play or insert music in your android application

Welcome back friends after a long time. Apologies for the delay, this tute is going to tell you as how you can play music in your android app. Well to begin with I created a new project named as "Chooseme". What I am going to do is that when I would run my application then a music will be played and that too of my own choice. :-)

Ok friends just follow the following steps which are supported by the screenshot. First of all you should know that the activity in which I will be inserting the music is named as "Home.java" which will be the home-screen or you can call the menu screen of my application.

Follow the following steps

1. Create a sub folder named "raw" in your "res" folder.

2. Right click on "res" then "New" and then "Folder"



3. Now copy any "mp3" music in your raw folder.


4. Write the following code below the line "setContentView(R.layout.home);" of your activity. Refer the screenshot

     "MediaPlayer startingmusic = MediaPlayer.create(Home.this, R.raw.musichome);
      startingmusic.start();"


Save the project and run your app. Yes thats it!! Sweet and simple

EXPLANATION

MediaPlayer - is a class which is used when handling music related files.
startingmusic - It is just a variable name you can name it whatever you want
musichome - It is the name of my mp3 file which I pasted in raw folder
Rest all is just compulsory code like Home.this implies that we are dealing with the current class.
start() - This method plays the music and similarly you can use stop() method to stop the music.
In case you face any problem comment here.

Monday, 1 April 2013

How to insert a cool background in your android application

Well folks this tutorial is going to tell you as how you can get a cool background for your android app. The final product after this tutorial is going to look like this.
Step 1

Get a 400 X 600 image or crop an image of somewhat this dimension. Save it as a png format. In case you have a jpeg image convert it into a png. Edit it using paint and then save it as a png. The reason why I am focusing on png is that it is a standard image format.

You can download the background image that I have used from here : Background.png

Step 2

Save this image to the drawable-hdpi folder. See the screenshot below.

Step 3

Open the layout in which you want to insert the background image and add the following code in the layout

android:background="@drawable/bgimg"


And thats it you are done. Your final code should be like this.




Friends only have a look at the code that has to be inserted. I have used Android 4.2 here but that does not matters. In your android 2.2 development only add the "android:background" line to the respective layout. Any confusion or issues just comment here.