1

I have created a gridView of images.i am trying to change background colour of image on selection in the following code.But no change occurs on selecting an image.

gridView = (GridView)rootView.findViewById(R.id.gridView); customGridAdapter = new GridViewAdapter(getActivity(), R.layout.row_grid, getData()); gridView.setAdapter(customGridAdapter); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { //Toast.makeText(TeamFragment.this, position + "#Selected", // Toast.LENGTH_SHORT).show(); count=position; gridView.setBackgroundColor(Color.argb(125,75,236,90)); /*int currentPic = position; Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);*/ } }); 

GridViewAdapter

public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; ViewHolder holder = null; if (row == null) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new ViewHolder(); holder.imageTitle = (TextView) row.findViewById(R.id.text); holder.image = (ImageView) row.findViewById(R.id.image); row.setTag(holder); } else { holder = (ViewHolder) row.getTag(); } row.setBackgroundColor(Color.RED); ImageItem item = data.get(position); holder.imageTitle.setText(item.getTitle()); holder.image.setImageBitmap(item.getImage()); return row; } 
0

2 Answers 2

2

Inside getView(...) method in your GridViewAdapter, you can set OnClickListener for ImageView as follows,

public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView== null) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); convertView= inflater.inflate(layoutResourceId, parent, false); holder = new ViewHolder(); holder.imageTitle = (TextView) convertView.findViewById(R.id.text); holder.image = (ImageView) convertView.findViewById(R.id.image); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } convertView.setBackgroundColor(Color.RED); ImageItem item = data.get(position); holder.imageTitle.setText(item.getTitle()); holder.image.setImageBitmap(item.getImage()); final View row = convertView; holder.image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub row.setBackgroundColor(Color.yourColor); } }); return row; } 

Don't forget to declare variable row as final so it can be accessed in anonymous inner class View.OnclickListener.

Sign up to request clarification or add additional context in comments.

4 Comments

setting row as final this statement doesn't work row = inflater.inflate(layoutResourceId, parent, false); shows error "The final local variable row cannot be assigned. It must be blank and not using a compound assignment"
The value of variable declared as final must be assigned at the time of declaration.Since you are assigning the value inside if block,you got that error. You can try the edited answer.
Thanks a lott..my grid view consists of many images and the only problem is that when i scroll up and down many of the images which i have selected previously their selection colour disappears..also if i open another fragment and return back to this fragment all selection colour disappears..
@user3684678 Try this solution : stackoverflow.com/questions/19633672/…
0

It looks like you are setting background color for whole gridView in the item selection. You should call getView() method using Adpater and set the color.

3 Comments

should i call getView() method inside gridView.setOnItemClickListener()..?
if i try to add item.setBackgroundColor(Color.GREEN); inside adapter it shows error "the method setBackgroundColor(int) is undefined for the type ImageItem"
I think you are trying set border for an image if it selected. If my assumption is right, then get the selected imageView and set border color for it.