title: Android UI - add buttons dynamically to Gridview
body: <p>This is where I am stuck now. I am stuck at dynamically adding buttons to gridview.<br>
  - My gridview stars with a one button.<br>
  - When user clicks that button a context menu , pops up, asking user to enter info, once that is done. A block in the grid view is created with that info<br>
  - This is shown in the picture  </p>
  
  <p>I have pasted the code. I do not have clear idea on how to do it. I am assuming it will be inflating a new view and adding it to parent(layout or grid ?) view. I do not have much idea on how to code it. I have tried many things from google. I thought I could start from simple image gridview and modify it to my needs but it did not work.</p>
  
  <p>Please provide some directions.</p>
  
  <p><img src="https://i.stack.imgur.com/xFccU.jpg" alt="enter image description here"></p>
  
  <p>create_team_new.xml(layout where gridview resides)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout
      android:id="@+id/parentcreateteam"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      xmlns:android="http://schemas.android.com/apk/res/android"
       android:background="@drawable/background"&gt;
  
  &lt;TextView
      android:id="@+id/citytextview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/city"
      android:layout_alignTop="@+id/teamcity"
      android:layout_alignParentLeft="true" /&gt;
  
  &lt;AutoCompleteTextView
      android:id="@+id/teamname"
      android:layout_width="244dp"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/teamcity"
      android:layout_alignRight="@+id/gridviewplayer"
      android:text="@string/teamname"
      android:textSize="18sp" &gt;
  &lt;requestFocus /&gt;
  &lt;/AutoCompleteTextView&gt;
  &lt;Button
      android:id="@+id/createteam"
      android:layout_width="140dp"
      android:layout_height="wrap_content"
      android:text="Create Team"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
       android:background="@drawable/red_button"
            style="@+style/button_text" /&gt;
  
  &lt;TextView
      android:id="@+id/teamnametextview"
      android:layout_width="wrap_content"
      android:layout_height="22dp"
      android:layout_alignBaseline="@+id/teamname"
      android:layout_alignBottom="@+id/teamname"
      android:layout_alignParentLeft="true"
      android:text="@string/teamname" /&gt;
  
  &lt;AutoCompleteTextView
      android:id="@+id/teamcity"
      android:layout_width="244dp"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/cricket"
      android:layout_alignRight="@+id/gridviewplayer"
      android:layout_below="@+id/teamname"
      android:layout_marginTop="22dp"
      android:ems="10"
      android:text="@string/city"
      android:textSize="18sp" &gt;
  
  
  &lt;/AutoCompleteTextView&gt;
  
  &lt;TextView
      android:id="@+id/radiotextview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/teamcity"
      android:layout_marginTop="15dp"
      android:text="Pick a Sport" /&gt;
  
  
  &lt;GridView
      android:id="@+id/gridviewplayer"
      android:layout_width="match_parent"
      android:layout_height="170dp"
      android:layout_above="@+id/createteam"
      android:layout_below="@+id/pick_sport"
      android:layout_centerHorizontal="true"
      android:background="@drawable/gv_bkg"
   android:padding="5dp"
      android:numColumns="4" &gt;
  
  &lt;/GridView&gt;
  
  
  
  &lt;RadioGroup
      android:id="@+id/pick_sport"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
  
      android:layout_alignRight="@+id/createteam"
      android:layout_below="@+id/radiotextview"
      android:orientation="horizontal" &gt;
  
      &lt;RadioButton
          android:id="@+id/cricket"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Cricket" /&gt;
  
      &lt;RadioButton
          android:id="@+id/soccer"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Soccer" /&gt;
  &lt;/RadioGroup&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>gv_createplayer.xml(view to be inflated inside gridview)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      &gt;
  
  &lt;Button  android:id="@+id/btn_player"
      android:layout_width="match_parent"
  
      android:layout_height="wrap_content"
  
      android:text= "+"
  
      android:background="@drawable/red_button"&gt;
  
  &lt;/Button&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>PlayerAdapter.java(gridview Adapter)</p>
  
  <pre><code>import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.Button;
  import android.widget.GridView;
  import android.widget.ImageView;
  
  public class PlayerAdapter extends BaseAdapter {
  
      private Context mContext;
      LayoutInflater inflater;
  
      // Keep all Images in array
      public Integer[] mThumbIds = {};
          /*  R.drawable.gridview_createteam, R.drawable.pic2,
              R.drawable.pic3, R.drawable.pic4,
              R.drawable.pic5, R.drawable.pic6,
              R.drawable.pic7, R.drawable.pic8,
              R.drawable.pic9, R.drawable.pic10,
              R.drawable.pic11, R.drawable.pic12,
              R.drawable.pic13, R.drawable.pic14,
              R.drawable.pic15
      };
  */
      // Constructor
      public PlayerAdapter(Context c){
          mContext = c;
      }
  
      @Override
      public int getCount() {
          return mThumbIds.length;
      }
  
      @Override
      public Object getItem(int position) {
          return mThumbIds[position];
      }
  
      @Override
      public long getItemId(int position) {
          return 0;
      }
  
      @SuppressWarnings("static-access")
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View view = new View(mContext);
          view.inflate(mContext, R.layout.gv_createplayer,null);
  
          return view;
  
  
      }
  
  
  }
  </code></pre>