Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android N Amanquah.

Similar presentations


Presentation on theme: "Android N Amanquah."— Presentation transcript:

1 Android N Amanquah

2 Installation: Install jdk Install android sdk starter pack
(this also installs sdk tools) Install platform tools (eg unzip platform-tools_r03-windows, rename to “platform tools”, directly under android-sdk/) Install sdkPlatform (eg unzip android-2.2_r02-windows into “platforms”) Create an AVD using android sdk and avd mgr Netbeans: Install nbandriod into netbeans Eclipse: install ADT add SDK's tools/ and platform-tools to your PATH

3 Building Blocks There are four building blocks to an Android application: Activity - composed of Views, responds to events Intent Receiver Service – long lived running code eg media plyr Content Provider eg store in SQLite db AndroidManifest.xml. declare the components of your application capabilities and requirements of your components

4 Intents Intent what an application wants done- a request to do smth.
Use Intent to move from screen to screen Two main components of intent data structure: the action and the data to act upon Typical values for action are MAIN (entry point: can be started from icon), VIEW, PICK, EDIT, etc. The data is expressed as a Uniform Resource Indicator (URI) Eg to view a website: new Intent(android.content.Intent.VIEW_ACTION, ContentURI.create("

5 Intent Filters Intent Filter
a description of what intents an activity (or intent receiver) is capable of handling Activities publish their IntentFilters in the AndroidManifest.xml file. Navigating from screen to screen is accomplished by resolving intents. To navigate forward, an activity calls startActivity(myIntent).  systems looks for matching intent filters Advantages: reuse of existing functionality Activities can be replaced/subsituted.

6 Intent Receivers execute in reaction to an external event
App need not be running (eg to a phone call) Apps can broadcast intents Context.broadcastIntent().

7 Hello world: programmatically
package com.example.helloandroid; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloAndroid extends Activity {    /** Called when the activity is first created. */      public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        TextView tv = new TextView(this);        tv.setText("Hello, Android");        setContentView(tv);    } } Note: copying and pasting this code results in additional invisible characters Past in Notepad first

8 Hello world: by XML package com.example.helloandroid;
import android.app.Activity; import android.os.Bundle; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } Configure the xml file to indicate what is loaded in the activity

9 XML Layout- a hierarchy of views
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content“ android:text="Hello World"/> </LinearLayout> View class is base for all widgets ViewGroup

10 Views views in a window are arranged in a single tree
can add views from code or from tree of views in XML layout files After creating view, do some of the ff Set properties eg setText SetFocus Set up listeners Set visibility

11 View Hierarchy- view w button
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a TextView" /> <Button android:text="Hello, I am a Button" /> </LinearLayout>

12 Comparison to Swing Activities i-(J)Frame in Swing.
Views -(J)Components in Swing. TextViews -(J)Labels in Swing. EditTexts -(J)TextFields in Swing. Buttons -(J)Buttons in Swing. Handling Events is similar: myView.setOnClickListener(new OnClickListener(){ ... // Swing myButton.addActionListener(new ActionListener() {...

13 XML layout <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="     android:layout_width="fill_parent"   android:layout_height="fill_parent"   These refer to string.xml and also create a new id called textview which can be referred to in code. A compiled object representation of the layout defined in /res/layout/main.xml is R.layout.main Textview can be referred to subsequently as R.id.textview

14 Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=" package="org.anddev.android.hello_android"> <application <activity android:name=".Hello_Android" <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

15 References & resources in code
prefix to introduce a resource reference (will be in color.xml) R.java is an auto-generated Xml: In code: Button myButton = (Button) findViewById(R.id.my_button);

16 A button (View) and event
Views may have an integer id associated with them. –assigned in xml file Eg: define btn and assign an id: <Button          android:layout_width="wrap_content"      android:layout_height="wrap_content"     In the onCreate method of activity, find the button: Button myButton = (Button) findViewById(R.id.my_button); Event button.setOnClickListener(new OnClickListener() {     public void onClick(View v) {         // Perform action on clicks         Toast.makeText(HelloFormStuff.this, “Msg displayed in msgbox", Toast.LENGTH_SHORT).show();     } }); @+id/foo means you are creating an id named foo in the namespace of your application. You can refer to means you are referring to an id defined in the android namespace. This namespace is the namespace of the framework The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file).

17 Create a form by adding widgets
Create a basic linear view eg <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent" > </LinearLayout> Add widgets to this layout. Add their corresponding events if any, to onCreate()

18 Radio Button(use radioGroup)
  <RadioGroup       android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:orientation="vertical">       <RadioButton           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="Red" />       <RadioButton           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="Blue" />     </RadioGroup> To do something when each RadioButton is selected, you need an View.OnClickListener. Place radio group inside say a LinearLayout

19 Acting when clicked: Add ff to onCreate() method:
private OnClickListener radio_listener = new OnClickListener() {     public void onClick(View v) {         // Perform action on clicks         RadioButton rb = (RadioButton) v;         Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();     } }; Add ff to onCreate() method:   final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);   final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);   radio_red.setOnClickListener(radio_listener);   radio_blue.setOnClickListener(radio_listener);

20 Layouts

21 Excercise Create a range of layouts. Test them
See


Download ppt "Android N Amanquah."

Similar presentations


Ads by Google