Wednesday, May 2, 2012

ListView in Transcript Mode


ListView in Transcript Mode

This tutorial demonstrates how to scroll the list to the bottom to make new items visible when they are added. This is similar to a typical chat application where user enters a new text in a text box and it is added to the bottom and list keeps scrolling to the bottom.
The steps for creating the application are:
In your Android Project create a class that extends the ListActivity.
public class MainActivity extends ListActivity { }
Create the layout file "main.xml". To set the transcript mode, set android:transcriptMode="normal" for ListView.
<?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="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:stackFromBottom="true"
        android:transcriptMode="normal"/>
    <EditText android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
Open the MainActivity.java and declare.
    private EditText mEditText;
    private ArrayAdapter<String> mAdapter;
In the onCreate() method, set the laytout resource.
setContentView(R.layout.main);
Create an ArrayAdapter. Use Android's built-in layout android.R.layout.simple_list_1.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
Set the List Adapter.
setListAdapter(adapter);
Whenever user press the Directional Pad Center key or click, we want to add the text from the Edit Text to the List.
To acheive this first implement the OnClick and onKey event listeners.
        // Implement the OnClick event listener
        mEditText.setOnClickListener(new OnClickListener(){
   public void onClick(View v) {
                //call to update the list
    updateList();
   }
        });
        // Implement the OnKey event listener
        mEditText.setOnKeyListener(new OnKeyListener(){
         public boolean onKey(View arg0, int keyCode, KeyEvent event) {
          // Check if the Pad's center key pressed
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    switch (keyCode) {
                        case KeyEvent.KEYCODE_DPAD_CENTER:
                        case KeyEvent.KEYCODE_ENTER:
                            //Call to update the list
                         updateList();
                            return true;
                    }
                }
   return false;
  }
 });
Now, implement the updateList() method. It just adds the text from the Edit Text to the end of ArrayAdapter's array.
    private void updateList() {
     // Add the input string to adapter's array
     mAdapter.add(mEditText.getText().toString());
     mEditText.setText("");
    }
Final code for the MainActivity.java:
public class MainActivity extends ListActivity {
  
    private EditText mEditText;
    private ArrayAdapter<String> mAdapter;
 
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set the layout for this Activity
        setContentView(R.layout.main);
        // get the EditText from XML layout
        mEditText = (EditText) findViewById(R.id.edit_text);
        // Instantiate the ArrayAdapter
        mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
        // Set the list adapter
        setListAdapter(mAdapter);
       
        // Implement the OnClick event listener
        mEditText.setOnClickListener(new OnClickListener(){
   public void onClick(View v) {
                //call to update the list
    updateList();
   }
        });
        // Implement the OnKey event listener
        mEditText.setOnKeyListener(new OnKeyListener(){
         public boolean onKey(View arg0, int keyCode, KeyEvent event) {
          // Check if the Pad's center key pressed
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    switch (keyCode) {
                        case KeyEvent.KEYCODE_DPAD_CENTER:
                        case KeyEvent.KEYCODE_ENTER:
                            //Call to update the list
                         updateList();
                            return true;
                    }
                }
   return false;
  }
 });
   
    }
    // Method to update the list
    private void updateList() {
     // Add the input string to the end of adapter's array
     mAdapter.add(mEditText.getText().toString());
     mEditText.setText("");
    }
}


Conclusion:

In this example you learn how to create a ListView in Transcript Mode by setting the android:transcriptMode="normal" for ListView.

No comments: