Transcript 4th April

Android
N Amanquah
Installation:

Install jdk
Install android sdk starter pack
• (this also installs sdk tools)
Install platform tools (unzip into “platform tools”)
Install sdkPlatform (unzip into “platforms”)
Create and 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

http://developer.android.com/sdk/installing.html







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. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}
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
XML layout
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"/>
•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
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
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:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_button_text"/>
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();
}
});
Create a form by adding widgets




Create a basic linear view eg
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/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()
Radio Button(use radioGroup)


<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<RadioButton android:id="@+id/radio_blue"
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.
Acting when clicked:



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);
Excercise


Create a range of layouts. Test them
See
http://developer.android.com/resources/tutorials/
views/index.html