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

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

Next

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

Install/Upgrade (

click install

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

Accept All

Install

click

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

project (1) debugging (2) (3) (4) OK Phone Run>Run device (Android version 2.2 ) USB

create USB Cable

check

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

(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.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin )

l on ( click )

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

Android Virtual Device Manager eclipse tool ool Android Virtual Device Manager click

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

RAM 1024 512 or 256 or 128 )

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

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

HHl

Start

click

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

eclipse application Hello Run world!

( ) emulator

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

() ( ) coding application run Hello World! Eclipse Package Droid1>src>com.example.droid1>DroidActivity.java Explorer create Droid1

package com.example.droid1; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TextView;//***** public class DroidActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

super.onCreate(savedInstanceState); TextView tv = new TextView(this);//***** tv.setText("Hello! I'm Dr. Htar Htar Lwin");//***** setContentView(tv);//***** } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }

phone Emulator run run

run

() Discussion tutorial

Android

SDK

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

install install i k D sdk drive

Start l check number of packages) Android 4.2 ( (Android SDK install download ool Install * packages

SDK pl click

Manager om pl om

check download (* =

) sdk folder ) SDK Manager

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

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.2 ) User Interface ( ool create a new Android project tool Next click click 3.0

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

Next

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

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

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

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

MyApp2>res>layout>activity_main.xml o copy

<?xml 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.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.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

Run Button run apk (coding ( )

emulator

run Textbox in install

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

() ( ) coding

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

textbox

button textbox button layout

For layout <?xml 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" 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.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

<?xml version="1.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.xml Add android:onClick="sendMessage" as follow: <Button android:layout_width="wrap_content" htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

android:layout_height="wrap_content" android:text="@string/button_send" n oi on li k n g ) Open MyApp>src>MainActivity.java Add these codes. package com.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 class MainActivity extends Activity { public final static String

EXTRA_MESSAGE

"com.example.myfirstapp.MESSAGE"; /** 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); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

} @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } Tip: In Eclipse, press Ctrl + Shift + O to import missing classes Now you need to create the DisplayMessageActivity class in order for this to work. To create DisplayMessageActivity class 1. Click New in the toolbar. 2. In the window that appears, open the Android folder and select Android Activity. Click Next. 3. Select BlankActivity and click Next. 4. 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.example.myfirstapp.MainActivity Title: My Message

Click Finish.

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

Figure 1. The new activity wizard in Eclipse. Open the DisplayMessageActivity.java file. The class already includes an implementation of the required onCreate() method. 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 should look like this: public class DisplayMessageActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); } } All subclasses of Activity must implement 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 to the manifest

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

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

@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); } Finally, DisplayMessageActivity class looks like this: package com.example.myfirstapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class DisplayMessageActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

// 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); } } You can now run the app. When it opens, type a message in the text field, click Send, and the message appears on the second activity.

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

Figure 2. Both activities in the final app, running on Android 4.0.

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

n oi application

i project

current time create

display

Project name

skeleton Now.java file

activity name o

Now o

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 extends Activity implements View.OnClickListener { Button btn; @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()); htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

} } Discussion package com.example.skeleton; package declare

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import java.util.Date; program android-specific class l android import package

public class Now extends Activity implements View.OnClickListener { Button btn; Activity Now base class program (Button application i g ) on implement @Override htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin li k Now activity OnClickListener trap View p li l Now android.app.Activity button (btn) android widget I l m n base class extend hold widget extend

public void onCreate(Bundle icicle) { super.onCreate(icicle); btn = new Button(this); btn.setOnClickListener(this); updateTime(); setContentView(btn); } Activity invoke The first thing you should do is chain upward to the superclass, so the stock Android activity initialization can be done. 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(); } Button invoke click onClickListener instance button updateTime method button configure ll click onClick() method

private void updateTime() { btn.setText(new Date().toString()); htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

} 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.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

() 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.button xml code project NowRedux main l yo run button button tap ml main.xml button create layout name o file i g generate yl text ol ol aapt tool l yo R.layout.main or R.java p op attribute value button i attribute p op i l yo relationship widget relationship l yo il l yo activity attach

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

button <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.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.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.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin android.view.View Button class name subclass i code code android:id attribute i inherit namespace root declare hil n

create com.example.MyWidget android:layout_width="wrap_content" android:layout_height="wrap_content" button android:text="" button space button main.xml layout file back setContentView(R.layout.main); statement button findViewById(); j package com.example.layouts; o text width height

full package declaration

XML java code

code res/layout access

NowRedux.java file

onCreate() call

NowRedux.java

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import java.util.Date; htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin

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

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

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

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, italic or bold and italic android:textColor RGB project Label main.xml 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 something profound" /> o p activity name file create main o hex format

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

Fields of Green. Or Other Colors. Edit Text Edit Text widget p op i android:autoText auto spelling android:capitalize capital o android:digits igi TextView subclass Edit Text

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

android:singleline single line or multiline android:numeric numeric-only input android:password password input android:phoneNumber phon n m Field FieldDemo main.xml o <?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://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 l FieldDemo.java o package com.commonsware.android.basic; import android.app.Activity; import android.os.Bundle; htarhtarlwin.blogspot.com www.facebook.com/htarhtarlwintun www.facebook.com/groups/htarhtarlwin file project create activity name

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

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

()

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

You might also like