You are on page 1of 51

Android

Application

(၁

(၁) Hello eclipse world! open Application

htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

Project create File>New>Android Application Project

htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

Next Minimum Required SDK Android version 2.2 ppli ion 2.2 install sdk version

click application 3.0

htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

Browse (application Next ) logo

htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

com/htarhtarlwintun www.com/groups/htarhtarlwin .com www.htarhtarlwin.facebook.blogspot.facebook.

Next htarhtarlwin.facebook.blogspot.facebook.com/htarhtarlwintun www.com www.com/groups/htarhtarlwin .

com/htarhtarlwintun www.com/groups/htarhtarlwin .Install/Upgrade ( click install ) htarhtarlwin.facebook.com www.blogspot.facebook.

blogspot.com/htarhtarlwintun www.facebook.Accept All Install click htarhtarlwin.facebook.com www.com/groups/htarhtarlwin .

blogspot.com/htarhtarlwintun www.facebook.com www.facebook.com/groups/htarhtarlwin .2 ) USB create USB Cable check htarhtarlwin.project (1) debugging (2) (3) (4) OK Phone Run>Run device (Android version 2.

facebook.com/groups/htarhtarlwin ) .com/htarhtarlwintun www.facebook.com www.(5) (6) Phone menu Hello world! application (Settings>Applications>Manage ) Droid1 Hello World! application touch uninstall applications pk il bin Droid1.apk install o ( htarhtarlwin.blogspot.

facebook.facebook.l on ( click ) (၁) Run on Emulator Android android ( /tablet/note) (emulator) ( ) run emulator run run htarhtarlwin.com/groups/htarhtarlwin run project real device emulator ppli ion device .blogspot.com/htarhtarlwintun www.com www.

com/htarhtarlwintun www.blogspot.com www.facebook.Android Virtual Device Manager eclipse tool ool Android Virtual Device Manager click htarhtarlwin.facebook.com/groups/htarhtarlwin .

facebook.RAM 1024 512 or 256 or 128 ) htarhtarlwin.facebook.com www.blogspot.com/htarhtarlwintun www.com/groups/htarhtarlwin .

com/groups/htarhtarlwin .blogspot.com www.facebook.facebook.htarhtarlwin.com/htarhtarlwintun www.

com/htarhtarlwintun www.facebook.HHl Start click htarhtarlwin.com/groups/htarhtarlwin .facebook.blogspot.com www.

com www.blogspot.facebook.facebook.com/groups/htarhtarlwin .eclipse application Hello Run world! ( ) emulator htarhtarlwin.com/htarhtarlwintun www.

import android. import android.droid1>DroidActivity.example.Menu.//***** public class DroidActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { htarhtarlwin.app.os.facebook.widget.java Explorer create Droid1 package com.TextView.com/htarhtarlwintun www.Bundle.Activity.view.com www. import android.com/groups/htarhtarlwin .droid1.(၁) ( ) coding application run Hello World! Eclipse Package Droid1>src>com.facebook.blogspot. import android.example.

inflate(R.com/htarhtarlwintun www.blogspot.com www.setText("Hello! I'm Dr. } } phone Emulator run run run (၁) Discussion tutorial Android SDK htarhtarlwin. Htar Htar Lwin"). return true.onCreate(savedInstanceState).menu.com/groups/htarhtarlwin . menu).//***** } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater(). TextView tv = new TextView(this).//***** tv.super.facebook.//***** setContentView(tv).main.facebook.

