How to Write Android Application (2 to 12)

  • Published on
    28-Oct-2015

  • View
    40

  • Download
    3

Embed Size (px)

DESCRIPTION

About how to write android app

Transcript

Android Application () htarhtarlwin.blogspot.com

Android Application ()Hello world! Application eclipse open

Project create File>New>Android Application Project

Next click Minimum Required SDK 2.2 application Android version 2.2 install application sdk version 3.0

Browse (application logo Next )

Next

Install/Upgrade click ( install )

Accept All Install click

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

(5) Phone Hello World! (6) menu Droid1 application touch Hello world! application uninstall (Settings>Applications>Manage applications )apk file bin Droid1.apk install software ( ) data select button click ()

Android Application ()Run on Emulator Android application android device (/tablet/note) (emulator) run () project real device run emulator run emulator run Android Virtual Device Manager eclipse tool bar Android Virtual Device Manager tool click

( RAM 1024 512 or 256 or 128 )

HHl Start click

eclipse () application Run Hello world! emulator

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

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) { 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 run Emulator run run

Android Application ()Discussion tutorial Android SDK install install D: drive D:\sdk sdk D drive

Start SDK Manager check latest SDK tools platform download platform check Install * packages click (* = number of packages)Android 4.2 download () sdk folder (Android SDK install ) SDK Manager eclipse download unzip Eclipse IDE eclipse Eclipse android application ADT Plugin install Project create eclipse command line tools eclipse create application run run emulator run

Android Application ()( SDK 2.2 3.0 ) User Interface () Tool bar create a new Android project tool click Next click

() Next

MyApp2>res>layout>activity_main.xml code copy

MyApp2>values>strings.xml code

My First App Enter a message Send Settings MainActivity

Run emulator run Textbox Button run bin apk install (coding ())

Android Application () () coding textbox button textbox button layout For layout

res>layout>activity_main.xmlAdd android:onClick="sendMessage" as follow: ()Open MyApp>src>MainActivity.javaAdd 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); } @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 class1. 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: Project: MyFirstApp Activity Name: DisplayMessageActivity Layout Name: activity_display_message Navigation Type: None Hierarchial Parent: com.example.myfirstapp.MainActivity Title: My MessageClick Finish.

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 manifestYou must declare all activities in your manifest file, AndroidManifest.xml, using an element.You used the Eclipse tools to create the activity, it creates a default entry. It should look like this:

...

Receive the IntentEvery 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 the DisplayMessageActivity classs onCreate() method, get the intent and extract the message delivered by MainActivity:Intent intent = getIntent();String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);Display the MessageTo show the message on the screen, create a TextView widget and set the text using setText(). Then add the TextView as the root view of the activitys 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 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 {

@Overridepublic 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);}

}

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.

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

Android Application (9) htarhtarlwin.blogspot.com

Android Application ()Android device current time display application project create Project name skeleton activity name Now Now.java file source code

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;@Overridepublic 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());}}Discussionpackage 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 class import android-specific class android package

public class Now extends Activity implements View.OnClickListener {Button btn;Activity Now public class android.app.Activity base class extend program Now button (btn) hold (Button android widget widget application UI element Widget View base class extend ) Button click activity trap Now OnClickListener implement

@Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);btn = new Button(this);btn.setOnClickListener(this);updateTime();setContentView(btn);}Activity onCreate() method invoke The first thing you should do is chain upward to the superclass, so the stock Android activity initialization can be done. button instance create button click instance setOnclickListener() updateTime() method program run button current time display setContentView()

public void onClick(View view) {updateTime();}Button click button configure onClickListener instance onClick() method invoke updateTime method call button click

private void updateTime() {btn.setText(new Date().toString());}Activity open button click button label current time setText() program run tap current time tap

onCreate() method updateTime() run run run current time tap current time

Android Application ()Widget create activity attach Java code () XML-based layout file XML-based layout widget relationship widget continer relationship XML-based layout resource layout file res folder XML element attribute properties widget container properties Button element attribute value android:textStyle = bold button text bold layout aapt tool R.java file generate layout layout widget R.layout.main or R.id.button xml code button project create NowRedux layout name main xml code res/layout main.xml file run button button tap button

XML layout file button create

xmlns:android="http://schemas.android.com/apk/res/android"Root element Android XML namespace declare element root children name space declaration inherit

android:id="@+id/button" button jave code identifier android:id attribute element id java code element id ButtonButton XML element Button Android-supplied widget class name widget android.view.View subclass create full package declaration com.example.MyWidget

android:layout_width="wrap_content"android:layout_height="wrap_content" button width height

android:text=""button text space

button XML code main.xml res/layout layout file java code access NowRedux.java file onCreate() call back setContentView(R.layout.main); statement button findViewById(); java code NowRedux.java package com.example.layouts;

import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import java.util.Date;public class NowRedux extends Activityimplements View.OnClickListener {Button btn;@Overridepublic 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 tap program button XML code

Android Application ()Employing Basic Widget widget Widget filed, label, button Assigning LabelsLabel android TextView user edit textbox user box label textbox Label create TextView instance create XML layout file TextView element create button android: text property text properties android:typefacemonospace android:textstylebold, italic or bold and italic android:textColor RGB hex format - #FF0000 project create Label activity name main main.xml file code run

output

Fields of Green. Or Other Colors. Edit Text Edit Text widget TextView subclass Edit Text properties android:autoTextauto spelling

android:capitalize capital auto

android:digitsdigit

android:singlelinesingle line or multiline

android:numericnumeric-only input

android:passwordpassword input

android:phoneNumberphone number

Field project create activity name FieldDemo main.xml code

android:singleLine false FieldDemo.java file code package com.commonsware.android.basic;import android.app.Activity;import android.os.Bundle;import android.widget.EditText;public class FieldDemo extends Activity {@Overridepublic 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.comwww.facebook.com/htarhtarlwintunwww.facebook.com/groups/htarhtarlwin