1

in my app i use a GridView with several random placed ImageViews. User should select an image. To mark the selected one i use setBackgroundColor() for the view. My problem is, if user want now to select another image, the background from previous selected image (or all images in the grid) must be reset (or set to white). Any idea how to do this?

Or, is there another possibility to implement it, so that selected image in a grid will be highlighted?

EDIT: Thank you for all hints in comments. Solution below.

CoUnt3r
  • 119
  • 1
  • 2
  • 8
  • see http://stackoverflow.com/questions/4185930/how-to-highlight-imageview-when-focused-or-clicked or http://stackoverflow.com/questions/9676689/change-attributes-of-view-on-list-item-selection – Rachel Gallen Jan 26 '13 at 17:14
  • Do you want to reload the grid view?? – Swati Jan 26 '13 at 17:20
  • @Swati, my images are random placed in the GridView. If i reload the GridView, the position of the images will change, so the gridView cannot be reloaded... – CoUnt3r Jan 26 '13 at 17:28
  • @Rachel Gallen: i know that thanks, but how to use a selector in a GridView? – CoUnt3r Jan 26 '13 at 17:31
  • did you look at the second link? – Rachel Gallen Jan 26 '13 at 17:32
  • or look at this http://stackoverflow.com/questions/3159897/android-gridview-disable-highlighting you may have to edit the answer to your needs – Rachel Gallen Jan 26 '13 at 17:34
  • or add an-on touch listener http://stackoverflow.com/questions/3335211/android-gridview-and-ontouchlistener again you will have to modify the code but it'll point you in the right direction – Rachel Gallen Jan 26 '13 at 17:37
  • @Rachel Gallen thank you for interesting links! I'll read this later over again. For now I solved this now with a dirty hack: v.setBackgroundColor(Color.CYAN); if (previewView != null) { previewView.setBackgroundColor(Color.WHITE); } previewView = v;} Question: how to do with my main thread question. How to set it solved for the moment? – CoUnt3r Jan 26 '13 at 18:05
  • good for you! happy for you :) +1 – Rachel Gallen Jan 26 '13 at 18:06
  • Ehm... if i try +1, i get "You can't vote for your own post." (sorry, i'm new here) – CoUnt3r Jan 26 '13 at 18:16

2 Answers2

1

Again Set an Adapter

 @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);

        Context context = this;

        gridView = (GridView) findViewById(R.id.grid_view);

        gridView.setAdapter(new ImageAdapter(this));

        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View v,
                    int position, long id) {
            gridView.setAdapter(new ImageAdapter(context));
            v.setBackgroundColor(Color.CYAN);
            }
        });
    }
Praveen
  • 62
  • 1
  • 6
  • @Praven: thanks, but this doesn't work and i cannot reload the GridView, because my images are placed random, so after a reload all images are changed... – CoUnt3r Jan 26 '13 at 17:34
0

OK, a possible working solution (one of the links from Rachel Gallen) i checked is to use

gridView.getChildAt(previous).setBackgroundColor(Color.WHITE); 

Another solution i used is a dirty hack with hold the previous view in a static variable:

...
View previousView = null;
...
 @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);

        gridView = (GridView) findViewById(R.id.grid_view);

        gridView.setAdapter(new ImageAdapter(this));

        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View v,
                    int position, long id) {

            if (previewView != null) {
                previewView.setBackgroundColor(BACKGROUNDCOLOR);
            }
            previewView = v;
            v.setBackgroundColor(HIGHLIGHTCOLOR);
            }
        });
    }
CoUnt3r
  • 119
  • 1
  • 2
  • 8