facebook.facebook.blogspot.install install i k D sdk drive Start l check number of packages) Android 4.com www.2 ( (Android SDK install download ool Install * packages SDK pl click Manager om pl om check download (* = ) sdk folder ) SDK Manager htarhtarlwin.com/htarhtarlwintun www.com/groups/htarhtarlwin .

com www.blogspot.2 ) User Interface ( ool create a new Android project tool Next click click 3.facebook.eclipse Eclipse IDE download unzip eclipse Eclipse install Project create command line eclipse tools eclipse create m l o application run run run n oi ppli ion ADT Plugin (၁) ( SDK 2.com/groups/htarhtarlwin .com/htarhtarlwintun www.facebook.0 htarhtarlwin.

facebook.com/groups/htarhtarlwin .com www.facebook.com/htarhtarlwintun www.blogspot.( Next htarhtarlwin.

facebook.htarhtarlwin.com/htarhtarlwintun www.com www.blogspot.com/groups/htarhtarlwin .facebook.

facebook.blogspot.com/groups/htarhtarlwin .com www.htarhtarlwin.com/htarhtarlwintun www.facebook.

blogspot.htarhtarlwin.com/htarhtarlwintun www.facebook.com www.com/groups/htarhtarlwin .facebook.

com/apk/res/android" xmlns:tools="http://schemas.facebook.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.xml o copy <?xml version="1.com/htarhtarlwintun www.facebook.MyApp2>res>layout>activity_main.xml o <?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> htarhtarlwin.android.blogspot.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.com www.com/groups/htarhtarlwin .

Run Button run apk (coding ( ) emulator run Textbox in install ) htarhtarlwin.com/htarhtarlwintun www.facebook.facebook.blogspot.com/groups/htarhtarlwin .com www.

facebook.com/groups/htarhtarlwin .com www.(၁) ( ) coding htarhtarlwin.blogspot.com/htarhtarlwintun www.facebook.

com/groups/htarhtarlwin .com/tools" android:layout_width="match_parent" android:layout_height="match_parent" screen android:orientation="horizontal" textbox button width and height Textbox <EditText android:id="@+id/edit_message" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" textbox width and height android:hint="@string/edit_message" /> button <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" /> </LinearLayout> htarhtarlwin.com/apk/res/android" xmlns:tools="http://schemas.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.textbox button textbox button layout For layout <?xml version="1.facebook.facebook.blogspot.android.com www.com/htarhtarlwintun www.

blogspot.facebook.xml Add android:onClick="sendMessage" as follow: <Button android:layout_width="wrap_content" htarhtarlwin.0" encoding="utf-8"?> <resources> <string name="app_name">My First App</string> ppli ppli “En ppli ion ion m ion n g ” n on ing “ n ” n m o 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> (၁) ( ) textbox button display ( ) MyApp button project create textbox Open MyApp>res>layout>activity_main.com www.facebook.com/groups/htarhtarlwin .com/htarhtarlwintun www.<?xml version="1.

setContentView(R.java Add these codes.view.putExtra(EXTRA_MESSAGE.app.onCreate(savedInstanceState).com/groups/htarhtarlwin .com/htarhtarlwintun www. intent.blogspot.layout.os.toString().Intent.view. message). startActivity(intent).myfirstapp. } @Override public void onCreate(Bundle savedInstanceState) { super.Menu.myfirstapp.activity_main). EditText editText = (EditText) findViewById(R.Activity. public class MainActivity extends Activity { public final static String EXTRA_MESSAGE = "com. DisplayMessageActivity.EditText. import android. import android. import android. package com.content.widget.MESSAGE".facebook.example.example.android:layout_height="wrap_content" android:text="@string/button_send" n oi on li k n g ) Open MyApp>src>MainActivity.edit_message).com www.facebook. /** Called when the user clicks the Send button */ public void sendMessage(View view) { Intent intent = new Intent(this. htarhtarlwin. import android.id. String message = editText.Bundle.class). import android.getText(). import android.View.

menu). press Ctrl + Shift + O to import missing classes Now you need to create the DisplayMessageActivity class in order for this to work.com/htarhtarlwintun www. return true.blogspot. htarhtarlwin.activity_main.com www.} @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().example.facebook. 3. Fill in the activity details: o o o o o o Project: MyFirstApp Activity Name: DisplayMessageActivity Layout Name: activity_display_message Navigation Type: None Hierarchial Parent: com. Click Next.MainActivity Title: My Message Click Finish. In the window that appears. Click New in the toolbar. } } Tip: In Eclipse.inflate(R.facebook.menu. 2.com/groups/htarhtarlwin . open the Android folder and select Android Activity. 4. To create DisplayMessageActivity class 1.myfirstapp. Select BlankActivity and click Next.

com/htarhtarlwintun www.com www. Add it to the manifest htarhtarlwin. The class should look like this: public class DisplayMessageActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super. The new activity wizard in Eclipse.onCreate(savedInstanceState). setContentView(R.layout.activity_display_message).blogspot. } } All subclasses of Activity must implement the onCreate() method.facebook. It is where you must define the activity layout and where you should perform initial setup for the activity components. Open the DisplayMessageActivity.com/groups/htarhtarlwin . The system calls this when creating a new instance of the activity. There's also an implemtation of the onCreateOptionsMenu() method. but you won't need it for this app so you can remove it. The class already includes an implementation of the required onCreate() method.Figure 1.java file.facebook.

