GridView Application

GridView application represent the data in the form of row and column.

Step 1.

Create new application with any name from file menu option.
File → New Project → Application name → select API → Add Blank Activity → Activity Name → finish

Step 2.

In activity_main.xml layout file design your application.
Add following Controls:-

Step 3.

Import Package and write following coding in MainActivity.java class file.

    
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.GridView;
    import android.widget.TextView;
    import android.widget.Toast;


  public class MainActivity extends ActionBarActivity {

    GridView gd;
    String []arr={"Akhtar","Manzar","Hussainn","Raza","hello","A","B","C"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gd=(GridView)findViewById(R.id.grd1);
        ArrayAdapter<String> adp=new ArrayAdapter<String>
                (this,android.R.layout.simple_list_item_1,arr);
        gd.setAdapter(adp);
        gd.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String data=((TextView)view).getText().toString()+" is selected";
        Toast.makeText(getApplicationContext(),data,Toast.LENGTH_LONG).show();
            }
        });

    }
    
    

Step 4. Final Output

grid view
Advertisement here

Share this Page.