[TinyTut]ListActivity with startSubActivity

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[TinyTut]ListActivity with startSubActivity

Postby Stephen.Ada » Mon Apr 28, 2008 4:28 pm

[align=center][TinyTut]ListActivity with startSubActivity[/align]

What to describe: here i will show u how to use a ListActivity to start a subactivity and handle return data, pop up dialog is also a fancy skill. 8) 8)

Difficulty:1 of 5

What it will look like below.(I am sorry I don't know how to upload image here) :)


Chinese Version in my blog:http://stephenchenit.spaces.live.com/

Description:
1. Create a ListActivity containing several country names;
2. Popup a edit dialog if click the list item;
3. If modified text is empty, it will show alert dialog.


Solution:
1. Create a project called ListDemo, and Change the super class Activity to ListActivity

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ListDemo extends ListActivity{
  2.  
  3. ......
  4.  
  5. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


2. Add UI elements in xml layout file(listdemo.xml)
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.  
  3.    android:orientation="vertical"
  4.  
  5.    android:layout_width="fill_parent"
  6.  
  7.    android:layout_height="fill_parent">
  8.  
  9.     <TextView  
  10.  
  11.        android:layout_width="fill_parent"
  12.  
  13.        android:layout_height="wrap_content"
  14.  
  15.        android:textSize="30dip"
  16.  
  17.        android:text="@string/list_title"/>
  18.  
  19.     <ListView
  20.  
  21.        android:id="@id/android:list"
  22.  
  23.        android:layout_width="fill_parent"
  24.  
  25.        android:layout_height="fill_parent"
  26.  
  27.        android:layout_weight="1"
  28.  
  29.        android:drawSelectorOnTop="false"/>
  30.  
  31. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Here, I set the testSize of TextView as 30dip, to make it more symmetric to the whole layout,.

3. Add some viriables in ListDemo class

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private int selectedItem = -1;
  2.  
  3. protected static final int SUB_ACTIVTY_REQUEST_CODE = 1314;
  4.  
  5. private String[] country={"China", "America", "Japan", "Russia", "Canada"};
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


selectedItem is the id of selected item, SUB_ACTIVTY_REQUEST_CODE is the requestcode of startSubActivity().

4. Initialize ListDemo, and set ListAdapter.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onCreate(Bundle icicle) {
  2.  
  3.     super.onCreate(icicle);
  4.  
  5.     setContentView(R.layout.listdemo);
  6.  
  7.     //Bind ListAdapter to show list item
  8.  
  9.     this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, country));
  10.  
  11. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


5. Create a new Activity called Editor, which is used to modify the item of ListDemo, and then create corresponding xml layout file(editor.xml)

Editor.class
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Editor extends Activity {
  2.  
  3. ....
  4.  
  5. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

editor.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <LinearLayout
  2.  
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:layout_width="wrap_content"
  6.  
  7.    android:layout_height="wrap_content"
  8.  
  9.    android:orientation="vertical"
  10.  
  11.    android:paddingLeft="6dip"
  12.  
  13.    android:paddingRight="6dip"
  14.  
  15.    android:paddingBottom="6dip">
  16.  
  17.     <EditText
  18.  
  19.        android:id="@+id/edit_title"
  20.  
  21.        android:maxLines="1"
  22.  
  23.        android:layout_marginTop="3dip"
  24.  
  25.        android:layout_marginBottom="2dip"
  26.  
  27.        android:layout_width="wrap_content"
  28.  
  29.        android:ems="20"
  30.  
  31.        android:layout_height="wrap_content"/>
  32.  
  33.     <Button
  34.  
  35.        android:id="@+id/ok"
  36.  
  37.        android:layout_width="wrap_content"
  38.  
  39.        android:layout_height="wrap_content"
  40.  
  41.        android:layout_gravity="right"
  42.  
  43.        android:text="@string/btn_confirm"/>
  44.  
  45. </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


6. Add strings in the string.xml for which we used in previous files.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <resources>
  4.  
  5.     <string name="app_name">List Demo</string>
  6.  
  7.     <string name="list_title">Select a country : </string>
  8.  
  9.     <string name="btn_confirm">Confirm</string>
  10.  
  11.     <string name="edit_widget">Edit CountryName</string>
  12.  
  13. </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


7. Add click handler in ListDemo, override onListItemClick function, in which we add code to start editor dialog.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. protected void onListItemClick(ListView l, View v, int position, long id) {
  4.  
  5.     // TODO Auto-generated method stub
  6.  
  7.     //record the current selected item
  8.  
  9.     selectedItem = position;
  10.  
  11.     //passing data
  12.  
  13.     Intent intent = new Intent(ListDemo.this, Editor.class);
  14.  
  15.     Bundle b = new Bundle();
  16.  
  17.     b.putString("COUNTRY", country[selectedItem]);
  18.  
  19.     intent.putExtras(b);
  20.  
  21.     //start new activity
  22.  
  23.     super.startSubActivity(intent, SUB_ACTIVTY_REQUEST_CODE);
  24.  
  25. }
  26.  
  27.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4

8. Retrieve and handle the data from Edtior Activity, and update the list data in ListDemo.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. protected void onActivityResult(int requestCode, int resultCode,
  4.  
  5.         String data, Bundle extras) {
  6.  
  7.     // TODO Auto-generated method stub
  8.  
  9.     super.onActivityResult(requestCode, resultCode, data, extras);
  10.  
  11.     //check requestcode
  12.  
  13.     if (requestCode == SUB_ACTIVTY_REQUEST_CODE) {
  14.  
  15.         //retrieve return data , and update the list
  16.  
  17.         country[selectedItem] = data;
  18.  
  19.         this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, country));
  20.  
  21.     }
  22.  
  23. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


9. Initialize Editor Activity, display the passing data in EditText. Set click listener for confirm button.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. protected void onCreate(Bundle icicle) {
  4.  
  5.     // TODO Auto-generated method stub
  6.  
  7.     super.onCreate(icicle);
  8.  
  9.     this.setContentView(R.layout.editor);
  10.  
  11.     //set edit text
  12.  
  13.     edit_text = (EditText)findViewById(R.id.edit_title);
  14.  
  15.     Bundle b = this.getIntent().getExtras();
  16.  
  17.     String t = b.getString("COUNTRY");
  18.  
  19.     edit_text.setText(t);
  20.  
  21.     //add click listener
  22.  
  23.     Button btn_ok = (Button)findViewById(R.id.ok);
  24.  
  25.     btn_ok.setOnClickListener(new Button.OnClickListener(){
  26.  
  27.         public void onClick(View arg0) {
  28.  
  29.             // TODO Auto-generated method stub
  30.  
  31.             String text = edit_text.getText().toString();
  32.  
  33.             if (text.trim().length() > 0){
  34.  
  35.                 //if text is not null, and finish
  36.  
  37.                 Editor.this.setResult(SUCCESS_RETURN_CODE, text);
  38.  
  39.                 Editor.this.finish();
  40.  
  41.             }
  42.  
  43.             else{
  44.  
  45.                 //else if the modified text is nul, show alert dialog
  46.  
  47.                 showAlert("Error", R.drawable.icon, "Text can't be empty", "OK", false);
  48.  
  49.             }
  50.  
  51.         }
  52.  
  53.     });
  54.  
  55. }
  56.  
  57. Button btn_ok;
  58.  
  59. EditText edit_text;
  60.  
  61. protected final int SUCCESS_RETURN_CODE = 1;
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


10. Configure the AndroidManifest.xml file, add Editor Activity in this configure file, and modify the theme of Editor to Dialog, in which way the Editor can show in dialog form, without filling out the full screen.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    package="org.stephenada.android.listdemo">
  6.  
  7.     <application android:icon="@drawable/icon">
  8.  
  9.         <activity android:name=".Editor" android:label="@string/edit_widget"
  10.  
  11.                    android:theme="@android:style/Theme.Dialog">
  12.  
  13.             <intent-filter>
  14.  
  15.                 <action android:name="android.intent.action.VIEW" />
  16.  
  17.                 <category android:name="android.intent.category.DEFAULT" />
  18.  
  19.             </intent-filter>
  20.  
  21.         </activity>
  22.  
  23.         <activity android:name=".ListDemo" android:label="@string/app_name">
  24.  
  25.             <intent-filter>
  26.  
  27.                 <action android:name="android.intent.action.MAIN" />
  28.  
  29.                 <category android:name="android.intent.category.LAUNCHER" />
  30.  
  31.             </intent-filter>
  32.  
  33.         </activity>
  34.  
  35.     </application>
  36.  
  37. </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


That is it. Though a little easy, but useful for novice in some degree , I think. 8) 8) 8)
Attachments
2.JPG
2.JPG (32.25 KiB) Viewed 9405 times
1.JPG
1.JPG (30.54 KiB) Viewed 9405 times
ListDemo.rar
(37.72 KiB) Downloaded 618 times
Stephen.Ada
Junior Developer
Junior Developer
 
Posts: 10
Joined: Mon Mar 10, 2008 3:18 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests