Android View Groups

A ViewGroup is a special view that can contain other views.

List view

ListView list = new ListView(this);
String[] listItems = {"Option 1","Option 2","Option 3"};
 
list.setAdapter(new ArrayAdapter<String>(
                this,android.R.layout.simple_list_item_1, listItems ));
                 
list.setOnItemClickListener(new OnItemClickListener() {
 
      @Override
      public void onItemClick(AdapterView<?> parent,
                    View view, int position, long id) { 
             Log.d("ListView test", "Item " + id + " was selected.");
       }
});

android view group

Spinner (drop down list)

Spinner spinner = new Spinner(this);
spinner.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listItems ));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
  
     @Override
     public void onItemSelected(AdapterView<?> parent,
                View view, int position, long id) {
              Log.d("Spinner test", "Item " + id + " was selected.");
              
     }
 
     @Override
     public void onNothingSelected(AdapterView<?> parent) {
              
     }
});

 

 

android view groups