ListView with TextView as cell renderer

Description

The following code shows how to create a ListView with TextView as cell renderer.

Example

res/values/string.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="footer">This is a demo of the ListView. Click and hold on to one of the items above to show the context menu.</string>
    <string name="app_name">ListViewDemo</string>
  <string-array name="countries">
    <item>Sweden</item>
    <item>Norway</item>
    <item>Finland</item>
    <item>Denmark</item>
    <item>Germany</item>
    <item>Poland</item>
    <item>Netherlands</item>
    <item>Belgium</item>
  </string-array>
  <string-array name="menu">
    <item>Edit</item>
    <item>Delete</item>
    <item>Send</item>
  </string-array>
</resources>

Main layout file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    <ListView
    android:id="@+id/list"  
      android:layout_width="fill_parent" 
      android:layout_height="0px"
      android:layout_weight="1"
      />
  <TextView
    android:id="@+id/footer"  
      android:layout_width="fill_parent" 
      android:layout_height="60dip"
      android:text="@string/footer"
      android:padding="4dip"
      android:background="#FF666666"
      />
</LinearLayout>

res/layout/listitem.xml


<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:textSize="24dip"
  android:padding="8dip"
  >
</TextView>

Java code


import java.util.Arrays;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewDemoActivity extends Activity {
  private String[] Countries;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Countries = getResources().getStringArray(R.array.countries);
        Arrays.sort(Countries);
        
    ListView list = (ListView)findViewById(R.id.list);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, Countries);
    list.setAdapter(adapter);
    registerForContextMenu(list);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
      if (v.getId()==R.id.list) {
          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        menu.setHeaderTitle(Countries[info.position]);
        String[] menuItems = getResources().getStringArray(R.array.menu); 
        for (int i = 0; i<menuItems.length; i++) {
          menu.add(Menu.NONE, i, i, menuItems[i]);
      }
      }
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
      int menuItemIndex = item.getItemId();
    String[] menuItems = getResources().getStringArray(R.array.menu);
    String menuItemName = menuItems[menuItemIndex];
      String listItemName = Countries[info.position];
      
      TextView text = (TextView)findViewById(R.id.footer);
      text.setText(String.format("Selected %s for item %s", menuItemName, listItemName));
      return true;
    }
}





















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView