0% found this document useful (0 votes)
6 views30 pages

Android Google Maps v2 Tutorial

The document provides a step-by-step guide for creating a simple Android Media Player application, including layout design, source code implementation, and Android Manifest configuration. It also outlines the process for preparing the application for release, exporting it as an APK, and registering on Google Play for distribution. Key functionalities include playing, pausing, and seeking through audio tracks, along with UI elements like TextViews and SeekBars.

Uploaded by

Bhumika Kachhava
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views30 pages

Android Google Maps v2 Tutorial

The document provides a step-by-step guide for creating a simple Android Media Player application, including layout design, source code implementation, and Android Manifest configuration. It also outlines the process for preparing the application for release, exporting it as an APK, and registering on Google Play for distribution. Key functionalities include playing, pausing, and seeking through audio tracks, along with UI elements like TextViews and SeekBars.

Uploaded by

Bhumika Kachhava
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Media Player

2. Creating the layout of the main Activity


We are going to make a very simple layout xml for our activity, that only consists of
a LinearLayout that contains the graphical elements for our Media Player.
Open res/layout/activity_main.xml , go to the respective xml tab and paste the following:
activity_main.xml
01 <LinearLayout xmlns:android="[Link]

02 xmlns:tools="[Link]

03 android:id="@+id/container"

04 android:layout_width="match_parent"

05 android:layout_height="match_parent"

06 android:layout_gravity="center"

07 android:background="#333333"

08 android:orientation="vertical"

09 android:paddingBottom="@dimen/activity_vertical_margin"

10 android:paddingLeft="@dimen/activity_horizontal_margin"

11 android:paddingRight="@dimen/activity_horizontal_margin"

12 android:paddingTop="@dimen/activity_vertical_margin" >

13

14 <TextView

15 android:id="@+id/songName"

16 android:layout_width="wrap_content"

17 android:layout_height="wrap_content"

18 android:layout_gravity="center"

19 android:text="songName" />
20

21 <ImageView

22 android:id="@+id/mp3Image"

23 android:layout_width="match_parent"

24 android:layout_height="200dp"

25 android:padding="30dp"

26 android:src="@drawable/music"

27 android:background="#ffffff"

28 android:layout_margin="30dp" />

29

30 <TextView

31 android:id="@+id/songDuration"

32 android:layout_width="wrap_content"

33 android:layout_height="wrap_content"

34 android:layout_gravity="center"

35 android:text="songDuration" />

36

37 <SeekBar

38 android:id="@+id/seekBar"

39 android:layout_width="match_parent"

40 android:layout_height="wrap_content" />

41

42 <LinearLayout

43 android:layout_width="match_parent"

44 android:layout_height="match_parent"
45 android:layout_marginTop="30dp"

46 android:gravity="center_horizontal"

47 android:orientation="horizontal" >

48

49 <ImageButton

50 android:id="@+id/media_rew"

51 android:layout_width="wrap_content"

52 android:layout_height="wrap_content"

53 android:layout_marginLeft="14dp"

54 android:onClick="rewind"

55 android:src="@android:drawable/ic_media_rew" />

56

57 <ImageButton

58 android:id="@+id/media_pause"

59 android:layout_width="wrap_content"

60 android:layout_height="wrap_content"

61 android:layout_marginLeft="14dp"

62 android:onClick="pause"

63 android:src="@android:drawable/ic_media_pause" />

64

65 <ImageButton

66 android:id="@+id/media_play"

67 android:layout_width="wrap_content"

68 android:layout_height="wrap_content"

69 android:layout_marginLeft="14dp"
70 android:onClick="play"

71 android:src="@android:drawable/ic_media_play" />

72

73 <ImageButton

74 android:id="@+id/media_ff"

75 android:layout_width="wrap_content"

76 android:layout_height="wrap_content"

77 android:layout_marginLeft="14dp"

78 android:onClick="forward"

79 android:src="@android:drawable/ic_media_ff" />

