title: In Android, how to update GridView cell element control when selected?
body: <p>I am trying to implement a GridView in Android to display a list of Products as mentioned in below image:
  <a href="https://i.stack.imgur.com/BFwZo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BFwZo.png" alt="enter image description here"></a></p>
  
  <p>With Custom Button and Grid List I am implemented this.</p>
  
  <p>I want to know, how can I make this Product Button Red when I select it. Or in other words, I want to get the selected cell item object and change the background color to red, TextViews text color to white. Plus, at the same time, I want to make all remaining cell items to default white background and text color to purple.</p>
  
  <p>I am new to android, any help would be great support. Thanks in advance. Here is my code:</p>
  
  <p>GridView in Fragment</p>
  
  <pre><code>&lt;GridView
  android:id="@+id/grid_Products"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_margin="5dp"
  android:horizontalSpacing="10dp"
  android:gravity="center"
  android:numColumns="3"
  android:stretchMode="columnWidth"
  android:verticalSpacing="10dp /&gt;
  </code></pre>
  
  <p>In ProductFragment class in onCreateView() method, I am binding the productModels to gridView</p>
  
  <pre><code>List&lt;ProductModel&gt; productModels;
  GridView gdGridView=(GridView)(view.findViewById(R.id.grid_Products));
  adapter = new ProductButtonAdaptor(view.getContext(), productModels);
  gdGridView.setAdapter(adapter);
  </code></pre>
  
  <p>product_button.xml</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@drawable/btn_product_red"
      android:id="@+id/pnl_ProudctButton"
      android:orientation="vertical"&gt;
  
      &lt;LinearLayout
          android:paddingTop="10dp"
          android:layout_width="wrap_content"
          android:layout_height="0dp"
          android:layout_weight="5"
          android:gravity="center"
          android:layout_gravity="center"
          android:orientation="horizontal"&gt;
          &lt;TextView
              android:id="@+id/lbl_ProductName"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:gravity="bottom"
              android:text="5"
              android:layout_marginRight="2dp"
              android:textColor="@color/purple"
              android:textAlignment="center"
              android:textSize="30dp"
              android:textStyle="bold" /&gt;
          &lt;TextView
              android:id="@+id/lbl_ProductCurrency"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_marginLeft="2dp"
              android:gravity="bottom"
              android:text="QAR"
              android:textAlignment="center"
              android:textColor="@color/purple"
              android:textSize="20dp"
              android:textStyle="bold" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;View
          android:id="@+id/lbl_ProductSeparator"
          android:layout_width="match_parent"
          android:layout_height="2dp"
          android:layout_alignParentBottom="true"
          android:layout_margin="4dp"
          android:background="@color/purple" /&gt;
  
      &lt;TextView
          android:id="@+id/lbl_ProductCategory"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="4"
          android:gravity="top"
          android:text="International"
          android:textAlignment="center"
          android:textColor="@color/purple"
          android:paddingBottom="10dp"
          android:paddingLeft="10dp"
          android:paddingRight="10dp"
          android:textSize="16dp" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>ProductButtonAdpater class:</p>
  
  <pre><code>public class ProductButtonAdaptor extends ArrayAdapter&lt;ProductModel&gt; implements AdapterView.OnItemClickListener
  {
  private Context context;
  private final List&lt;ProductModel&gt; productModels;
  private int selected = -1;
  public ProductButtonAdaptor(Context context, List&lt;ProductModel&gt; productValues)
  {
      super(context, R.layout.button_product, productValues);
      this.context = context;
      this.productModels = productValues;
  }
  
  public View getView(int position, View convertView, ViewGroup parent)
  {
  
      LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
      View gridView;
  
      if (convertView == null)
      {
  
          gridView = new View(context);
  
          // get layout from button_product.xml
          gridView = inflater.inflate(R.layout.button_product, null);
  
          // set value into textview
          TextView lbl_ProductName = (TextView)gridView.findViewById(R.id.lbl_ProductName);
          TextView lbl_ProductCurrency = (TextView)gridView.findViewById(R.id.lbl_ProductCurrency);
          TextView lbl_ProductCategory = (TextView)gridView.findViewById(R.id.lbl_ProductCategory);
          lbl_ProductName.setText(productModels.get(position).getCode());
          lbl_ProductCurrency.setText(productModels.get(position).getCurrency());
          lbl_ProductCategory.setText(productModels.get(position).getCategoryName());
  
      }
      else
      {
          gridView = (View)convertView;
      }
  
      if (selected == position)
      {
          TextView lbl_ProductName = (TextView)gridView.findViewById(R.id.lbl_ProductName);
          TextView lbl_ProductCurrency = (TextView)gridView.findViewById(R.id.lbl_ProductCurrency);
          TextView lbl_ProductCategory = (TextView)gridView.findViewById(R.id.lbl_ProductCategory);
          View lbl_ProductSeperator = (View)gridView.findViewById(R.id.lbl_ProductSeparator);
          LinearLayout pnl_ProductButton = (LinearLayout)gridView.findViewById(R.id.pnl_ProudctButton);
  
          lbl_ProductName.setTextColor(ContextCompat.getColor(context, R.color.vodafone_white));
          lbl_ProductCurrency.setTextColor(ContextCompat.getColor(context, R.color.vodafone_white));
          lbl_ProductCategory.setTextColor(ContextCompat.getColor(context, R.color.vodafone_white));
          lbl_ProductSeperator.setBackgroundColor(ContextCompat.getColor(context, R.color.vodafone_white));
          pnl_ProductButton.setBackground(ResourcesCompat.getDrawable(context.getResources(), R.drawable.btn_product_red, null));
  
      }
      else
      {
          //setup the other cells
          TextView lbl_ProductName = (TextView)gridView.findViewById(R.id.lbl_ProductName);
          TextView lbl_ProductCurrency = (TextView)gridView.findViewById(R.id.lbl_ProductCurrency);
          TextView lbl_ProductCategory = (TextView)gridView.findViewById(R.id.lbl_ProductCategory);
          View lbl_ProductSeperator = (View)gridView.findViewById(R.id.lbl_ProductSeparator);
          LinearLayout pnl_ProductButton = (LinearLayout)gridView.findViewById(R.id.pnl_ProudctButton);
  
          lbl_ProductName.setTextColor(ContextCompat.getColor(context, R.color.vodafone_purple));
          lbl_ProductCurrency.setTextColor(ContextCompat.getColor(context, R.color.vodafone_purple));
          lbl_ProductCategory.setTextColor(ContextCompat.getColor(context, R.color.vodafone_purple));
          lbl_ProductSeperator.setBackgroundColor(ContextCompat.getColor(context, R.color.vodafone_purple));
          pnl_ProductButton.setBackground(ResourcesCompat.getDrawable(context.getResources(), R.drawable.btn_product_white, null));
      }
  
      return gridView;
  }
  
  @Override
  public int getCount()
  {
      return productModels.size();
  }
  
  @Override
  public ProductModel getItem(int position)
  {
      return productModels.get(position);
  }
  
  @Override
  public long getItemId(int position)
  {
      return 0;
  }
  
  @Override
  public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id)
  {
      selected = position;
  
      this.notifyDataSetChanged();
      //adapter.notifyDataChanged();
  
      //parent.invalidate();
      //view.invalidate();
  }
  }
  </code></pre>
  
  <p>this is the updated code</p>