1 / 23

Building your first app

Building your first app. Better reference the original webpage : http://developer.android.com/training/basics/firstapp/index.html. Basic Requirements. Download the Android SDK. http :// developer.android.com/sdk/index.html

skylar
Download Presentation

Building your first app

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Building your first app Better reference the original webpage : http://developer.android.com/training/basics/firstapp/index.html

  2. Basic Requirements • 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. Basic Requirements(cont’d) • 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. Basic Requirements(cont’d) • Download the latest SDK tools and platforms using the SDK Manager.

  5. Create an Android project • 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、Themethen click Next • select BlankActivity and click Next. • click Finish

  6. Important checking • AndroidManifest.xml have <uses-sdk>element • src/ have includes an Activityclass • res/ have app resourceswith is something like: drawable-hdpi/ 、layout/、values/

  7. Run on a Real Device • Plug in your device to your development machine with a USB cable.(install the appropriate USB driverfor your deviceif needed) • Enable USB debugging on your device.(On Android 4.0 and newer, it's in Settings > Developer options.) • Run in Eclipse: • 1.clickRun  from the toolbar. • 2. In the Run as window that appears, select Android Application and click OK.

  8. Run on emulator • 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 on emulator(cont’d) • Run in Eclipse: • 1.click Run  from the toolbar. • 2. In the Run as window that appears, select Android Application and click OK.

  10. Building a Simple User Interface • The graphical user interface for an Android app is built using a hierarchy ofView and ViewGroup objects. • View objects are usually UI widgets such as buttons or text 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.

  11. Create a Linear Layout • Open the activity_main.xml file from the res/layout/ directory. • First, delete the <TextView> element and change the <RelativeLayout> element to <LinearLayout>. Then add the android:orientation attribute and set it to "horizontal".  • Add a Text Field-To create a user-editable text field, add an <EditText> element inside the <LinearLayout> The result looks like this: • <?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns: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" ></LinearLayout>

  12. Add a Text Field • To create a user-editable text field, add an <EditText> element inside the <LinearLayout>

  13. Add String Resources • 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: • <?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></resources>

  14. Add a Button • Now add a <Button> to the layout, immediately following the <EditText> element: • <Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_send" /> • 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.

  15. Starting Another Activity • 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 <Button> element: • <Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_send"android:onClick="sendMessage" /> • 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.

  16. Build an Intent • 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. • Inside the sendMessage() method, create an Intent to start an activity called DisplayMessageActivity: • Intent intent = new Intent(this, DisplayMessageActivity.class);

  17. Build an Intent(cont’d) • 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) • The Class of the app component to which the system should deliver the Intent (in this case, the activity that should be started)

  18. Build an Intent(cont’d) • 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: • Intent intent = new Intent(this, DisplayMessageActivity.class);EditTexteditText = (EditText) findViewById(R.id.edit_message);String message = editText.getText().toString();intent.putExtra(EXTRA_MESSAGE, message);

  19. Start the Second Activity • 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. • 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);EditTexteditText = (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. Create the Second Activity • ClickNew  in the toolbar. • In the window that appears, open theAndroid folder and select Android Activity • ClickNext.SelectBlankActivity 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. Receive the Intent • 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. • In the DisplayMessageActivity class’s onCreate() method, get the intent and extract the message delivered by MainActivity: • Intent intent = getIntent();String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

  22. Display the Message • 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(). • The complete onCreate() method for DisplayMessageActivity now looks like this: • @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);    // Get the message from the intentIntentintent = getIntent();    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);    // Create the text viewTextViewtextView = new TextView(this);textView.setTextSize(40);textView.setText(message);    // Set the text view as the activity layoutsetContentView(textView);}

  23. END Better reference the original webpage if any question.

More Related