80 </LinearLayout>

81

82 </LinearLayout>

You can notice that we will use the smart android:onClick attribute from the xml, in order to
avoid setting onTouch listeners in the main Android Java Code. The onClick attribute describes
which method is going to “catch” this event.
Also, do not forget to insert a sound clip in the res/raw folder of the project. We have dragged
and dropped the sample_song.mp3 file.
Figure 8: Inserted an mp3 file inside raw folder
We have also added an image jpg file, in res/drawable folders of the project. This image is
used as a background source of an ImageView in our layout xml file.

3. Creating the source code of the main Activity


Open src/[Link]/[Link] file and paste the
code below.
[Link]
01 package [Link];

02

03 import [Link];

04

05 import [Link];
06 import [Link];

07 import [Link];

08 import [Link];

09 import [Link];

10 import [Link];

11 import [Link];

12

13 public class AndroidMediaPlayerExample extends Activity {

14

15 private MediaPlayer mediaPlayer;

16 public TextView songName, duration;

17 private double timeElapsed = 0, finalTime = 0;

18 private int forwardTime = 2000, backwardTime = 2000;

19 private Handler durationHandler = new Handler();

20 private SeekBar seekbar;

21

22 @Override

23 protected void onCreate(Bundle savedInstanceState) {

24 [Link](savedInstanceState);

25

26 //set the layout of the Activity

27 setContentView([Link].activity_main);

28

29 //initialize views

30 initializeViews();
31 }

32

33 public void initializeViews(){

34 songName = (TextView) findViewById([Link]);

35 mediaPlayer = [Link](this, [Link].sample_song);

36 finalTime = [Link]();

37 duration = (TextView) findViewById([Link]);

38 seekbar = (SeekBar) findViewById([Link]);

39 [Link]("Sample_Song.mp3");

40

41 [Link]((int) finalTime);

42 [Link](false);

43 }

44

45 // play mp3 song

46 public void play(View view) {

47 [Link]();

48 timeElapsed = [Link]();

49 [Link]((int) timeElapsed);

50 [Link](updateSeekBarTime, 100);

51 }

52

53 //handler to change seekBarTime

54 private Runnable updateSeekBarTime = new Runnable() {

55 public void run() {


56 //get current position

57 timeElapsed = [Link]();

58 //set seekbar progress

59 [Link]((int) timeElapsed);

60 //set time remaing

61 double timeRemaining = finalTime - timeElapsed;

[Link]([Link]("%d min, %d sec",


[Link]((long) timeRemaining),
62 [Link]((long) timeRemaining) -
[Link]([Link]((long)
timeRemaining))));

63

64 //repeat yourself that again in 100 miliseconds

65 [Link](this, 100);

66 }

67 };

68

69 // pause mp3 song

70 public void pause(View view) {

71 [Link]();

72 }

73

74 // go forward at forwardTime seconds

75 public void forward(View view) {

//check if we can go forward at forwardTime seconds before song


76
endes

77 if ((timeElapsed + forwardTime) 0) {
78 timeElapsed = timeElapsed - backwardTime;

79

80 //seek to the exact second of the track

81 [Link]((int) timeElapsed);

82 }

83 }

84

85 }

Let’s see in detail the code above.


We set the activity_main.xml layout and we initialize our basic views by:
1 setContentView([Link].activity_main);

2 initializeViews();

With [Link](); we can start the audio playback, and with timeElapsed =

[Link](); we can take the exact milliseconds in order to set the seekbar
progress [Link]((int) timeElapsed);
1 // play mp3 song

2 public void play(View view) {

3 [Link]();

4 timeElapsed = [Link]();

5 [Link]((int) timeElapsed);

6 [Link](updateSeekBarTime, 100);

7}

You can also learn more about Android SeekBar in the JavaCodeGeeks Android SeekBar
Example.
As you can see, we use a Handler in order to continuously update the seekbar progress and
the time remaining duration. In order to achive this we have added the method:
01 //handler to change seekBarTime

