android very simple book برمجة أندرويد

230

Upload: -

Post on 28-Jul-2015

576 views

Category:

Documents


6 download

TRANSCRIPT

1

(2010 / (2011 /

) )

2

1

(Java SE) (Android SDK) (Eclipse) (ADT) (Eclipse) (Android)

( ( ( )

) )

(Mash up)

3

1

(2009)

(2010)

(4)

.

.

.4

1

((Android)

)

(iPhone/iPad) (Blackberry) (Windows Mobile) www.badwi.com : [email protected] : 00966503415343 : http://twitter.com/badwi : http://www.facebook.com/badwi :

(

)

..Data Mining .( [email protected] : http://twitter.com/waelalwani : )

5

1

(2005) .(2007) (Open Handset Alliance) (OHA) (48) (2008) (2009) .( ) . (HTC) (Android) (T-Mobile) (HTC)

Linux 2.6 ) (GNU/Linux) (kernel glibc ) (native windowing system) (GNU/Linux) (support memory ) (security) process ) (management network ) (management (driver model) (stack . (abstraction layer)

(Managed Code) (Java)6

(

)

1

Java5 ) Java Dalvik ) Java Virtual )

(Java) (JDV) (JVM) (SE (Virtual . (Machine

. . . (Web Kit OSS) . (Google chrome) . (OpenGL ES) (SQLite)

(GPS) . (accelerometer) .. (Linux Kernel) : Display Driver Bluetooth Driver Camera Driver7

1

Flash Memory Driver Binder (IPC) Driver Keypad Driver USB Driver Wi-Fi Driver Audio Driver Power Management

: Dalvik Virtual Machine Core Libraries : Surface Manager Media Framework SQLite OpenGL ES Free Type Web Kit SGL SSL Libbc

8

1

: Activity Manager Window Manager Content Provider View System Notification Manager Package Manager Telephony Manager Resource Location Manager Sensor Manager

: Home Contacts Phone Browser

.

9

1

-

:

10

1

: Activity Intent Service Content Provider

: (Activity) (Class) (Intent) . (Activity) (Activity) 11

(Start)

On Create On Start Om Restore Instance State On Resume

1

: : On Save Instance State On Resume On Stop : On Destroy On Start On Restart On Resume On Push On Save Instance State

(Running)

(Paused)

(Destroyed)

(Intent)

/

.12

1

(Service)

(Media Player) .(Service) (Content Provider)

.(Contacts) (Activity) (Window)

(Linear Layout)

vertical or ) .(horizontal13

1

(Frame Layout ) . (Relative Layout)

.(B) (Table Layout)

(A)

.(HTML)

14

1

SDK (Software Development Kit) \ . NDA (Native Development Kit) ADT (Android Development Tools) JDK (Java Development Kit) JRE (Java Runtime Environment) JVM (Java Virtual Machine) AVD (Android Virtual Device) AVDM (Android Virtual Device Manager) APK (Android Package)15

1

DDMS (Dalvik Debug Monitor Server) GPS (Global Position System) CMD (Command Line) ADB (Android Debug Bridge)

16

1

.

17

1

(Java SE) Java ) (Java SE) (JDK 6) (Standard Edition ((Java Development Kit (JDK) (Java Runtime Environment) (JRE) (JDK) (JRE) : http://java.sun.com/javase/downloads/index.jsp

:

(Windows)

18

1

(Skip)

Sun Download ) (Manager19

1

(Save)

20

1

(70)

(Accept)21

1

(Next)

22

1

(Next)

23

1

(Finish)

24

1

(Android SDK) Android ) (SDK

http://developer.android.com/sdk/index.html

(Windows)

25

1

(Download)

(:(Save)

26

1

(Save)

(22)

(WinZip) (Windows)

27

1

(Copy) (Paste)

(android-sdk-windows)

28

1

(SDK Setup)

(https)29

1

(Close)

(Cancel) (Settings)

30

1

(Force Https) (Https)

(Available Packages)

(1.6)

31

1

(2.1)

(1.6) (%50) .(1.6)

. (1.5)

.

(Install Selected)

32

1

(Accept All) (Install)

..

33

1

(Eclipse) (Eclipse) (IBM)

. :http://www.Eclipsee.org/downloads

(Eclipse Classic 3.5.2)

34

1

(Torrent)

.

(Save)

35

1

(Save)

Eclipsee-SDK-3.5.2-) (win32

36

1

(163) (Win Zip) : (Windows)

(Copy) : (Paste)

(Eclipse)

37

1

38

1

(ADT) (ADT) (Eclipse) (Eclipse)

(Use this as default and do not ask again)

(OK)

39

1

(welcome) (X)

40

1

(Help)

(ADT) (Install New Software)

41

1

(Add)

(Name)

(Android) (Location)

/https://dl-ssl.google.com/android/Eclipsee (OK)

42

1

(S) (http) (https)

43

1

(Next)

44

1

(I accept) (Finish)

(Eclipse)

45

1

.

. (Preferences) : window

46

1

:

47

1

(Android) :

(Browse)

:

48

1

(OK) :

49

1

(Apply) :

.

.50

1

(Hell World) ( . ( ) )

. (Eclipse)

51

1

New )

(New)

(File) (Project

52

1

(Next)

53

1

(1.6) :

Project Nam: AndroidVerySimple Build Target: 1.6 Application Name:VerySimple Package name:com.book.VerySimple Create Activity:Activity_VerySimple54

1

(Finish)

(Layout)55

(Res) (main.xml)

1

Hello ) (World

56

1

(Eclipse)

(Properties) (Advanced)

57

1

(Environment Variables)

58

1

(User Environment) (New)

(Path)

(Name) (Variable Value)

C:\Documents and Settings\UserName\Desktop\android-sdk-windows\tools

(UserName) (tools)

59

1

(OK)

60

1

(OK) (OK)

(CMD)

(Start >> Run)

61

1

(OK)

android list target (Enter)

62

1

(1)

android create project -n AndroidVerySimple -p c:\1\ -k com.book.VerySimple -a Activity_VerySimple -t 1

) ( : (Project Name) (-n) (AndroidVerySimple)

63

1

Package ) (-k) (Activity Name) (-a)

(com.book.VerySimple) (Name (Activity_VerySimple)

(-p)

(c:\1\) (Path) (1)

(-t)

(Target)

(Enter)

(c:\1)64

1

(Eclipse)C:\Documents and Settings\UserName\workspace

65

1

(Android) (Window) Android SDK and AVD ) (Manager

66

1

android-) (SDK Setup)

(sdk-windows

.

(Virtual Devices) (New) .

67

1

(1.6) (SD) . (VM_Android1.6) (1.6) (500)

68

1

(Hardware) (New)

(SD Card Support) (New) GPS Support Accelerometer (OK)

69

1

(Create AVD)

(OK)

70

1

(Start) (Launch)

71

1

(Run As) (Android Application)

72

1

(HelloWorld)

73

1

(Clean)

(Project)

74

1

(OK)

(X) .

75

1

Environment ) (Variables

(Start >> Run) (CMD)

(OK)

76

1

Android h (enter)

(h)

(android -h) (android)

77

1

android list target (Enter)

78

1

(1) (Enter) android create avd -n VM_Android1.6 -t 1

create ) (-t) (1) (Enter) [yes]

(VM_Android1.6) (avd -n

(No)

(yes)

79

1

: (VM_Android1.6)

(VM_Android1.6_1)

emulator -avd VM_Android1.6_180

1

:

(SDK Setup) .

81

1

) DroidSans-Bold.ttf, : (DroidSansFallback.ttf, DroidSans.ttf adb remount adb shell rm /system/fonts/* adb push c:\fontss\DroidSans-Bold.ttf /system/fonts/ adb push c:\fontss\DroidSansFallback.ttf /system/fonts/ adb push c:\fontss\DroidSans.ttf /system/fonts/

: adb shell rm /system/app/AlarmClock.apk

adb install c:\Vending.apk

82

1

(DDMS) (Eclipse) (DDMS) (+)

(DDMS)

83

1

(DDMS) (Java)

(DDMS)

84

1

(File Explorer) (Heap) (Threads) (Allocation Tracker) (File Explorer) (sd card) . : ( ) . (Devices) USB )85

1

(SDK) . Emulator )

(Driver

(Control

(SMS) (GPS) (latitude) (Longitude) . (GPS)

86

1

(Browser)

87

1

(Google.com in English)

:

88

1

(slideme.org)

89

1

(SAM v2.42)

90

1

91

1

(Done)

Slide ) (Me (Menu)

92

1

(Settings)

93

1

(Applications)

(Unknown sources)

(Development)

94

1

(DDMS)

(Slide Me)

95

1

(slideme.org)

(Utilities) (OI Shopping List)

OI Shopping )

(Utilities) (List

96

1

(Terms & Download)

97

1

(Install) (Notifications)

98

1

(Install)

(Open) (apk) (Download) (SD Card) Slide Me (Apps Installer) (apk) (Slide Me)

99

1

. (DDMS) (Devices) (Screen Capture) ( : )

android-sdk-windows\platforms\android-1.6\skins\HVGA

100

1

(

)

.

(

) . (Eclipse) (Task List)

:

101

1

102

1

(X) (Project >> Clean) .

(src) (com.android.TaskList) (Activity_TaskList)

(

)

103

1

( (res)

) (layout) (main.xml)

104

1

(main.xml)

(Portrait)

(Landscape)

(Config)

105

1

(Layout) (Views)

. (main.xml) (main.xml) (layout) :

106

1

1 2 7 12 107

1

(Eclipse) (Show Line Numbers)

108

1

1

(UTF-8)

(XML)

2 109

1

(6)

(2)

(LinearLayout) (4)

(LinearLayout) (orientation)

(layout_width)

(horizontal) (layout_height) (fill_parent)

7 110

1

(11)

(7)

(layout_width) (fill_parent) (wrap_content)

(layout_height)

(text) (@string/hello)

12

(LinearLayout)

(12)

(TextView) (res) ()111

(Value)

1

(hell) (!Hello World, Activity_TaskList) (Task List)

112

1

(app_name) (My 1st Application)

(Task List)

(add)

113

1

(OK)

(color)

Name : TextViewBackColor Value: #FF0000

114

1

(RGB) (Red) (B)

(#FF0000) (HTML) (R) (FF) (G) (00) (00)

(Green) (Blue)

(string.xml)

(main.xml) (layout)

(background) (TextViewBackColor)

115

1

(My 1st Application) (Task List)

(String) (String)116

:

1

(TextView) (MainTextView)

(res >> layout) (id) (Properties) (MainTextView)

117

1

(main.xml)

android:id="@+id/MainTextView"118

1

(MainTextView)

(Relative)

(@+id/)

@id/MainTextView (+)

(

)

(Title) (Details)119

1

(Menu)

(Menu) (Activity_TaskList) (Menu) (View)import android.view.Menu; import android.view.View;

import android.widget.TextView;

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.Menu; import android.widget.TextView; public class Activity_TaskList extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { 120

1

super.onCreate(savedInstanceState); setContentView(R.layout.main); } }

(

)

(activities,alertdialog,toast)

(Welcome)

import android.widget.Toast; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

Toast.makeText(Activity_TaskList.this,"Welcome",0).show() ; 121

1

TextView[] tvs; tvs = new TextView[10]; for (int i=0; inew>project Android Project Android

139

1

:

140

1

141

1

.

UTF-8 Properties:

142

1

intro:

Class

:

143

1

android.app.Activity ) ) ( main.xml . .

(

144

1

: . main.xml values ( new>file>colors.xml . lower case

colors.xml values ). :

#FFFFFFFF 145

1

. strings.xml

. : main.xml

1

android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/andbooklogo" android:adjustViewBounds="true" android:layout_centerHorizontal="true" >

. ) ). RelativeLayout LinearLayout . 147 ImageView res/drawable res/drawable-hdpi

1

RelativeLayout ( ) . android:layout_below . .

:

Layout

):

intro . )

package android.prog.easily; 148

1

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class intro extends Activity { // private Button getbest50; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); main // setContentView(R.layout.main); setUpViews(); } private void setUpViews() { getbest50 = (Button)findViewById(R.id.search); "); getbest50.setText(" // getbest50.setOnClickListener(new View.OnClickListener() { public void onClick(View v) 149

1

{ top50Handler(); } }); } protected void top50Handler() { } }

: . intro.java ( ( : package android.prog.easily; import java.io.Serializable; public class RatedItem implements Serializable { private static final long serialVersionUID = 5500258407135652423L; 150 RatedItem.java

1

private String name; private int id; private int voteCount; private double wScore; public RatedItem(String name, int id, int voteCount, double wScore) { this.name = name; this.id = id; this.voteCount = voteCount; this.wScore = wScore; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { 151

1

this.id = id; } public int getVoteCount() { return voteCount; } public void setVoteCount(int voteCount) { this.voteCount = voteCount; } public double getwScore() { return wScore; } public void setwScore(int wScore) { this.wScore = wScore; } }

RestDroidApplication.java Activities ). 152 )

1

: package android.prog.easily; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONException; import org.json.JSONObject; import android.app.Application; public class RestDroidApplication extends Application { // Qaym API Key private String key = "xxxxxxxxxxxxxxxxxxxx"; private int[] countryAndCityIds; private JSONObject[] ratedRestaurants; private ArrayList globalAdapterData; private RatedItem itemOnMap; private String branchesDetailsText;

public String getBranchesDetailsText() 153

1

{ return branchesDetailsText; } public void setBranchesDetailsText(String branchesDetailsText) { this.branchesDetailsText = branchesDetailsText; } public RatedItem getItemOnMap() { return itemOnMap; } public void setItemOnMap(RatedItem itemOnMap) { this.itemOnMap = itemOnMap; } @Override public void onCreate() { super.onCreate(); } @Override public void onTerminate() { super.onTerminate(); } 154

1

HTTP REST public String queryRESTurl(String url) { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); HttpResponse response; try { response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); String result1 = convertStreamToString(instream); instream.close(); return result1; } } catch (IOException e) 155

//

1

{ } return null; } String //

public String convertStreamToString(InputStream is) throws IOException { if (is != null) { StringBuilder sb = new StringBuilder(); String line; try { BufferedReader reader = new BufferedReader(new while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } } finally { is.close(); } 156

1

return sb.toString(); } else { return ""; } } /* JSON

JSON Key/value .. ..( ) " " JSON */ public JSONObject[] retrieveJSONs(String urlString) { String result = queryRESTurl(urlString); JSONObject[] jarr = null; if (result != null) { try { result = result.substring(1, result.length()1); 157

1

int counter = 0; boolean firstSeen = false; boolean secondSeen = false; for JSON for (int i = 0; i < result.length(); i++) { if (result.charAt(i) == '{' && firstSeen == { firstSeen = true; counter++; } else if (result.charAt(i) == '{' && firstSeen == true) { secondSeen = true; } if (result.charAt(i) == '}' && secondSeen == true) { secondSeen = false; } else if (result.charAt(i) == '}' && secondSeen == false) { firstSeen = false; 158 //

1

} } jarr = new JSONObject [counter]; int ind1 = 0; int ind2 = 0; boolean firstSeenS = false; boolean secondSeenS = false; JSON // for (int i = 0; i < result.length(); i++) { firstSeenS = false; secondSeenS = false; if (result.charAt(i) == '{' && firstSeenS false) == { firstSeenS = true; ind1 = i; for (int j = i+1; j < result.length(); j++) { if (result.charAt(j) == '{' && { secondSeenS = true; } 159

1

if (result.charAt(j) == '}' && { secondSeenS = false; } else if (result.charAt(j) == '}' && secondSeenS == false) { String mid = jarr[ind2] = new ind2++; i = j; break; } } } } } catch (JSONException e) { } } return jarr; } API // public String getUrlStringCommand(String command) 160

1

{ String service_url = "http://api.qaym.com/0.1/"; String url = ""; url = service_url + command + "/key=" + key; return url; } public void setCountryAndCityIds(int[] countryAndCityIds) { this.countryAndCityIds = countryAndCityIds; } public int[] getCountryAndCityIds() { return countryAndCityIds; } public JSONObject[] getRatedRestaurants() { return ratedRestaurants; } public void setRatedRestaurants(JSONObject[] ratedRestaurants) { this.ratedRestaurants = ratedRestaurants; } public ArrayList getGlobalAdapterData() 161

1

{ return globalAdapterData; } public void setGlobalAdapterData(ArrayList globalAdapter) { this.globalAdapterData = globalAdapter; } public RatedItem getGlobalAdapterDataItem(int position) { return globalAdapterData.get(position); } }

: protected void top50Handler() { try { API // " " String attrcommand = "cities/" + "57" + "/items/top"; 162

intro.java

1

// accessApplicationMethods() RestDroidApplication.java String attributesCommand = accessApplicationMethods().getUrlStringCommand(attrcom mand); JSONObject[] topCities = accessApplicationMethods().retrieveJSONs(attributesComma nd); // \ JSON ArrayList sRest = new ArrayList(); if (topCities != null) { for (int ff = 0; ff < topCities.length; ff++) { // "item_id" JSON int id = Integer.parseInt(topCities[ff].getString("item_id")); 163 ReatedItem

1

String name = topCities[ff].getString("item_name"); int voteConutt = Integer.parseInt(topCities[ff].getString("total_number_of_vot es")); double weScore = Double.parseDouble(topCities[ff].getString("weighted_score ")); // double weScoreR = weScore * 100; weScoreR = Math.round(weScoreR); weScoreR = weScoreR / 10 / 2; RatedItem singleRest = new id, voteConutt, weScoreR); sRest.add(singleRest); } } else { RatedItem singleRest = new 0, 0, 0); sRest.add(singleRest); } 164

RatedItem(name,

RatedItem("Empty List",

1

accessApplicationMethods().setGlobalAdapterData(sRest); // *** goToBest50Act(); } catch (JSONException e) { } } // RestDroidApplication.java protected RestDroidApplication accessApplicationMethods() { return (RestDroidApplication)getApplication(); }

. : // *** : goToBest50Act

protected void goToBest50Act() { Intent goToBest = new Intent(this, Best50.class); startActivity(goToBest); } 165

1

: package android.prog.easily; import java.util.ArrayList; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class intro extends Activity { // private Button getbest50;

intro.java

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); main // setContentView(R.layout.main); setUpViews(); } private void setUpViews() 166

1

{ getbest50 = (Button)findViewById(R.id.search);

setText(" // getbest50.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { top50Handler(); } }); } protected void goToBest50Act() { Intent goToBest = new Intent(this, Best50.class); startActivity(goToBest); } protected void top50Handler() { try { API // " " String attrcommand = "cities/" + "57" + "/items/top"; 167

1

// accessApplicationMethods() RestDroidApplication.java String attributesCommand = accessApplicationMethods().getUrlStringCommand(attrcom mand); JSONObject[] topCities = accessApplicationMethods().retrieveJSONs(attributesComma nd); // \ JSON ArrayList sRest = new ArrayList(); if (topCities != null) { for (int ff = 0; ff < topCities.length; ff++) { // "item_id" JSON int id = Integer.parseInt(topCities[ff].getString("item_id")); String name = topCities[ff].getString("item_name"); int voteConutt = 168 ReatedItem

1

Integer.parseInt(topCities[ff].getString("total_number_of_vot es")); double weScore = Double.parseDouble(topCities[ff].getString("weighted_score ")); // double weScoreR = weScore * 100; weScoreR = Math.round(weScoreR); weScoreR = weScoreR / 10 / 2; RatedItem singleRest = new id, voteConutt, weScoreR); sRest.add(singleRest); } } else { RatedItem singleRest = new 0, 0, 0); sRest.add(singleRest); }

RatedItem(name,

RatedItem("Empty List",

// RestDroidApplication.java Activities 169

1

accessApplicationMethods().setGlobalAdapterData(sRest); goToBest50Act(); } catch (JSONException e) { } } // RestDroidApplication.java protected RestDroidApplication accessApplicationMethods() { return (RestDroidApplication)getApplication(); } }

RestDroidApplications . ( action . Intent (

170

1

Best50.java. : RatedRestaurantListItem.java rest_list_item.xml \ . xml RelativeLayout RelativeLayout -. - ( )

:

RatedRestaurantListItem.java

package android.prog.easily; import android.content.Context; import android.util.AttributeSet; import android.widget.RelativeLayout; public class RatedRestaurantListItem extends RelativeLayout { public RatedRestaurantListItem(Context context, AttributeSet attr) { super(context, attr); } } : 171 rest_list_item.xml

1

):

layout ( ) )

res>layout ( new>file).

172

1

-->

RatedRestaurantListItem.java : 173 .( )

1

package android.prog.easily; import android.prog.easily.R; import android.content.Context; import android.util.AttributeSet; import android.widget.CheckedTextView; import android.widget.RelativeLayout; import android.widget.TextView; public class RatedRestaurantListItem extends RelativeLayout { private CheckedTextView ratedButton; private RatedItem ratedItem1; private TextView texty; private TextView texty2; public RatedRestaurantListItem(Context context, AttributeSet attr) { super(context, attr); } // xml @Override protected void onFinishInflate() { super.onFinishInflate(); ratedButton = (CheckedTextView)findViewById(R.id.ctext); 174

1

texty = (TextView)findViewById(R.id.ratedlabel); texty2 = (TextView)findViewById(R.id.scorelabel); } // public void setRatedItem(RatedItem ratedItem1) { this.ratedItem1 = ratedItem1; ratedButton.setText(ratedItem1.getName()); : texty.setText(" "+ratedItem1.getVoteCount()); : "+ratedItem1.getwScore() texty2.setText(" + "/5"); } public RatedItem getRatedItem() { return ratedItem1; } } : Best50.java rated_rest_list.xml :

Best50.java. . Activity ListActivity Design Patterns . ( ) Best50). Best50 Best50.java. ( 176 ) ) \ intro.java. List Adapter

1

! inner class . : package android.prog.easily; import java.util.ArrayList; import android.app.Activity; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.RatingBar; import android.widget.TextView; public class Best50 extends ListActivity { private TextView selectAttr; private RatedRestaurantListAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { 177

1

super.onCreate(savedInstanceState); setContentView(R.layout.rated_rest_list); setUpViews(); } // @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); RatedItem ite = accessApplicationMethods().getGlobalAdapterDataItem(posi tion); accessApplicationMethods().setItemOnMap(ite); goToViewOnMapActivity(); } private void goToViewOnMapActivity() { Intent goToMapActivity = new Intent(this, startActivity(goToMapActivity); } private void setUpViews() 178

1

{ selectAttr = (TextView)findViewById(R.id.RRselect); "); selectAttr.setText(" // adapter = new RatedRestaurantListAdapter setListAdapter(adapter); } Adapter . // ArrayAdapter class RatedRestaurantListAdapter extends ArrayAdapter { Activity context; private ArrayList myRatedItems; RatedRestaurantListAdapter(Activity context, { super(context, R.layout.rest_list_item, items); this.context=context; this.myRatedItems = items; } // public View getView(int position, View 179 InnerClass //

1

parent) {

convertView, ViewGroup

RatedRestaurantListItem row = (RatedRestaurantListItem)convertView; if (null == row) { LayoutInflator //

//

// LayoutInflater inflater=context.getLayoutInflater(); row = (RatedRestaurantListItem)inflater.inflate(R.layout.rest_list_it em, null); }

row.setRatedItem(myRatedItems.get(position)); RatingBar rateBar = (RatingBar)row.findViewById(R.id.rate); rateBar.setStepSize((float) 0.1);

rateBar.setRating((float)myRatedItems.get(position).getwSco 180

1

re()); return row; } public RatedItem getItem(int position) { return myRatedItems.get(position); } public void reload() { // notifyDataSetChanged(); } } protected RestDroidApplication accessApplicationMethods() { return (RestDroidApplication)getApplication(); } } OnListItemClick . ) position 181

1

. ( .

(

ViewOnMapActivity.java xml ( . : view_map.xml :

android:apikey . ) SDK.( trminal shell): cmd

debugging )

$ keytool -list -alias androiddebugkey \ -keystore xxx \ -storepass android -keypass android :Windows XP: C:\Documents

xxx

Windows Vista: C:\Users\\.android\debug.keystore

and Settings\\.android\debug.keystore OS X and Linux: ~/.android/debug.keystore

: Certificate fingerprint (MD5): 94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98 : http://code.google.com/android/maps-api-signup.html 183

1

API xxxxxxxxxx.

.

: http://code.google.com/android/add-ons/googleapis/mapkey.html

ViewOnMapActivity.java : AddressOverlay.java: package android.prog.easily; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import android.location.Address; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; import com.google.android.maps.Projection; public class AddressOverlay extends Overlay { private static final int CONTAINER_RADIUS private static final int = 1; CONTAINER_SHADOW_OFFSET 184

1

private Address address; private GeoPoint geopoint; Bitmap bb; public AddressOverlay(Address address) { super(); assert(null != address); // this.setAddress(address); Double convertedLongitude = address.getLongitude() * 1E6; Double convertedLatitude = address.getLatitude() * 1E6; setGeopoint(new GeoPoint( convertedLatitude.intValue(), convertedLongitude.intValue())); } // @Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { super.draw(canvas, mapView, shadow); Point locationPoint = new Point(); Projection projection = mapView.getProjection(); projection.toPixels(getGeopoint(), locationPoint); Paint containerPaint = new Paint(); containerPaint.setAntiAlias(true); 185

1

int containerX = locationPoint.x; int containerY = locationPoint.y; if (shadow) { containerX += CONTAINER_SHADOW_OFFSET; containerY += CONTAINER_SHADOW_OFFSET; containerPaint.setARGB(90, 0, 0, 0); canvas.drawCircle(containerX, containerY, CONTAINER_RADIUS, containerPaint); } else { containerPaint.setColor(Color.GREEN); canvas.drawCircle(containerX, containerY, CONTAINER_RADIUS, containerPaint); } } public void setAddress(Address address) { this.address = address; } public Address getAddress() { return address; } 186

1

public void setGeopoint(GeoPoint geopoint) { this.geopoint = geopoint; } public GeoPoint getGeopoint() { return geopoint; } }

ViewOnMapActivity.java: package android.prog.easily; import java.io.IOException; import java.util.List; import org.json.JSONException; import org.json.JSONObject; import android.graphics.Bitmap; import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.widget.TextView; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; 187

1

import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; public class ViewOnMapActivity extends MapActivity { public static final String ADDRESS_RESULT = "address";

private MapView mapView; private Address address; private TextView restName; private JSONObject[] restaurantsLocObjects; private String[] restaurantsBranchesNames; private double [][] longLat; private int maxLat = 0; private int minLat = 0; private int maxLon = 0; private int minLon = 0; Bitmap bmp; @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.view_map); setUpViews(); bringRestLocations(); 188

1

mapCurrentAddress(); } // private void bringRestLocations() { try { ID // Best50.java RestDroidApplication int restNum = accessApplicationMethods().getItemOnMap().getId(); API //

String command = "items/" + restNum + "/locations"; String restaurantsCommand = accessApplicationMethods().getUrlStringCommand(comman d); restaurantsLocObjects = accessApplicationMethods().retrieveJSONs(restaurantsCom mand); restaurantsBranchesNames = new String[restaurantsLocObjects.length]; longLat = new double[restaurantsLocObjects.length][2]; 189

1

for (int i = 0; i < restaurantsBranchesNames.length; i++) { restaurantsBranchesNames[i] = (i+1) " + + "\n" +" : restaurantsLocObjects[i].getString("country") + "\n" :" +" + restaurantsLocObjects[i].getString("city") + "\n" :" +" + restaurantsLocObjects[i].getString("location_id") + "\n" +" :" + restaurantsLocObjects[i].getString("address") + "\n" +" :" + restaurantsLocObjects[i].getString("phone") + "\n" :" +" + restaurantsLocObjects[i].getString("title") + "\n"; longLat[i][0] = Double.parseDouble(restaurantsLocObjects[i].getString("latit ude")); longLat[i][1] = Double.parseDouble(restaurantsLocObjects[i].getString("lon gitude")); } 190

1

} catch (JSONException e) { } } // protected void mapCurrentAddress() { Geocoder g = new Geocoder(this); List addresses; boolean firstTime = true; for (int i = 0; i < restaurantsBranchesNames.length; i++) { try { addresses = g.getFromLocation(longLat[i][0], longLat[i][1], 1); if (addresses.size() > 0) { address = addresses.get(0); List mapOverlays = mapView.getOverlays(); AddressOverlay addressOverlay = new AddressOverlay(address); 191

1

mapOverlays.add(addressOverlay); // zoom //

if (firstTime) { minLat = addressOverlay.getGeopoint().getLatitudeE6(); maxLat = addressOverlay.getGeopoint().getLatitudeE6(); minLon = addressOverlay.getGeopoint().getLongitudeE6(); maxLon = addressOverlay.getGeopoint().getLongitudeE6(); firstTime = false; } minLat = (minLat > addressOverlay.getGeopoint().getLatitudeE6()) ? addressOverlay.getGeopoint().getLatitudeE6() : minLat; maxLat = (maxLat < addressOverlay.getGeopoint().getLatitudeE6()) ? addressOverlay.getGeopoint().getLatitudeE6() : maxLat; minLon = (minLon > addressOverlay.getGeopoint().getLongitudeE6()) ? 192

1

addressOverlay.getGeopoint().getLongitudeE6() : minLon; maxLon = (maxLon < addressOverlay.getGeopoint().getLongitudeE6()) ? addressOverlay.getGeopoint().getLongitudeE6() : maxLon; mapView.invalidate(); } else { } } catch (IOException e) { } } final MapController mapController = mapView.getController(); GeoPoint center = new GeoPoint((maxLat + minLat)/2,(maxLon + minLon)/2); mapController.animateTo(center, new Runnable() { public void run() { mapController.zoomToSpan(maxLat minLat,maxLon - minLon); } 193

1

}); } private void setUpViews() { restName = (TextView)findViewById(R.id.restaurantNameMap);

restName.setText(accessApplicationMethods().getItemOnMa p().getName()); mapView = (MapView)findViewById(R.id.map); Zoomin/out // mapView.setBuiltInZoomControls(true); } @Override protected boolean isLocationDisplayed() { return true; } @Override protected boolean isRouteDisplayed() { return false; } protected RestDroidApplication 194

1

accessApplicationMethods() { return (RestDroidApplication)getApplication(); } }

:

195

1

AndroidManifest.xml 196

..

!

1

. . : 197

1

198

1

:

199

1

200

1

201

1

202

1

:( ) : $ cd /Developer/android-sdk-mac_86/tools/ $ ./adb devices : List of devices attached emulator-5554 device ( : $ cd /Developer/android-sdk-mac_86/tools/ $ ./emulator -avd GPSenables -partition-size 500 -avd \ GPSenables . ) -

. ) ( : 203 -

1

$ ./adb remount

remount succeeded ): )

$ ./adb push /Users/wa2el/Desktop/fontss/DroidSans-Bold.ttf /system/fonts/ $ ./adb push /Users/wa2el/Desktop/fontss/DroidSans.ttf /system/fonts/ $ ./adb push /Users/wa2el/Desktop/fontss/DroidSansFallback.ttf /system/fonts/

xml) . )

204

1

: : \ -

: Orientation Vertical . . Horizontal Widgets

205

1

layout_width fill_parent. layout_height . . android:text . android:id . fill_parent . .

206

1

wrap_content .

layout_height). . ) ):

(layout_width

207

1

Layout .. XML )

(

:

. LinearLayout : Widgets Orientation.

): 209 )

1

android:layout_weight :

211

1

android:layout_agravity Widgets . Pixels

Widgets

alignment android:layout_marginLeft . android:layout_marginLeft android:orientation=horizontal :

android:layout_gravity android:orientation=vertical: 213

1

.

android:gravity . \ . .

android:paddingLeft

Orientation

x.setOrientation(LinearLayout.HORIZONTAL); 214

1

.

Widget

x -

>TableRow< :

. . - android:layout_span : . . Columns - android:layout_column : Widget. .

) . .

- android:stretchColumns : Widgets ) - android:shrinkColumns : : :

215

1

: 218

1

. . Widgets ). 223

1

225

1

227

1

229

1

230