title: Android- Select an item of a GridView programmatically?
body: <p>I want to perform a click on an item after <code>setAdapter()</code> method. </p>
  
  <p>Consider if you have declared and initialized a <strong>Button</strong> in your <strong>Activity</strong> globally then you can perform a click on it programmatically from any scope of that activity and the listener will work. I want something similar like that, to be able to  perform a click on any item of my <strong>GridView</strong>.</p>
  
  <p>So far i have tried all of the below methods one by one but none of them seems to work, i tried different stack questions but not even a single answer worked for me.
        <code>iconsGrid.setSelection(2);</code>, 
        <code>iconsGrid.setSelected(true);</code>, 
        <code>iconsGrid.performClick();</code>, 
        <code>iconsGrid.performItemClick(iconsGrid, 2, 2);</code>,
        <code>iconsGrid.performItemClick(iconsGrid, 2, iconsGrid.getItemIdAtPosition(2));</code>,</p>
  
  <pre><code>//Activity
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      mTextView = (TextView) findViewById(R.id.txt);
      Integer[] resource_icons = { 
              R.drawable.ic_attachment,
              R.drawable.ic_attachment,
              R.drawable.ic_attachment,
              R.drawable.ic_attachment,
              R.drawable.ic_attachment,
              R.drawable.ic_attachment };
  
      GridView iconsGrid = (GridView) findViewById(R.id.gv_icons);
      IconGridAdapter iconAdapter = new IconGridAdapter(this, resource_icons);
      iconsGrid.setAdapter(iconAdapter);
     /*
      * I want to perform a click here
      */
  }
  
  //Adapter
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      LayoutInflater li;
      View grid;
      if (convertView == null) {
          grid = new View(mContext);
          li = ((Activity) mContext).getLayoutInflater();
          grid = li.inflate(R.layout.grid_cell_imageview, parent, false);
      } else {
          grid = (View) convertView;
      }
  
      ImageView imageView = (ImageView) grid.findViewById(R.id.image);
      imageView.setImageResource(imageId[position]);
  
      return grid;
  }
  </code></pre>