02 private Runnable updateSeekBarTime = new Runnable() {

03 public void run() {


04 //get current position

05 timeElapsed = [Link]();

06 //set seekbar progress

07 [Link]((int) timeElapsed);

08 //set time remaing

09 double timeRemaining = finalTime - timeElapsed;

[Link]([Link]("%d min, %d sec",


[Link]((long) timeRemaining),
10 [Link]((long) timeRemaining) -
[Link]([Link]((long)
timeRemaining))));

11

12 //repeat yourself that again in 100 miliseconds

13 [Link](this, 100);

14 }

15 };

This method is continuously executed, with a delay of 100 milliseconds. You can also learn
more about Android Handler in the JavaCodeGeeks Android Handler Example.
We alse move our current playback back position forwards and backwards, but we should in
the first place check if this move is valid by:
01 // go forward at forwardTime seconds

02 public void forward(View view) {

//check if we can go forward at forwardTime seconds before song


03
endes

04 if ((timeElapsed + forwardTime) <= finalTime) {

05 timeElapsed = timeElapsed + forwardTime;

06

07 //seek to the exact second of the track

08 [Link]((int) timeElapsed);

09 }
10 }

4. Android Manifest
The [Link] of our project is simple and basic. We have only blocked our
orientation to portait by adding the line: android:screenOrientation="portrait"
[Link]
01 <?xml version="1.0" encoding="utf-8"?>

02 <manifest xmlns:android="[Link]

03 package="[Link]"

04 android:versionCode="1"

05 android:versionName="1.0" >

06

07 <uses-sdk

08 android:minSdkVersion="8"

09 android:targetSdkVersion="19" />

10

11 <application

12 android:allowBackup="true"

13 android:icon="@drawable/ic_launcher"

14 android:label="@string/app_name" >

15 <activity

1 android:name="[Link]
6 dMediaPlayerExample"

17 android:screenOrientation="portrait"

18 android:label="@string/app_name" >

19 <intent-filter>

20 <action android:name="[Link]" />


21 <category android:name="[Link]" />

22 </intent-filter>

23 </activity>

24 </application>

25

26 </manifest>

5. Build, compile and run


When we build, compile and run our project, the main Activity should look like this:
Figure 9: The Android app is loaded and the song is played
Figure 10: The song is played and the seek bar has

Publish Android Application Example


After the implementation of an Android application, the last step is to publish your app in
order to be available to the users. You can sell your app or release it for free via an Android
application marketplace (like Google Play ), via your website or even by distribute it directly to
the users.
In this example we will show how to prepare the deliver of your application and finally
publish it to the Google Play marketstore, the digital distribution platform for Android apps
operated by Google.
1. Prepare your application for release
Before releasing your app to the market, you should perform some basic tasks in order to
check it.
 You need to remove the Logs and any debugging attributes of your implementation.
 You should test you application to different devices (phones and tablets) in order to
ensure the desirable result. It is important to make sure that your app runs properly in the
targeted Android versions.
 Be sure that the resources you are using (multimedia and graphics) are updated and are
included to your app. It is recommended to use high-quality graphic assets, as well as the
application icon be compatible with the icon guidelines .
 You should ensure the security of the external servers or services, if any is used.
There are many other tasks as part of preparation process, as Dev Guide in Preparing For
Release show us.
The launch of an application on Google Play requires more preparation. Except for
understanding the polices and agreement part, there are some other tasks:
 Google Play requires to use promotional materials, such as screenshots, videos, localized

descriptions etc, in order to further your application.


 Also you have to declare the content rating for your app to show the maturity level. The
available current ratings are Everyone, Low maturity, Medium maturity, High maturity
 It is important to decide if you want to sell or distribute for free your application. For paid
apps you have to decide the currencies and the price you want to sell it in different
countries. If you want to deliver it for free, remember that this app will always remain
free.
 Google Play gives you the opportunity to release your application worldwide or in targeted
