Download presentation
Presentation is loading. Please wait.
1
Better reference the original webpage : http://developer.android.com/training/basics/firstapp/index.html
2
Download the Android SDK. http://developer.android.com/sdk/index.html Install the ADT plugin for Eclipse (if you’ll use the Eclipse IDE). Eclipse download : http://eclipse.org/mobile/
3
ADT plugin downlaod: Start Eclipse, then select Help > Install New Software. Click Add, in the top-right corner. In the Add Repository dialog that appears, enter "ADT Plugin" for the Name and the following URL for the Location:https://dl- ssl.google.com/android/eclipse/ Click OK.If you have trouble acquiring the plugin, try using "http" in the Location URL, instead of "https" (https is preferred for security reasons). In the Available Software dialog, select the checkbox next to Developer Tools and click Next. In the next window, you'll see a list of the tools to be downloaded. Click Next. Read and accept the license agreements, then click Finish.If you get a security warning saying that the authenticity or validity of the software can't be established, click OK. When the installation completes, restart Eclipse.
4
Download the latest SDK tools and platforms using the SDK Manager.
5
Click New in the toolbar. In the window that appears, open the Android folder, select Android Application Project, and click Next. Fill in the form that appears: Application Name 、 Project Name 、 Package Name 、 Minimum Required SDK 、 Target SDK 、 Compile With 、 Theme then click Next select BlankActivity and click Next. click Finish
6
AndroidManifest.xml have element src/ have includes an Activity classActivity res/ have app resources with is something like:app resources drawable-hdpi/ 、 layout/ 、 values/
7
Plug in your device to your development machine with a USB cable.(install the appropriate USB driver for your device if needed) Enable USB debugging on your device.(On Android 4.0 and newer, it's in Settings > Developer options.) Run in Eclipse: 1.click Run from the toolbar. 2. In the Run as window that appears, select Android Application and click OK.
8
Launch the Android Virtual Device Manager:In Eclipse, click Android Virtual Device Manager from the toolbar. In the Android Virtual Device Manager panel, click New. Fill in the details for the AVD. Click Create AVD. Select the new AVD from the Android Virtual Device Manager and click Start. After the emulator boots up, unlock the emulator screen.
9
Run in Eclipse: 1.click Run from the toolbar. 2. In the Run as window that appears, select Android Application and click OK.
10
The graphical user interface for an Android app is built using a hierarchy of View and ViewGroup objects.ViewViewGroup View objects are usually UI widgets such as buttons or text fields Viewbuttonstext fields ViewGroup objects are invisible view containers that define how the child views are laid out, such as in a grid or a vertical list. ViewGroup
11
Open the activity_main.xml file from the res/layout/ directory. First, delete the element and change the element to. Then add the android:orientation attribute and set it to "horizontal". android:orientation Add a Text Field-To create a user-editable text field, add an element inside the The result looks like this:
12
To create a user-editable text field, add an element inside the
13
By default, your Android project includes a string resource file at res/values/strings.xml. Add a new string named "edit_message" and set the value to "Enter a message.“ While you’re in this file, also add a "Send" string for the button you’ll soon add, called "button_send". The result for strings.xml looks like this: My First App Enter a message Send Settings MainActivity
14
Now add a to the layout, immediately following the element: The height and width are set to "wrap_content" so the button is only as big as necessary to fit the button's text. This button doesn't need the android:id attribute, because it won't be referenced from the activity code.android:id
15
Respond to the Send Button To respond to the button's on-click event, open theactivity_main.xml layout file and add theandroid:onClick attribute to the element:android:onClick The android:onClick attribute’s value, "sendMessage", is the name of a method in your activity that the system calls when the user clicks the button.android:onClick
16
Intent represents an app’s "intent to do something." You can use intents for a wide variety of tasks, but most often they’re used to start another activity.Intent Inside the sendMessage() method, create an Intent to start an activity called DisplayMessageActivity:Intent Intent intent = new Intent(this, DisplayMessageActivity.class);
17
The constructor used here takes two parameters: A Context as its first parameter (this is used because the Activity class is a subclass of Context)ContextActivityContext The Class of the app component to which the system should deliver the Intent (in this case, the activity that should be started)ClassIntent
18
An intent not only allows you to start another activity, but it can carry a bundle of data to the activity as well. Inside thesendMessage() method, use findViewById() to get theEditText element and add its text value to the intent:findViewById()EditText Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id.edit_message); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message);
19
To start an activity, call startActivity() and pass it your Intent. The system receives this call and starts an instance of the Activity specified by the Intent.startActivity()IntentActivityIntent With this new code, the complete sendMessage() method that's invoked by the Send button now looks like this: /** Called when the user clicks the Send button */ public void sendMessage(View view) { Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id.edit_message); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); } Now you need to create the DisplayMessageActivity class in order for this to work.
20
Click New in the toolbar. In the window that appears, open theAndroid folder and select Android Activity Click Next.Select BlankActivity and click Next. Fill in the activity details:Project: MyFirstApp Activity Name: DisplayMessageActivity Layout Name: activity_display_message Title: My Message Hierarchial Parent: com.example.myfirstapp.MainActivity Navigation Type: None Click Finish.
21
Every Activity is invoked by an Intent, regardless of how the user navigated there. You can get the Intentthat started your activity by calling getIntent() and retrieve the data contained within it.ActivityIntent getIntent() In the DisplayMessageActivity class’s onCreate() method, get the intent and extract the message delivered by MainActivity:onCreate() Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
22
To show the message on the screen, create a TextView widget and set the text using setText(). Then add theTextView as the root view of the activity’s layout by passing it to setContentView().TextViewsetText()TextViewsetContentView() The complete onCreate() method for DisplayMessageActivity now looks like this:onCreate() @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the message from the intent Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); // Create the text view TextView textView = new TextView(this); textView.setTextSize(40); textView.setText(message); // Set the text view as the activity layout setContentView(textView); }
23
Better reference the original webpage if any question.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.