You are on page 1of 12

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

Programming Mobile Phones


Two responsive screens, communicating using explicit intents Ian Bayley

Ian Bayley

Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

Lecture Overview I
1

Adding a Menu to Hello World How to program menus The le src\...\HelloWorldMenu.java Adding an Intent to Hello World How to use intents to create a subactivity The le src\...\HelloWorldIntent.java The le src\...\HelloWorldIntentSub.java The le AndroidManifest.xml How to use intents to communicate with that subactivity Adding a responsive ListView and EditText to Hello World What we need to do The le res\layout\main.xml The le src\...\HelloWorldListView.java
Ian Bayley Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

How to program menus The le src\...\HelloWorldMenu.java

1 . Adding a Menu to Hello World

Ian Bayley

Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

How to program menus The le src\...\HelloWorldMenu.java

How to program menus


1

when the menu button is pressed, add options to a Menu object


override call

Activity method onCreateOptionsMenu Menu method add to give it a label and id 0,1,2
two dummy arguments

...

groupId

order

when an option is selected, nd out which one it is and respond


override call

Activity method onOptionsItemSelected MenuItem method getItemId to give id 0,1,2 ...

context menus are similar Simplest app that has a menu show the id for each menu item as a toast
Ian Bayley Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

How to program menus The le src\...\HelloWorldMenu.java

The le src\...\HelloWorldMenu.java I


public class HelloWorldMenu extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); for (int i=0; i <13; i++) menu.add(Menu.NONE, i, Menu.NONE, "option "+i); return true; }
Ian Bayley Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

How to program menus The le src\...\HelloWorldMenu.java

The le src\...\HelloWorldMenu.java II


@Override public boolean onOptionsItemSelected(MenuItem item){ super.onOptionsItemSelected(item); Toast.makeText(this, "selected "+item.getItemId(), Toast.LENGTH_SHORT).show(); return true; }

Ian Bayley

Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

How to use intents to create a subactivity The le src\...\HelloWorldIntent.java The le src\...\HelloWorldIntentSub.java The le AndroidManifest.xml How to use intents to communicate with that subactivity

2 . Adding an Intent to Hello World

Ian Bayley

Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

How to use intents to create a subactivity The le src\...\HelloWorldIntent.java The le src\...\HelloWorldIntentSub.java The le AndroidManifest.xml How to use intents to communicate with that subactivity

How to use intents to create a subactivity


Denition of intent a message from one activity to tell another activity to start. How to start an activity with an explicit intent Intent intent = new Intent(fromActivityObject,toActivityClass.class); startActivity(intent); use Activity method finish to end Simplest app that uses intents move to subactivity when button is pressed, then back again
Ian Bayley Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

How to use intents to create a subactivity The le src\...\HelloWorldIntent.java The le src\...\HelloWorldIntentSub.java The le AndroidManifest.xml How to use intents to communicate with that subactivity

The le src\...\HelloWorldIntent.java I


public class HelloWorldIntent extends Activity { Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent( getApplicationContext(), HelloWorldIntentSub.class);
Ian Bayley Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

How to use intents to create a subactivity The le src\...\HelloWorldIntent.java The le src\...\HelloWorldIntentSub.java The le AndroidManifest.xml How to use intents to communicate with that subactivity

The le src\...\HelloWorldIntent.java II

} });

startActivity(intent);

Ian Bayley

Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

How to use intents to create a subactivity The le src\...\HelloWorldIntent.java The le src\...\HelloWorldIntentSub.java The le AndroidManifest.xml How to use intents to communicate with that subactivity

The le src\...\HelloWorldIntentSub.java I


public class HelloWorldIntentSub extends Activity { Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { finish(); } });
Ian Bayley Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

How to use intents to create a subactivity The le src\...\HelloWorldIntent.java The le src\...\HelloWorldIntentSub.java The le AndroidManifest.xml How to use intents to communicate with that subactivity

The le src\...\HelloWorldIntentSub.java II