groups. So you have to control the setting of timezone, localization, the listing languages
you want to use etc.
 Although the current maximum size of the APK is set to 50MB, you can use APK Expansion

Files .
Also there is a Launch CheckList that provide us advice for successful product launch
on Google Play .

2. Export Android Application


To release your application and to deliver it to users, you should make a signed .apk file. So,
you have to go to Eclipse and export the application as an APK (Android Package) file before
distribute it to the market.
After you insert the application in the Package Explorer of Eclipse, go to the central menu
and click on File → Export.
In the popup window, select “Export Android Application” from Android folder. Then press
Next button.

Click “Browse…” button and choose the project you want to export. If it has no errors, click
Next. Otherwise you can’t continue and you have to fix the errors. You can go to the next
picture directly, if you right click on the current project → Android Tools → Export Signed
Application Package.
In the next window, choose “Create new keystore”. Then, click “Browse…” to put it in a
location locally to your pc and write down a password. After the password confirmation,
press again Next button.
Now you have to create the key, so you should complete some of the fields, like shown in
the image below. It is necessary to fill in some of these fields, but not all of them. Then click
Next.
Specify the destination of the .apk file that you want to export and give it an appropriate
name, like in the next picture. Finally press Finish.
You can ensure the certification of your APK file from Eclipse Console, as you can see in the
image below.

3. Register in Google Play


The first step is to register in Google Play Developer Console , in order to have a publisher account.
If you already have a Google account you can use it, otherwise you have to create a new
one.
After you read and accept the Developer Distribution Agreement , you have to click “Continue to
payment” button, in order to proceed. That step requires a payment of $25 as a registration
fee, using Google Wallet . If you don’t have a Google Wallet account, you can easily set up one
during the procedure. The next picture shows the main part of Google Play registration page.
Finally, you can enter basic information about your developer identity and your account
details.
After the success of your registration you can sign in to Developer Console of Google Play , where
you can find all the tools and operations for publishing process.
Now, you can upload your release-ready APK in order to publish it. The application remains
at Drafts and you can publish it any time you are ready. It is important to mention that you
can unpublish it wherever you want.

Android Contacts Example


And paste the following code:
[Link]:
01 <RelativeLayout xmlns:android="[Link]

02 xmlns:tools="[Link]

03 android:layout_width="match_parent"

04 android:layout_height="match_parent"

05 tools:context=".MainActivity" >
06

07 <TextView

08 android:id="@+id/textView"

09 android:layout_width="wrap_content"

10 android:layout_height="wrap_content"

11 android:layout_alignParentLeft="true"

12 android:layout_alignParentRight="true"

13 android:layout_alignParentTop="true"

14 android:layout_margin="10dp"

15 android:textSize="20dp"

16 android:gravity="center"

17 android:text="Contacts Information" />

18

19 <TextView

20 android:id="@+id/textView1"

21 android:layout_width="wrap_content"

22 android:layout_height="wrap_content"

23 android:layout_alignLeft="@+id/textView"

24 android:layout_alignRight="@+id/textView"

25 android:layout_below="@+id/textView"

26 android:gravity="center"

27 android:text="TextView" />

28

29 <ProgressBar

30 android:id="@+id/progressBar1"
31 style="?android:attr/progressBarStyleHorizontal"

32 android:layout_width="wrap_content"

33 android:layout_height="wrap_content"

34 android:layout_above="@+id/textView1"

35 android:layout_alignParentLeft="true"

36 android:layout_alignParentRight="true" />

37

38 </RelativeLayout>

3. Add READ_CONTACTS permissions to [Link]


In order to interact with the Contacts on your phone you should add the appropriate
permissions (just read permissions for our example):
1 <uses-permission android:name="[Link].READ_CONTACTS" >

2 </uses-permission>

Use the Package Explorer to navigate to the Java file of [Link] :