In h i pl y g i iy l ’ onCreate() method.blogspot.facebook. The complete onCreate() method for DisplayMessageActivity now looks like this: htarhtarlwin.example. You used the Eclipse tools to create the activity. <activity android:name=". it creates a default entry. You can get the Intent that started your activity by calling getIntent() and retrieve the data contained within it.facebook. using an <activity> element.MainActivity" /> </activity> </application> Receive the Intent Every Activity is invoked by an Intent.com/htarhtarlwintun www.xml... String message = intent.PARENT_ACTIVITY" android:value="com. AndroidManifest. regardless of how the user navigated there.EXTRA_MESSAGE).You must declare all activities in your manifest file. Then add the TextView to setContentView()..getStringExtra(MainActivity. > . create a TextView widget and set the text using setText().DisplayMessageActivity" android:label="@string/title_activity_display_message" > <meta-data android:name="android.com/groups/htarhtarlwin h oo i o h i i y’ l yo yp ing i ..com www.myfirstapp. get the intent and extract the message delivered by MainActivity: Intent intent = getIntent(). It should look like this: <application . Display the Message To show the message on the screen.support.

com/htarhtarlwintun www.blogspot. // Create the text view TextView textView = new TextView(this). textView.setText(message). import android.widget.getStringExtra(MainActivity.onCreate(savedInstanceState).EXTRA_MESSAGE). textView. // Get the message from the intent Intent intent = getIntent(). DisplayMessageActivity class looks like this: package com.os.facebook. import android. import android.app.myfirstapp.facebook.Bundle. // Set the text view as the activity layout setContentView(textView). String message = intent.setTextSize(40). htarhtarlwin. import android.com/groups/htarhtarlwin .example. public class DisplayMessageActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.Activity.onCreate(savedInstanceState).com www.@Override public void onCreate(Bundle savedInstanceState) { super. } Finally.content.Intent.TextView.

When it opens.EXTRA_MESSAGE).getStringExtra(MainActivity. and the message appears on the second activity.com/groups/htarhtarlwin .blogspot. String message = intent.com www.// Get the message from the intent Intent intent = getIntent().facebook. } } You can now run the app. textView. htarhtarlwin.setText(message). // Create the text view TextView textView = new TextView(this).com/htarhtarlwintun www. // Set the text view as the activity layout setContentView(textView). click Send.facebook.setTextSize(40). textView. type a message in the text field.

facebook. (၁၁) htarhtarlwin.com www.com/htarhtarlwintun www.0.facebook. Both activities in the final app.blogspot.Figure 2. running on Android 4.com/groups/htarhtarlwin .

com/groups/htarhtarlwin .blogspot.setText(new Date(). import android. import android.skeleton. btn.example.View.onCreate(icicle).Date. } public void onClick(View view) { updateTime().toString()).view.facebook.setOnClickListener(this).util. htarhtarlwin.widget.Button. import android.java file activity name o Now o package com.app. setContentView(btn).Activity.facebook.OnClickListener { Button btn.n oi application i project current time create display Project name skeleton Now.com www. updateTime(). import java. public class Now extends Activity implements View.os.Bundle. @Override public void onCreate(Bundle icicle) { super. import android. } private void updateTime() { btn.com/htarhtarlwintun www. btn = new Button(this).

program android-specific class l android import package public class Now extends Activity implements View.Bundle.Activity button (btn) android widget I l m n base class extend hold widget extend . import android. Activity Now base class program (Button application i g ) on implement @Override htarhtarlwin.Activity.facebook.view. import android.util.View.OnClickListener { Button btn. import java.skeleton.facebook.Button. import android.blogspot.} } Discussion package com.app.example.widget.com/htarhtarlwintun www.os.app.com/groups/htarhtarlwin li k Now activity OnClickListener trap View p li l Now android. package declare import android.com www.Date.

public void onCreate(Bundle icicle) { super.com/groups/htarhtarlwin .setText(new Date(). setContentView(btn).toString()). btn.setOnClickListener(this).com www. button setOnclickListener() updateTime() button method program current run time display instance create instance button li k onCreate() method setContentVi public void onClick(View view) { updateTime().facebook.blogspot.facebook. btn = new Button(this). htarhtarlwin.com/htarhtarlwintun www. so the stock Android activity initialization can be done.onCreate(icicle). updateTime(). } Activity invoke The first thing you should do is chain upward to the superclass. } Button invoke click onClickListener instance button updateTime method button configure ll click onClick() method private void updateTime() { btn.

} Activity open button label button current click time setText() program tap current time run tap onCreate() method run updateTime() run tap current n run time im htarhtarlwin.com/groups/htarhtarlwin .com/htarhtarlwintun www.blogspot.facebook.facebook.com www.

main or R.blogspot.facebook.xml button create layout name o file i g generate yl text “ ol ” ol aapt tool l yo R.com/groups/htarhtarlwin .layout.(၁၁) i g create o il XML-based layout widget on in o ol l m n i g on in Button element n oi l yo file layout R.id.java p op attribute value button i attribute p op i l yo relationship widget relationship l yo il l yo activity attach htarhtarlwin.button xml code project NowRedux main l yo run button button tap ml main.com/htarhtarlwintun www.facebook.com www.

view.button <RelativeLayout xmlns:android="http://schemas.com www.android.facebook.com/tools"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </RelativeLayout> XML layout file button create xmlns:android="http://schemas.com/groups/htarhtarlwin android.com/apk/res/android" Root element Android XML l m n name space declaration android:id="@+id/button" button jave identifier element java l m n Button Button Android-supplied XML element widget widget htarhtarlwin.com/htarhtarlwintun www.com/apk/res/android" xmlns:tools="http://schemas.android.android.blogspot.facebook.View Button class name subclass i code code android:id attribute i inherit namespace root declare hil n .

facebook. import android.Date.View.Activity.example. j package com. import java.com/groups/htarhtarlwin . import android.os.xml layout file back setContentView(R.com www.view. htarhtarlwin.Button.java import android.com/htarhtarlwintun www.util.Bundle.example.app. statement button findViewById().blogspot.layout.layouts.widget.main).java file onCreate() call NowRedux.MyWidget android:layout_width="wrap_content" android:layout_height="wrap_content" button android:text="" button space button main.create com. o text width height full package declaration XML java code code res/layout access NowRedux.facebook. import android.

@Override public void onCreate(Bundle icicle) { super.facebook. btn.blogspot. btn=(Button)findViewById(R.com/htarhtarlwintun www. } private void updateTime() { btn. } public void onClick(View view) { updateTime().layout.setText(new Date().button).id.onCreate(icicle). updateTime().main).facebook. setContentView(R.setOnClickListener(this).OnClickListener { Button btn.toString()). } } run button program button tap XML code htarhtarlwin.com/groups/htarhtarlwin .com www.public class NowRedux extends Activity implements View.

button Assigning Labels Label android TextView textbox user box htarhtarlwin.com www.com/groups/htarhtarlwin user edit .facebook. label.(၁၁) Employing Basic Widget i g Widget filed.facebook.com/htarhtarlwintun www.blogspot.

facebook.android.blogspot.com/htarhtarlwintun www.label Label create TextView element button property create textbox TextView instance XML layout file create android: text text p op i android:typeface monospace android:textstyle bold.xml run <TextView xmlns:android="http://schemas. italic or bold and italic android:textColor RGB project Label main.com www.facebook.com/groups/htarhtarlwin .com/apk/res/android" android:id="@+id/TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="You were expecting something profound" /> o p activity name file create main o hex format htarhtarlwin.

blogspot.Fields of Green.com/htarhtarlwintun www.com/groups/htarhtarlwin . Or Other Colors.facebook.com www. Edit Text Edit Text widget p op i android:autoText auto spelling android:capitalize capital o android:digits igi TextView subclass Edit Text htarhtarlwin.facebook.

android.facebook.com/htarhtarlwintun www.Bundle.android:singleline single line or multiline android:numeric numeric-only input android:password password input android:phoneNumber phon n m Field FieldDemo main.blogspot.app.com/groups/htarhtarlwin file project create activity name .Activity.android.com www. import android.basic. htarhtarlwin.facebook.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas. import android.com/apk/res/android" android:id="@+id/field" android:layout width="fill parent" android:layout height="fill parent" android:singleLine="false" /> android:singleLine l FieldDemo.java o package com.xml o <?xml version="1.commonsware.os.

import android. You may " + "obtain a copy of the License at " + "http://www.onCreate(icicle).blogspot.setText("Licensed under the Apache License.EditText.0 " + "(the \"License\"). setContentView(R.0"). public class FieldDemo extends Activity { @Override public void onCreate(Bundle icicle) { super.com www.id.facebook.layout. Version 2. fld. EditText fld=(EditText)findViewById(R.apache.widget. you may not use this file " + "except in compliance with the License.org/licenses/LICENSE-2.facebook. } } Run htarhtarlwin.com/groups/htarhtarlwin .field).com/htarhtarlwintun www.main).

com/groups/htarhtarlwin .com/htarhtarlwintun www.(၁၁) htarhtarlwin.com www.blogspot.facebook.facebook.