Ian Bayley

Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

How to use intents to create a subactivity The le src\...\HelloWorldIntent.java The le src\...\HelloWorldIntentSub.java The le AndroidManifest.xml How to use intents to communicate with that subactivity

The le AndroidManifest.xml I


<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="uk.ac.brookes.bayley" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloWorldIntent" android:label="@string/app_name"> <intent-filter> <action
Ian Bayley Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

How to use intents to create a subactivity The le src\...\HelloWorldIntent.java The le src\...\HelloWorldIntentSub.java The le AndroidManifest.xml How to use intents to communicate with that subactivity

The le AndroidManifest.xml II


android:name="android.intent.action.MAIN"

/>

<category android:name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".HelloWorldIntentSub" android:label="@string/app_name2"> </activity> </application> </manifest>

Ian Bayley

Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

How to use intents to create a subactivity The le src\...\HelloWorldIntent.java The le src\...\HelloWorldIntentSub.java The le AndroidManifest.xml How to use intents to communicate with that subactivity

How to use intents to communicate with that subactivity


Using the result for something startActivityForResult(intent, requestCode ); ... public void onActivityResult (int requestCode,int resultCode, Intent data); // called automatically on return Bundling data to pass and unbundling it again intent.putExtra(variableName ,value ); ... (type ) data.getExtras().get(variableName )
Ian Bayley Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

What we need to do The le res\layout\main.xml The le src\...\HelloWorldListView.java

3 . Adding a responsive ListView and EditText to Hello World

Ian Bayley

Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

What we need to do The le res\layout\main.xml The le src\...\HelloWorldListView.java

What we need to do
add an unresponsive list view add a list to an adapter and add the adapter to the list view
an adapter makes a view for each item in the list one parameter supplies a layout for this

1 2

make it respond to an item click event


note generics awkwardness !

4 5

add an unresponsive edit text make it respond to a key event

Ian Bayley

Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

What we need to do The le res\layout\main.xml The le src\...\HelloWorldListView.java

The le res\layout\main.xml I


<?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" > <ListView android:id="@+id/listView" android:layout_height="wrap_content" android:layout_width="fill_parent"> </ListView> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
Ian Bayley Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

What we need to do The le res\layout\main.xml The le src\...\HelloWorldListView.java

The le res\layout\main.xml II

android:text="EditText" android:id="@+id/editText"> </EditText> </LinearLayout>

Ian Bayley

Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

What we need to do The le res\layout\main.xml The le src\...\HelloWorldListView.java

The le src\...\HelloWorldListView.java I


public class HelloWorldListView extends Activity { private ListView listView; private EditText editText; private final String[] COLOURS = new String[] {"Green", "Red"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView) findViewById(R.id.listView); editText = (EditText) findViewById(R.id.editText);
Ian Bayley Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

What we need to do The le res\layout\main.xml The le src\...\HelloWorldListView.java

The le src\...\HelloWorldListView.java II


listView.setAdapter( new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, COLOURS)); listView.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick( AdapterView<?> parent, View view, int position, long id) { if (position == 0) listView.setBackgroundColor(Color.GREEN);
Ian Bayley Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

What we need to do The le res\layout\main.xml The le src\...\HelloWorldListView.java

The le src\...\HelloWorldListView.java III


else listView.setBackgroundColor(Color.RED); Toast.makeText(getApplicationContext(), COLOURS[position], Toast.LENGTH_SHORT).show();

} ); editText.setOnKeyListener(new OnKeyListener(){ @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if ((event.getAction() ==
Ian Bayley Programming Mobile Phones

Adding a Menu to Hello World Adding an Intent to Hello World Adding a responsive ListView and EditText to Hello World

What we need to do The le res\layout\main.xml The le src\...\HelloWorldListView.java

The le src\...\HelloWorldListView.java IV


KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)) { editText.setText("");

} });

} return false; // hasn't consumed the event

Ian Bayley

Programming Mobile Phones

You might also like