[Link]
01 <?xml version="1.0" encoding="utf-8"?>

02 <manifest xmlns:android="[Link]
03 package="[Link]"

04 android:versionCode="1"

05 android:versionName="1.0" >

06

07 <uses-sdk

08 android:minSdkVersion="8"

09 android:targetSdkVersion="17" />

10

11 <uses-permission android:name="[Link].READ_CONTACTS" >

12 </uses-permission>

13

14 <application

15 android:allowBackup="true"

16 android:icon="@drawable/ic_launcher"

17 android:label="@string/app_name"

18 android:theme="@style/AppTheme" >

19 <activity

android:name="[Link]
20
[Link]"

21 android:label="@string/app_name" >

22 <intent-filter>

23 <action android:name="[Link]" />

24

25 <category android:name="[Link]" />

26 </intent-filter>
27 </activity>

28 </application>

29

30 </manifest>

5. Code the Main Activity


Use the Package Explorer to navigate to the Java file of the Activity you’ve created:

[Link]:
01 package [Link];

02

03 import [Link];

04 import [Link];

05 import [Link];

06 import [Link];

07 import [Link];

08 import [Link];

09 import [Link];
10

11 public class MainActivity extends Activity {

12 public TextView outputText;

13

14 @Override

15 public void onCreate(Bundle savedInstanceState) {

16 [Link](savedInstanceState);

17 setContentView([Link]);

18 outputText = (TextView) findViewById([Link].textView1);

19 fetchContacts();

20 }

21

22 public void fetchContacts() {

23

24 String phoneNumber = null;

25 String email = null;

26

27 Uri CONTENT_URI = [Link].CONTENT_URI;

28 String _ID = [Link]._ID;

29 String DISPLAY_NAME = [Link].DISPLAY_NAME;

String HAS_PHONE_NUMBER =
30
[Link].HAS_PHONE_NUMBER;

31

Uri PhoneCONTENT_URI =
32
[Link].CONTENT_URI;
String Phone_CONTACT_ID =
33
[Link].CONTACT_ID;

34 String NUMBER = [Link];

35

Uri EmailCONTENT_URI =
36
[Link].CONTENT_URI;

String EmailCONTACT_ID =
37
[Link].CONTACT_ID;

38 String DATA = [Link];

39

40 StringBuffer output = new StringBuffer();

41

42 ContentResolver contentResolver = getContentResolver();

43

Cursor cursor =
44
[Link](CONTENT_URI, null,null, null, null);

45

46 // Loop for every contact in the phone

47 if ([Link]() > 0) {

48

49 while ([Link]()) {

50

String contact_id = [Link]([Link](


51
_ID ));

String name =
52
[Link]([Link]( DISPLAY_NAME ));

53

54 int hasPhoneNumber =
[Link]([Link]([Link]( HAS_PHONE_NUMBER )
));

55

56 if (hasPhoneNumber > 0) {

57

58 [Link]("\n First Name:" + name);

59

60 // Query and loop for every phone number of the contact

Cursor phoneCursor =
61 [Link](PhoneCONTENT_URI, null, Phone_CONTACT_ID + "
= ?", new String[] { contact_id }, null);

62

63 while ([Link]()) {

phoneNumber =
64
[Link]([Link](NUMBER));

65 [Link]("\n Phone number:" + phoneNumber);

66

67 }

68

69 [Link]();

70

71 // Query and loop for every email of the contact

Cursor emailCursor =
72 [Link](EmailCONTENT_URI, null, EmailCONTACT_ID+ "
= ?", new String[] { contact_id }, null);

73

74 while ([Link]()) {

75
email =
76
[Link]([Link](DATA));

77

78 [Link]("\nEmail:" + email);

79

80 }

81

82 [Link]();

83 }

84

85 [Link]("\n");

86 }

87

88 [Link](output);

89 }

90 }

91

92 }

6. Run the application


This is the main screen of our Application:

You might also like