android applications ေရးသားနည္းစာအုပ္...
Post on 03-Jun-2018
219 views
TRANSCRIPT
-
8/12/2019 Android Applications ( . )(lovermyohtay)
1/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Android appl icat ion beginner Tutor ial reference Andro id Appl icat ion ) ) jdk1.7.0https:// dl.dropbox.com/ u/94729309/jdk1.7.0.rar
install
) Eclipsehttp://eclipse.stu.edu.tw/technology/epp/downloads/release/juno/R/eclipse-mobile-juno-win32.zip
eclipse-mobile-juno-win32.zip unzip eclipse D drive (D drive D drive )
) Android SDK
http://dl.google.com/android/installer_r20.0.1-windows.exeinstaller_r20.0.1-windows.exe runNext Install just for me NextDestination floder application BrowseInstall clickNext
Finish
) Tools
Android 3.0
check
Install 5 packages
(4.2 3.03.0)
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttps://dl.dropbox.com/u/94729309/jdk1.7.0.rarhttp://eclipse.stu.edu.tw/technology/epp/downloads/release/juno/R/eclipse-mobile-juno-win32.ziphttp://dl.google.com/android/installer_r20.0.1-windows.exehttp://dl.google.com/android/installer_r20.0.1-windows.exehttp://eclipse.stu.edu.tw/technology/epp/downloads/release/juno/R/eclipse-mobile-juno-win32.ziphttps://dl.dropbox.com/u/94729309/jdk1.7.0.rarhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
2/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
) eclipseeclipse double click) Workspace application OK click) Android Developer Toos ( ADT) plug-in (ADT plug-in download offline install
)Help> Install New Software>
Add click
Ok
Next
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
3/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Connection android application software
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
4/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Andro id App l icat ion () htarh ta r lw in .b logspo t .com
Android Appl icat ion ()Hello w orld!Application
eclipseopen
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
5/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Project create
File> New> Android Application Project
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
6/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Next click Minimum Required SDK 2.2 application
Android version 2.2 install application sdk version 3.0
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
7/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
8/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Browse (application logo
Next)
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
9/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
10/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Next
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
11/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Install/Upgrade click
( install)
Accept AllI nstallclick
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
12/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
proj ectcreate
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
13/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
(1) (Android version 2.2)USB debuggingcheck
(2) Phone USB Cable
(3) Run> Run
(4) deviceOK
(5) PhoneHello World!
(6) menu Droid1 application touch
Hello world! application uninstall
(Sett ings> Applications> Manage applications)
apk file
bin Droid1.apk install
software ( )
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
14/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
data
selectbutton click()
Android Appl ication ()Run on Emulator Android application android device (/ tablet / note)
( emulator)r un
() project real device run emulator run emulator
r un
Android Virt ual Device Manager
eclipse tool bar Android Virtual Device Manager tool click
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
15/121
-
8/12/2019 Android Applications ( . )(lovermyohtay)
16/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
HHlStartclick
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
17/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
18/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
eclipse ()applicationRun
Hello w orld! emulator
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
19/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Android Appl ication () () create Droid1 coding application
run Hello World!
Eclipse
Package ExplorerDroid1> src>com.example.droid1> DroidActivity.java
package com.example.droid1;
i m p o r t android.os.Bundle;i m p o r t android.app.Activity;i m p o r t android.view.Menu;i m p o r t android.widget.TextView;/ / * * * * *
pub l ic c lassDroidActivity ex tendsActivity {@Override
p u b lic v o id onCreate(Bundle savedInstanceState) {super .onCreate(savedInstanceState);
TextView tv = n ew TextView( th is) ;/ / * * * * *
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
20/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
tv.setText("Hello! I'm Dr. Htar Htar Lwin" );/ / * * * * *
setContentView(tv);/ / * * * * *
}
@Override
public booleanonCreateOptionsMenu(Menu m enu) { getMenuInflater().inflate(R.menu.main, menu);
return true; }
}
/ / * * * * *
phone runEmulator run run
Android Application )Discussion tutorial
Android SDK
install install
D: drive D:\sdk sdk
D drive
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
21/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Start SDK Manager check latest SDK tools platform
download platform check Install * packagesclick( *
= number of packages)
Android 4.2download( )
sdk folder (Android SDK install) SDK Manager
eclipsedownloadunzip
Eclipse I DE eclipse
Eclipseandroid application ADT Plugininstall
Project create eclipse command line tools
eclipse
createapplication run run emulator
run
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
22/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Android Appl ication () SDK 2.2 3.0
)
User I nterface
) Tool bar create a new Android project toolclick Next
click
()Next
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
23/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
24/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
25/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
MyApp2> res> layout> activity_main.xml code copy
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
26/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
< ?xm l version= "1.0"encoding= "utf-8"?>
< LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height= "match_parent"
android:orientation= "horizontal">
< EditText android:id= "@+ id/ edit_message"
android:layout_weight= "1"
android:layout_width = "0dp"
android:layout_height= "wrap_content"
android:hint= "@string/edit_message"/ >
< Button
android:layout_width = "wrap_content"
android:layout_height= "wrap_content"
android:text= "@string/button_send"/ >
< /LinearLayout>
MyApp2> values> strings.xmlcode
< ?xml version= "1.0" encoding= "utf-8"?>
< resources>
< string name= "app_name"> My First App< / string>
< string name= "edit_message"> Enter a message< / string>
< string name= "button_send"> Send< / string>
< string name= "menu_settings"> Settings< /string>
< string name= "title_activity_main"> MainActivity< / string>
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/toolshttp://schemas.android.com/toolshttp://schemas.android.com/apk/res/androidhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
27/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
< / resources>
Run emulator run Textbox Button
ru n bin apk
install(coding))
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
28/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
29/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Android Application )() coding
textboxbutton
textbox button layout
For layout
< ?xm l version= "1.0"encoding= "utf-8"?>
< LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height= "match_parent"
screenwidth and height
android:orientation= "horizontal"
textbox button
Textbox
< EditText android:id= "@+ id/ edit_message"
android:layout_weight= "1"
android:layout_width = "0dp"
android:layout_height= "wrap_content"
textboxwidth and height
android:hint= "@string/edit_message"/ >
button
< Button
android:layout_width = "wrap_content"
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/toolshttp://schemas.android.com/toolshttp://schemas.android.com/apk/res/androidhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
30/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
android:layout_height= "wrap_content"
android:text= "@string/button_send"/ >
< /LinearLayout>
< ?xml version= "1.0" encoding= "utf-8"?>
< resources>
< string name= "app_name"> My First App< / string>
Application name
< string name= "edit_message"> Enter a message< / string>
Application runtextbox string Enter a m essage
< string name= "button_send"> Send< / string>
Application runbuttonstring Send
< string name= "menu_settings"> Settings< /string>
< string name= "title_activity_main"> MainActivity< / string>
< / resources>
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
31/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Android Application )( project
textbox button create textbox button
display MyApp
Open MyApp> res> layout> activity_main.xml
Add android:onClick= "sendMessage" as follow :
()
Open MyApp> src> MainActivity.java
Add these codes.
packagecom.example.myfirstapp;
import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.EditText;
public classMainActivit y extendsActivity {public final staticStringEXTRA_MESSAGE= "com.example.myfirstapp.MESSAGE";
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
32/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
/ * * Called when the user clicks the Send button * /
p u b li c vo id sendMessage(View view ) { Int ent intent = n ew Intent( th is, DisplayMessageActivity.class); EditText editText = (EditText) findViewById( R.id.edit_message) ;
String message = editText.getText( ).toString() ;
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
@Override
p u b lic v o id onCreate(Bundle savedInstanceState) {super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main) ;
}
@Override
p u b l ic b o o le an onCreateOptionsMenu(Menu m enu) { getMenuInflater().inflate(R.menu.activity_main, menu);
r et u rn t r ue ; }
}
Tip: In Eclipse, press Ctr l + Shift + O to import missing classesNow you need to create the DisplayMessageActivity class in order for this to work.
To create DisplayMessageActivity class1. Click New in the toolbar.
2. In the window that appears, open the Andro id folder and select Andro id Act iv i ty . Click Next .3. Select BlankActivi ty and click Next .
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
33/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
4. Fill in the activity details:
o Project: MyFirstApp
o Activity Name: DisplayMessageActivity
o Layout Name: activity_display_message
o Navigation Type: None
o Hierarchial Parent: com.example.myfirstapp.MainActivity
o Title: My Message
Click Finish .
Figure 1 . The new activity wizard in Eclipse.Open the DisplayMessageActivity.j ava file.
The class already includes an implementation of the required onCreate() method.
There's also an implemtation of the onCreateOptionsMenu() m ethod, but you won't need it for this app so
you can remove it.
The class should look like this:
public class DisplayMessageActivity extends Activity {
@Override
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
34/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
}
}
All subclasses of Activitym ust im plement the onCreate()method.
The system calls this when creating a new instance of the activity.
It is where you must define the activity layout and where you should perform initial setup for the activity
components.
Add it t o the manifestYou must declare all activities in your manifest file, AndroidManifest.xml, using an < activity> element.
You used the Eclipse tools to create the activity, it creates a default entry.
I t should look like this:
< application ... >
.. .
< activity
android: name= ".DisplayMessageActivity"
android:label= "@string/ title_activity_display_message" >
< meta-data
android: name= "andr oid.support .PARENT_ACTIVITY"
android:value= "com.example.myfirstapp.MainActivity" / >
< / activity>
< / application>
Receive the IntentEvery Activityis invoked by an Intent, r egardless of how the user navigated there.
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
35/121
-
8/12/2019 Android Applications ( . )(lovermyohtay)
36/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
}
Finally, DisplayMessageActivity class looks like this:
packagecom.example.myfirstapp;
import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;
public classDisplayMessageActivity extendsActivity {
@Override
public voidonCreate(Bundle savedInstanceState) {super .onCreate(savedInstanceState);
/ / Get the message from the in tent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE) ;
/ / Creat e the text view
TextView textView = new TextView( this) ; textView.setTextSize(40);
textView.setText(message);
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
37/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
/ / Set the text view as the act ivit y layou t
setContentView(textView);
}
}
You can now run t he app.
When it opens, type a message in the text field, click Send, and the message appears on the second
activity.
Figure 2 . Both activities in the final app, running on Android 4.0.
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
38/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Android Application 9) htarhtarlwin.blogspot.com
Android Application )
Android device current timedisplayapplication
projectcreate
Project nameskeletonactivity nameNow
Now.java filesource code
package com.example.skeleton;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Date;
public class Now ext ends Activity implements View.OnClickListener {
Button btn;
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
39/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
btn = new Button(this);
btn.setOnClickListener(this);
updateTime();
setContentView(btn);
}
public void onClick(View view) {
updateTime();
}
private void updateTime() {
btn.setText(new Date().toString());
}
}
Discussionpackage com.example.skeleton;packagedeclare
import android.app.Activity;impor t android.os.Bundle;import android.view.View;import android.widget.Button;import java.util.Date;
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
40/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
program class import android-specific class android
package
public class Now extends Activity implements View.OnClickListener {Button btn;Activity Nowpublic classandroid.app.Activity base classextend
program Now button (btn) hold (Button android widget
widget application UI element Widget View
base classextend)
Button click activity trap Now OnClickListener
implement
@Overridepublic void onCreate Bundle icicle) {super.onCreate icicle);btn = new Button t his);btn.setOnClickListener this);updateTime );setContentView btn);}ActivityonCreate() method invoke
The first thing you should do is chain upward to the superclass, so the stock Android activity initialization
can be done.
button instance create button click instance
setOnclickListener() updateTime() method
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
41/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
program run button current time display
setContentView()
public void onClick View view) {updateTime );}Button click button configure onClickListener instanceonClick()
method invoke updateTime method call
buttonclick
private void updateTime ) {btn.setText new Date ).t oString )) ;}Activity open button click button label current time
setText()
program run tap current time
tap
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
42/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
onCreate() method updateTime() run run run current time
t apcurrent time
Android Appl ication ()Widget create activity attach Java code
) XML-based layout file
XML-based layoutwidget relationshipwidget
continer relationship XML-based layout resource
layout f ile res folder
XML element attribute properties widget
container properties Button element attribute value
android:textStyle = bold buttontextbold
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
43/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
layout aapt tool R.java file generate layout
layoutwidget R.layout.main or R.id.button
xml code button project create
NowRedux layout name main xml code
res/layout main.xml file run button
button tap button
< RelativeLayout xmlns:android= h t tp : / / schemas.andro id .com/apk/ res /andro idxmlns:tools= h t tp : / / schemas.andro id .com/ too ls>< Button
android:id= @+ id/ bu t t onandroid:layout_width = wrap_contentandroid:layout_height= wrap_contentandroid:text = / >
< /RelativeLayout>XML layout filebuttoncreate
xmlns:android= h t tp : / / schemas.andro id .com/apk/ res /andro idRoot element Android XML namespace declare element root
children name space declarationinherit
android:id= @+ id/ bu t t on button jave code identifier android:id attribute
element id java code element id
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/toolshttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/toolshttp://schemas.android.com/apk/res/androidhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
44/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
ButtonButton XML element Button Android-supplied widget class
name w idget android.view.View subclass
createfull package declaration com.example.MyWidget
android:layout_width = wrap_contentandroid:layout_height= wrap_content
button width height
android:text= buttontext space
buttonXML code main.xml res/layout layout file
java code access NowRedux.java file onCreate() call back
setContentView(R.layout.main);
statement
button
findViewById();
java code NowRedux.java
package com.example.layouts;
import android.app.Activity;import android.os.Bundle;
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
45/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
import android.view.View;import android.widget.Button;import java.util.Date;public classNowRedux extendsActivityimplementsView.OnClickListener {Button btn;
@Override
public voidonCreate(Bundle icicle) {super.onCreate(icicle);setContentView(R.layout.main) ;
btn= (Button) findViewById(R.id.button) ;
btn.setOnClickListener( this) ;updateTime();
}
public voidonClick(View view) {updateTime();
}
private voidupdateTime() {btn.setText(new Date().toString());}
}
run button tap
program button XML code
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
46/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Android Appl ication ()Employ ing Basic Widgetwidget Widgetfiled, label, button
Assigning LabelsLabelandroidTextViewuseredit
textbox user box
label textbox
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
47/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Label create TextView instance create XML layout file
TextView element create button android:
text propertytext properties
android:typefacemonospace
android:textstylebold, italic or bold and italic
android:textColorRGB hex format # FF0000
project create Label activity name main
main.xml f ilecode run
< TextView xmlns:android= "http://schemas.android.com/apk/res/android"
android:id= "@+ id/ TextView"
android:layout_width = "wrap_content"
android:layout_height= "wrap_content"
android:text= "You were expecting someth ing profound"/ >
output
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/androidhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
48/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Fields of Green. Or Other Colors.Edit Text
Edit Text widgetTextViewsubclassEdit Textpr operties
andro id :au toTex tauto spelling
andro id :cap i ta l ize capitalauto
andro id :d ig i tsdigit
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
49/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
android:singlelinesingle line or multiline
android:numericnumeric-only input
android:passwordpassword input
android:phoneNumberphone number
Field projectcreateactivity nameFieldDemo
main.xml code
< ?xml version= "1.0" encoding= "utf-8"?>
< EditText xmlns:android= "ht tp:/ / schemas.android.com/ apk/res/android"
android:id= "@+ id/field"
android:layout width= "fill parent"
android:layout height= "fill parent"
android:singleLine= "false"
/ >
android:singleLine false
FieldDemo.java filecode
package com.commonsware.android.basic;
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
50/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
import android.app.Activity;import android.os.Bundle;import android.widget.EditText;public class FieldDemo extends Activity {@Override
public void onCreate(Bundle icicle) {super.onCreate(icicle);setContentView (R.layout.main);EditText fld= (EditText) findViewById(R.id.field);fld.setText("Licensed under the Apache License, Version 2.0 " +"(the \"License\"); you may not use this file " +
"except in compliance with the License. You may " +
"obtain a copy of the License at " +
"http://www.apache.org/licenses/LICENSE-2.0");
}
}
Run
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.apache.org/licenses/LICENSE-2.0%22);http://www.apache.org/licenses/LICENSE-2.0%22);http://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
51/121
-
8/12/2019 Android Applications ( . )(lovermyohtay)
52/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
1. t rivia game design
2. application prototype () implement
3. game prototype run
1. Designing an Android Trivia Gamegameuser
Great w hite sharks diving
gameBeen, Done That!
1.1. Determining High-Level Game Featuresfeatures
Application name, version and developersplash sequence Scores
Game rules
Game sett ings
feature main menu
user
application
Been There, Done That! applicationscreen
1. A startup screen
2. A main menu screen
3. A game play screen
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
53/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
4. A sett ings screen
5. A scores screen
6. A help screen
screenBeen There, Done That! application user interface
1.2. Determin ing Act iv i ty Requi rementsScreenactivity classFigure 6.1activity
Base Activity class QuizActivity activity
1. QuizActivity: android.app.activity class extend application preferences
application-wide settings and features classdefine
2. QuizSplashActivity: QuizActivityextend splash screen
3. QuizMenuActivity: QuizActivityextend main menu screen
4. QuizHelpActivity: QuizActivity
extend
help screen
5. QuizScoresActivity: QuizActivityextend scores screen
6. QuizSettingsActivity: QuizActivityextend settings screen
7. QuizAct iv ityextend game screen
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
54/121
-
8/12/2019 Android Applications ( . )(lovermyohtay)
55/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Splash screenmain menu screen
UserPlay Game, Settings, Scores, or Help
Figure 6.3main menu screen
1.3.3. Defining Help Screen Features
Help screen user game play functionality
QuizHelpActivity classhelp screen
Userhelp text scroll
UserQuestionsuggest
Figure 6.4Help screen
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
56/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
1.3.4. Defining Scores Screen Featu res
screen user game scores functionality
QuizScoresActivity class screen
Top score
usergame screenscore
Figure 6.5scores screen
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
57/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
1.3.5. Defining Settings Screen Features
screen user game settings edit save game
settings user name and other important features functionality
QuizSett ingActivity class screen
Usergame settings
User
Figure 6.6basic settings screen
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
58/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
1.3.6. Defining Game Screen Features
screentrivia quizdisplay functionalityQuizGameActivity class
screen
Yes/no questions
Inputhandlescorestate
Userscores screen
Figure 6.7game screen
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
59/121
-
8/12/2019 Android Applications ( . )(lovermyohtay)
60/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Create activity: QuizSplashActivity
setting projectcreate )
2.2. Adding Project ResourcesActivity layout file activity name text string
application icon
2.2.1. Adding String Resources
Eclipse res\values\strings.xml hello string string
create
screen
string
screen
Been There, Done That!Help ScreenMain Menu ScreenSplash ScreenSettings ScreenGame Screen
Scores Screen
2.2.2. Adding Drawable Resources
Application icon 48 x 48 pixel PNG type image file create
photoshop quizicon.png drawable folder
icon.png size create
/drawable-ldpi, /drawable-mdpi, /drawable-hdpi folder size
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
61/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
(low, medium, high resolution)
refer refer android operating system
device icon version ( project
default icon )
2.2.3. Adding Layout ResourcesActivity layout resurce file create main.xml splash.xml
splash.xml file (copy ) game.xml, help.xml, menu.xml,
scores.xml, settings.xml error layout TextView conrol @string/hello
string refer hello layout file
game.xml @string/hello @string/game
splash.xml file @string/splash,
@string/ help, @string/ menu, @string/ scores, @string/settings
2.3. Implementing Application Activitiessrc\com.androidbook.btdt.hour6\QuizSplashActivity.java file copy file name
QuizActivity.javacoding save
packagecom.androidbook.btdt.hour6;
importandroid.app.Activity;
public classQuizActivity extendsActivity {public static finalString GAME_PREFERENCES= "GamePrefs";
}
2.3.1. Creating the Rest of the Application Activities
copy file name QuizMenuActivity, QuizHelpActivity,
QuizScoresActivity, QuizSettingsActivity, QuizGameActivity (copy
com.androidbook.btdt.hour6 package name right click New Class
class name )
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
62/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
file error file main.xml
splash.xml file
QuizHelpActivity.java filesetContentView() method
setContentView(R.layout.help);
packagecom.androidbook.btdt.hour6;
importandroid.os.Bundle;
public classQuizHelpActivity extendsQuizActivity {/** Called when the activity is first created. */@Overridepublic voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.help); }}
QuizScoresActivity , QuizSettin gsActivity, QuizMenuActivit y, QuizGameActivity file
packagecom.androidbook.btdt.hour6;
importandroid.os.Bundle;
public classQuizScoresActivity extendsQuizActivity {/** Called when the activity is first created. */
@Overridepublic voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.scores); }
}
packagecom.androidbook.btdt.hour6;
importandroid.os.Bundle;
public classQuizSettingsActivity extendsQuizActivity {/** Called when the activity is first created. */@Overridepublic voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.settings); }
}
packagecom.androidbook.btdt.hour6;
importandroid.os.Bundle;
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
63/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
public classQuizMenuActivity extendsQuizActivity {/** Called when the activity is first created. */@Overridepublic voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.menu); }}
packagecom.androidbook.btdt.hour6;
importandroid.os.Bundle;
public classQuizGameActivity extendsQuizActivity {/** Called when the activity is first created. */@Overridepublic voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.game);
}}
2.3.2. Updating the Android Manifest File
manifest file () @drawable/quizicon (
) () activity
manifest file
register
(
) debuggable application attr ibute
true
QuizSplashActivity
default activity activity screen Splash screen
display
2.4. Creating Application Preferences application bastic state information and user data functionality
andro ids shared preferencesandroid.content.SharedPreferences
Shared preferences application activity
base classQuizActivity.java file public static finalString GAME_PREFERENCES= "GamePrefs";
subclasses (create shared
preference preference categories preference name
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
64/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
game preferences user preferences shared preference
)
Applicationshared preference
1. Activity class SharedPreferences object instance retrieve
getSharedPreferences() method
2. Preference modifySharedPreference.Editor objectcreate
3. Edit orpreference
4. Edit orcommit() m ethod commit
2.4.1. Saving Specific Shared Preferences
Preference key/value preference value data type
Boolean
Float
Integer
Long
String
preference save SharedPreferences object
instance editor
commit users name age preference save activity classcode
SharedPreferences settings =getSharedPreferences(GAME_PREFERENCES,MODE_PRIVATE);SharedPreferences.Editor prefEditor = settings.edit();prefEditor.putString(UserName, JaneDoe);prefEditor.putInt(UserAge, 22);prefEditor.commit();
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
65/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
preference clear shared preferences editor clear() method
preference remove ( remove preference
) remove() method
2.4.2. Retrieving Shared Preferences
Editor create retrieve activity class
shared preference values retr ieve code
SharedPreferences settings =getSharedPreferences(GAME_PREFERENCES,MODE_PRIVATE);if (settings.contains(UserName) == true) {
// We have a user nameString user = Settings.getString(UserName, Default);}
(shared preferences )
3. Running the Game Proto typedebug configurationcreate
3.1. Creating a Debug ConfigurationNew eclipse project debug configuration AVD (emulator) configure
application Minimum sdk version (
application minimum sdk version AndroidManifest.xml file
)
application android API level 7 configure emulator real android
device application API level 10
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
66/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
AVD Eclipse toolbar Android Virtual Device Manager
AVD application run Eclipse
toolbar Android Virtual Device Manager click AVD select start
buttonclick
3 .2 . Launch ing the Pro to type in th e Emu la to remulator AVD manager window (emulator )
eclipse Run button click (bug) click
emulator run Lunch a new Andorid Virtual Device option select OK
configure AVD
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
67/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
AVD
OKclick
emulator run real device run Figure 6.8
( emulatorinstallrun)
run
Figure 6.8
Application window icon Been There, Done That!
application application
Been There, Done That! application click
default QuizSplashActivity activity TextView
title Been There, Done That! Splash Screen TextView
application framework
coding game application
(applicationuninstall)
project link
https://dl.dropbox.com/u/94729309/Hour6.rar
google docHour6
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttps://dl.dropbox.com/u/94729309/Hour6.rarhttps://dl.dropbox.com/u/94729309/Hour6.rarhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
68/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Android Application )) I mplementing an Animated Splash Screen
1. Splash screen design
2. Splash screen layout
3. An imat ion
1. Designing the Splash Screenmain m enu screenscreen
screentitle and version information
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
69/121
-
8/12/2019 Android Applications ( . )(lovermyohtay)
70/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
fill_parent or wrap_content
splash screen ImageView control display TableLayout control
splash screen parent layout LinearLayout TextView,
TableLayout ( TableRow control TableRow ImageView ) ,
TextView
2.1. Adding N ew Pro ject Resources layout string, color and dimension resources create
splash1.png, splash2.png, splash3.png, splash4.png resolution create
res/drawable-ldpi, res/ drawable-mdpi, res/ drawable-hdpi
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
71/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
/ res/ vlu es/ str ings.xm l f ile String top title (Been There)
bottom title (Done That!) version information (multiple line)
13 splash string
strings.xml file
Been There, Done That!Help ScreenMain Menu ScreenSettings ScreenGame ScreenScores ScreenBEEN THEREDONE THAT!Version 2.0.0\nCopyright 2011 Mamlambo\nAll Rights Reserved.
/ res/ vau les/ color.xml file color resources title text
( version text (grayish white) version text background
( deep blue)color.xml file
-
8/12/2019 Android Applications ( . )(lovermyohtay)
72/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
name="logo_color">#FFFF0F#f0f0f0#1a1a48
/ res/ vlues/dim ens.xml fi le dimension value title font size
(48dp), version text font size (15dp), version text line spacing (3dp)
dp unit device resolution dimens.xml file
code save
2.2. Updating the Splash Screen Layoutsplash.xml file control
1. LinearLayout Orientation vertical background color attribute
2. TextView (TextViewTopTitle)
3. TableLayout (TableLayout01)
4. TableLayoutTableRowTableRowI mageView
-
8/12/2019 Android Applications ( . )(lovermyohtay)
73/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
android:id="@+id/TableRow01"android:layout_height="wrap_content" android:layout_width="wrap_content"android:layout_gravity="center_vertical|center_horizontal">
5. TableLayout TableRow TableRow ImageView
6. LinearLayout TextView (TextViewBottomTitle)
7. LinearLayoutTextView (TextViewBottomVersion)
-
8/12/2019 Android Applications ( . )(lovermyohtay)
74/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
android:layout_width="match_parent"android:layout_gravity="center_vertical|center_horizontal"android:gravity="center">
splash.xml filecode
-
8/12/2019 Android Applications ( . )(lovermyohtay)
75/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical|center_horizontal"android:src="@drawable/splash4">
saverun
3 . Work ing w i th An im at ion splash screen animation Android platform graphics animation
support
Animated GIF images
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
76/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Frame-by-frame animation
Tweened animation
OpenGL ES
applicationTweened animationalpha (transparency), rotation, scaling,
translating ( moving) animations
3.1. Adding Animations ResourcesAnimation create fade_in.xml, fade_in2.xml, custom_anim.xml file
/ res/ anim fade_in.xml file 2.5 seconds alpha value 0 (transparent) 1
(opaque) fade_in.xml f ilecode
animationtop TextView control
fade_in2.xml animation file stratOffset attribute 2500
milliseconds animation 5 seconds2 .5
seconds 2.5 seconds fade () 5 seconds splash screen
displayf ade_in2.xml f ile
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/androidhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
77/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
TableLayout graphics animation sequence
rotation, scaling, alpha transition custom_anim.xml file
0 360 degree rotate 2 seconds alpha value 0 1 2
seconds 10% 100% scale 2 seconds animation 2 seconds
parallel filesave animationView
3.2. Animating Specific ViewsAnimation sequence activity class file (QuizSplashAcivity class) coding
manageapplicationpauseanimation application
resumeanimationresume
fade_in animation title TextView (TextViewTopTitle) apply
QuizSplashAcitivty class TextView control instance create animation resource
(TextViewTopTitle) Animation object (logo1) load stratAnimation() method
TextView logo1 = (TextView) findViewById(R.id.TextViewTopTitle);
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/androidhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
78/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in); logo1.startAnimation(fade1);
onPause() callback animation stop clearAnimation() method
onPause() method code
@Overrideprotected voidonPause() {
super.onPause();// Stop the animation
TextView logo1 = (TextView) findViewById(R.id.TextViewTopTitle); logo1.clearAnimation(); TextView logo2 = (TextView) findViewById(R.id.TextViewBottomTitle); logo2.clearAnimation(); //stop other animations
}
3.3. Animating All View in a LayoutAnimation View apply parent control child View control
apply TableLayout LayoutAnimationController
object
Animationload
LayoutAnimationController instancecreate configure
layout setLayoutAnimation() method pass
- code
custom_animation load
LayoutAnimationControllercreate TableLayoutTableRowapply
Animation spinin = AnimationUtils.loadAnimation(this, R.anim.custom_anim); LayoutAnimationController controller = newLayoutAnimationController(spinin); TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
for(inti = 0; i < table.getChildCount(); i++) { TableRow row = (TableRow) table.getChildAt(i); row.setLayoutAnimation(controller);
}
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
79/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
startAniation() method call LayoutAnimationController
LayoutAnimationController animation stop clearAnimation() method
onPause() methodcode
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);for(inti = 0; i < table.getChildCount(); i++) {
TableRow row = (TableRow) table.getChildAt(i); row.clearAnimation();
}
3.4. Handling Animation Life Cycle Events splash screen animation animation
QuizSplshActivity QuizMenuActivity QuizMenuActivity Intent
create Intent startActivity() method pass QuizSplashActivity
finish() method ( back button splash screen
QuizSplashActivity activity stack finish ) fade_in2
animation 5 seconds AnimationListener object create callback
start, end, repeat onAnimationEnd() method implement
AnimationListener create onAnimationEnd() callback implement
code
Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fade_in2); fade2.setAnimationListener(newAnimationListener() {
public voidonAnimationEnd(Animation animation) { startActivity(newIntent(QuizSplashActivity.this, QuizMenuActivity.class)); QuizSplashActivity.this.finish();
}
});
emulator or real device (handset) run splash screen animation
main menu main menu
Example project codehour7
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
80/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Android Application )) I mplementing the Main Menu Screen
4. Main menu screendesign
5. Main menu screen layout implement
6. ListViews controls
7.
menu t ypes
1. Designing the Menu Screen screen application screen user option
play the game, review the help, configure the settings, view the high scores
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
81/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
option button
buttonclick listen screendisplay
play the gamebuttonclickgame playscreen option
button button ListView control
List Viewoption option screenscroll
Menu background image ListView control custom selection graphic
User list option click activity (screen) display
1.1. Determin ing Main Menu Screen Layout Requi rementsQuizMenuActivity/ res/layout/m enu.xml layout file header,
ListView control, I mageView control
1.2. D esign ing th e Screen H eader w i th Rela t iveLayout
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
82/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Header screen title TextView control display TextView control
RelativeLayout
RelativeLayout child view parent layout or other child view control
header RelativeLayout control
child layout
ImageView control (parent control top left )
TextView control ( parent control top center )
ImageView control (parent control top right )
1.3. Designing the ListView Control
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
83/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
ListView control View object custom divider selector
1.4. Finishing Touches for the Main Menu Layout DesignListView controlImageView
main m enu screen layout design
RelativeLayout
o RelativeLayout
ImageView
TextView (Title)
ImageView
o ListView
I tem( 0): TextView(Play Game)
I tem( 1): TextView(View Scores )
Item(2): TextView(Settings)
I tem(3): TextView(Help)
o ImageView
menu.xml file
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
84/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/androidhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
85/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
2 . I m p lem ent ing the Ma in Menu Screen Layou tProjectresurce
2.1. Adding N ew Pro ject ResourcesLayout drawable, string, color, dimension resource
/ res/ drawablebkgrnd.png, divider.png, half.png, slector.png bkgrnd.pngbackground image
(RelativeLayout divider.png and selector.png custom divider and selector
( ListView controlhalf.pngscreen (ImageView
/ res/ values/ str ing.xmlstring
Been There, Done That!Help ScreenSettings ScreenGame ScreenScores ScreenBEEN THERE
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
86/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
DONE THAT!
Version 2.0.0\nCopyright 2011 Mamlambo\nAll Rights
Reserved.MAIN MENUSettingsPlay GameView ScoresHelp
/ res/ values/ colors.xmlColor resource
#FFFF0F#f0f0f0#1a1a48#f0f0f0#F00#FFFF0F#F00
/ res/ values/dim ens.xmldimension
48dp15dp
3dp
40dp
34dp
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
87/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
2.2. Updating the Main Menu Screen Layout FilesMaster layout file menu.xml screen layout ListView
controllayout f ile create
2.2.1. Adding the ListView Tem plate LayoutListView control item item layout template
/res/layout/menu_item.xml file
3. Working with the ListView Control QuizMenuActivity.java file ListView control
ListView control item click listen
user click item screendisplay Play Game item click Game play
screendisplay
3.1. Filling a ListView ControlListView item QuizMenuActivity class onCreate() method code
findViewById() methodListView
ListView menuList = (ListView) findViewById(R.id.ListView_Menu);
resource string load
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/androidhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
88/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
String[] items = { getResources().getString(R.string.menu_item_play), getResources().getString(R.string.menu_item_scores), getResources().getString(R.string.menu_item_settings), getResources().getString(R.string.menu_item_help) };
ListView control data data layout template
(menu_item.xml) map map adapter data type
application ArrayAdapter
ArrayAdapter adapt = newArrayAdapter(this, R.layout.menu_item, items); menuList.setAdapter(adapt);
setAdapter() methodcallListView control data adapter
menuList.setAdapter(adapt);
QuizMenuAcivity.java file
package com.androidbook.btdt.hour8;
import android.content.Int ent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class QuizMenuActivity extends QuizActivit y {
/ * * Called when the activity is first created. * /
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ListView menuList = (ListView) findViewById(R.id.ListView_Menu);
String[] items = { getResources() .getString(R.string.menu_item_play),
getResources().getString(R.string.menu_item_scores),
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
89/121
-
8/12/2019 Android Applications ( . )(lovermyohtay)
90/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
ListView itemclick listenrespond listen
setOnI temClickListener() method respond onItemClick() method implement
item click application activity (screen) launch
menuList.setOnItemClickListener(newAdapterView.OnItemClickListener() {
public voidonItemClick(AdapterView parent, View itemClicked, intposition, long
id) {
// Note: if the list was built "by hand" the id could be used.// As-is, though, each item has the same id
TextView textView = (TextView) itemClicked; String strText = textView.getText().toString();
if
(strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_play))) {// Launch the Game Activity
startActivity(newIntent(QuizMenuActivity.this, QuizGameActivity.class));
} else if(strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_help))) {
// Launch the Help Activity startActivity(newIntent(QuizMenuActivity.this, QuizHelpActivity.class)); } else if
(strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_settings))) {// Launch the Settings Activity
startActivity(newIntent(QuizMenuActivity.this,QuizSettingsActivity.class)); } else if
(strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_scores))) {// Launch the Scores Activity startActivity(newIntent(QuizMenuActivity.this, QuizScoresActivity.class)); }
} });
OnItemClick() method itemclickinformation pass
QuizMenuActivity.java file
menuList.setOnItemClickListener(newAdapterView.OnItemClickListener() {
public voidonItemClick(AdapterView parent, View itemClicked, intposition, longid) {
// Note: if the list was built "by hand" the id could be used.// As-is, though, each item has the same id
TextView textView = (TextView) itemClicked; String strText = textView.getText().toString();
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
91/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
if
(strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_play))) {// Launch the Game Activity
startActivity(newIntent(QuizMenuActivity.this, QuizGameActivity.class)); } else if
(strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_help))) {// Launch the Help Activity
startActivity(newIntent(QuizMenuActivity.this, QuizHelpActivity.class)); } else if
(strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_settings))) {// Launch the Settings Activity
startActivity(newIntent(QuizMenuActivity.this,QuizSettingsActivity.class)); } else if
(strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_scores))) {// Launch the Scores Activity
startActivity(newIntent(QuizMenuActivity.this, QuizScoresActivity.class)); }
} });
runmenu itemclickscreen
3.3. Customizing ListView Control CharacteristicsListViewcustom divider and selection picture
3.3.1. Adding a Custom DividerDivider ListView item divider attribute color or drawable graphic
resource color item horizontal line
drawable graphic resource item graphic
item item divider
ListView control divider menu.xml file ListView control divider attribute
@drawable/ divider
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
92/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
3.3.2. Adding a Custom SelectorList item item select ListView selector
listSelector attributedefault
Selector menu.xml file ListView control listSelector attribute
@drawable/selector
saver undivider selector
4 . Work ing w i th Othe r Menu TypesAndroidmenu type
Context menus
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
93/121
-
8/12/2019 Android Applications ( . )(lovermyohtay)
94/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
4.1.2. Adding an Opt ions Menu to an Act iv i tyOption menu QuizGameActivity class QuizGameActivity class
onCreateOptionsMenu() method implement@Override
public booleanonCreateOptionsMenu(Menu menu) {super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.gameoptions, menu); menu.findItem(R.id.help_menu_item).setIntent(
newIntent(this, QuizHelpActivity.class)); menu.findItem(R.id.settings_menu_item).setIntent(
newIntent(this, QuizSettingsActivity.class));return true;
}
4.1.3. Han dl ing Opt ions Menu Select ionsUser option menu itemonOptionItemSelected() method
user selectmenu itemintent activity ( screen) lunch
(display)
@Overridepublic booleanonOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item); startActivity(item.getIntent());
return true;
}
QuizGameActivity.j ava file complete code
package com.androidbook.btdt.hour8;
import android.content.Int ent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class QuizGameActivity extends QuizActivity {
/ * * Called when the activity is first created. * /
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
95/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuIn flater().inflate(R.menu.gameoptions, menu);
menu.findItem(R.id.help_menu_item).setIntent(
new In tent( this, QuizHelpActivity.class));
menu.findItem(R.id.settings_menu_item).setIntent(
new In tent( this, QuizSettingsActivity.class)) ;
return true;
}
@Override
public boolean onOptionsI temSelected(MenuI tem item) {
super.onOptionsItemSelected(item);
startActivity(item.getIntent());
return true;
}
}
saverun
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
96/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Example project codeHour8
Andorid Application ( )
HelpScores Screens
1. Designing and implementing the help screen
2. Working with f iles
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
97/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
3. Designing and implementing the scores screen
4. Designing screens with tabs
5. Working with XML
1. Designing and Implementing the Help Screen
1.1. Designing the Help Screen
screen text displayscroll
Lay out design
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
98/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
1.2. Implement ing the Help Screen Layout
Projectnew resources help.xml file
1.2.1. Adding New Project Resources
Help screen layout string, color, dimension resources text file
help screen main TextView cont rol display text quizhelp.txt file
Have you been there?
Have you done that?
BEEN THERE, DONE THAT! is a trivia quiz dedicated to those who love travel and adventure. By
answering a series of Yes or No questions, you gain points toward being the most well-traveled
and field-tested person on the planet. Share your experiences and compete with your friends.
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
99/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
GAME SETTINGS
SUPPORT INFORMATION
"Been There, Done That!" was developed by Mamlambo.
Phone: 1-800-555-1212
Email: [email protected]: http://www.mamlambo.com
Address: 1060 West Addison Street, Chicago, IL 60613 (Pardon the Blues Brothers reference)COPYRIGHT (c) 2011, Mamlambo.All rights reserved.
quizhelp.txt fileres/raw/
1.2.2. Updating the Help Screen Layout
res/layout help.xml fileHelp screen layout filehelp.xml file
xml code
LinearLayout background attribute @dr awab l e/ bkg r nd
RelativeLayout Relative layout ImageView control
TextView TextView control text attribute @string/help
RelativeLayout control TextView control id
TextView_HelpText linksClickable attribute true autoLink attribute
all phone numbers, web addtesses, email addresses and postal addresses
auto link textStyle attribute bold or italic
-
8/12/2019 Android Applications ( . )(lovermyohtay)
100/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/icon"android:layout_alignParentLeft="true"android:layout_alignParentTop="true">
2. Working with Files
quizhelp.txt fileQuizHelpActivity.java filequizhelp.txt
fileTextView_HelpText TextView controldisplay
2.1. Accessing Raw File Resources
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/androidhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
101/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
quizhelp.txt filedataopenRawResource() method
InputStream iFile = getResources().openRawResource(R.raw.quizhelp);
InputStream object lieny-by-line or byte-by-byte string
create
TextView helpText = (TextView) findViewById(R.id.TextView_HelpText); String strFile = inputStreamToString(iFile); helpText.setText(strFile);
QuizHelpActivity.java file code
package com.androidbook.btdt.hour9;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class QuizHelpActivity extends QuizActivity {
/ * * Called when the activity is first created. * /
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help);
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
102/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
/ / Read raw file into string and populate TextView
Inpu tStream iFile = getResources() .openRawResource(R.raw .quizhelp);
try {
TextView helpText = (TextView) findViewById( R.id.TextView_HelpText) ;
String strFile = inputStreamToString(iFile);
helpText.setText(strFile);
} catch (Exception e) {
Log.e(DEBUG_TAG, "I npu tStreamToString failure", e);
}
}
/ * *
* Converts an input stream to a string
*
* @param is
* The { @code InputStream} object to read from
* @return A { @code String} object representing the string for of the input
* @throws IOException
* Thrown on read failure from the input
* /
public String inputStreamToString( InputStream is) throw s I OException {
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
103/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
StringBuffer sBuffer = new StringBuffer() ;
DataIn putStream dataIO = new DataInputStream(is);
String strLine = null;
while ((strLine = dataIO.readLine()) != null) {
sBuffer.append(strLine + "\ n");
}
dataIO.close();
is.close();
return sBuffer.toString() ;
}
}
save run splash screen main menu help screen
option help screen quizhelp.txt file
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
104/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
Scores screen
3. Designing and I mplementing the Scores Screen
3.1. Designing the Scores Screen
screen user scores score all-time-high scores
and users friends scores screen 2 users name, score and overall
ranking
Score information TextView control or ListView control
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
105/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
3.1.1. Determining Scores Screen Layout Requirements
/ res/ layout/ scores.xml layout file screen t i t le bar Relative
layout TabHost control tab tab all scores
friends scores
3.1.2. Adding the TabHost Control
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
106/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
3.2. Im plementing the Scores Screen Layout
design implement resources
scores.xml file
3.2.1. Adding New Project Resources
scores remote server server
xml file
/ res/xm l/ allscores.xml and friendscores.xml file allscores.xml
scores
-
8/12/2019 Android Applications ( . )(lovermyohtay)
107/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
username="SAC"score="12344"rank="2" />
-
8/12/2019 Android Applications ( . )(lovermyohtay)
108/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
rank="16" />
friendscores.xml file
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
109/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
< scores> element child < score> element element attribute
username, score and rank
3.2.2. Updating the Scores Screen LayoutScores screen user interface scores.xml layout file
TabHost controls design mode XML layout mode tab emulator devicerun )
Layout file update control LinearLayout control android:background attribute @drawable/bkgrnd
LinearLayout control control RelativeLayout control ImageView TextView TextView
control text attr ibute @string/scores RelativeLayout control TabHost control TabHost1
layout_width and layout_height attribute TabHost control LinearLayout control orientation
attribute vertical layout_width and layout_height LinearLayout control TabWidget control control id attribute
@android:id/tabs
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/groups/htarhtarlwinhttp://www.facebook.com/htarhtarlwintun -
8/12/2019 Android Applications ( . )(lovermyohtay)
110/121
htarhtarlwin.blogspot.com
www.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin
FrameLayout control id @android:id/tabcontent layout_width and layout_height
FrameLayout control TableLayout control Tab TableLayoutcontrol Tab TableLayout control TableLayoutcontrol scores display TableLayout control TableLayout_AllScores TableLayout_FriendScores layout_width and layout_height scoresinformation table column resize stretchColumnsattribute *
res/layout/xml/scores.xml file content
http://www.facebook.com/htarhtarlwintunhttp://www.facebook.com/groups/htarhtarlwinhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/androidhttp://www.facebook.com/groups/htarhtar