# Using GridView in Android Application

  • Add GridView on the activity layout which will automatically adds XML GridView object in the related xml layout as shown below


  • Create Activity layout : layout/results.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF234499"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/resultsGrid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:numColumns="5"
        android:background="#FF234499">
    </GridView>
</LinearLayout>
  • Create Adapter which is responsible for creating cells on the GUI 
public class TextAdapter extends BaseAdapter{
     private Context c;
     private String[] list;

     public TextAdapter(Context ctx) {
          this.c = ctx;
          list = new String[50];
         
          for (int i = 0; i < list.length; i++) {
               list[i] = "Cell #" + i;
          }
     }
    
     public int getCount() {
          return list.length;
     }

     public Object getItem(int arg0) {
          return null;
     }

     public long getItemId(int arg0) {
          return 0;
     }

     public View getView(int position, View convertView, ViewGroup parent) {
          final int p = position;
         
          TextView txtView = new TextView(c);
         
          if (position % 2 == 0) {
               txtView.setBackgroundColor(Color.BLACK);
          } else {
               txtView.setBackgroundColor(Color.WHITE);
          }
    
          //onclick enablement
          txtView.setOnClickListener(new View.OnClickListener() {
               public void onClick(View arg0) {
                    System.out.println("Postion clicked : " + p);
               }
          });
           
          txtView.setText(list[position]);
          return txtView;
     }
}
  • Create linkage between GridView and application code by adding below code
public class ResultsActivity extends Activity implements View.OnClickListener {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      //grid view
      GridView resultsGrid = (GridView) findViewById(R.id.resultsGrid);
      resultsGrid.setAdapter(new TextAdapter(this));
 }

#P2 Capital quiz on Android platform


Project Background
Develop a Quiz application to Display Country name and four capital options (out of one is correct). User could tap on any of the four options to select correct capital. Next question will be displayed once user selects correct answer.

Output
  
   

Project Structure in the Eclipse



























Source code 



package com.myapps.model;



/**

 * Model to hold information about each question and related four answer options,

 * out of which one is correct.

 * @author Santosh Lawoo

 * @version 1.2
 */
public class Question {
           private int index;
           private String description;
           private String answer;
           private String[] options;
          
          
           public int getIndex() {
                      return index;
           }
           public void setIndex(int index) {
                      this.index = index;
           }
           public String getDescription() {
                      return description;
           }
           public void setDescription(String description) {
                      this.description = description;
           }
           public String getAnswer() {
                      return answer;
           }
           public void setAnswer(String answer) {
                      this.answer = answer;
           }
           public String[] getOptions() {
                      return options;
           }
           public void setOptions(String[] options) {
                      this.options = options;
           }
}






-----------------------