0

Problem Description

In my application I have GridView with 12 items in it. Every item in the GridView has its own icon like it is shown in the image below. For every icon I have two images one pressed and one normal.

GridView with different item and images

Question

I try to find some standard mechanisms to change images from Normal state to Presses state when user press on the item and then release the button, but can't find. Instead of that I use public boolean onTouch(View v, MotionEvent event) method but it brings to some side effects for example when user scroll down or slide screen to change tab, items are clicked.

Source Code

@Override
public boolean onTouch(View v, MotionEvent event) {
    /* Get Action on Touch. */
    int action = event.getActionMasked();
    if (action == MotionEvent.ACTION_DOWN) {
        float currentXPosition = event.getX();
        float currentYPosition = event.getY();
        
        lastPressedPosition = gvCategories.pointToPosition((int) currentXPosition, (int) currentYPosition);
        if(lastPressedPosition < 0 || lastPressedPosition > sDefaultArray.length)
            return false;
    
        /* Set Pressed Image. */
        Drawable drawable = getActivity().getResources().getDrawable(sPressedArray[lastPressedPosition]);
        Category category = (Category) gvCategories.getItemAtPosition(lastPressedPosition);
        category.setImage(((BitmapDrawable) drawable).getBitmap());
        CategoriesGridAdapter adapter = (CategoriesGridAdapter)gvCategories.getAdapter();
        adapter.notifyDataSetChanged();
        
    }
    else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        
        /* Get current possition to compare with the last one. */
        float currentXPosition = event.getX();
        float currentYPosition = event.getY();
        int currentPosition = gvCategories.pointToPosition((int) currentXPosition, (int) currentYPosition);
        if(currentPosition == -1 && lastPressedPosition == -1)
            return false;
        if (currentPosition == lastPressedPosition && action == MotionEvent.ACTION_UP) {
            
            Category category = (Category) gvCategories.getItemAtPosition(currentPosition);
            Log.i(TAG, String.format("Category Title is: %s", category.getTitle()));
            
            if (category == Category.More) {
                
                //tracker.trackEvent("Category Clicked", "More", "", 0L);
                
                /* Get current selected language. */
                final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
                final String lang = preferences.getString(Preferences.LANGUAGE, "en");
                /* Load All categories available in the application. */
                updateCategoriesAdapter(lang, true);
            }
            else {
                
                //tracker.trackEvent("Category Clicked", category.getTitle(), "", 0L);
                
                CategoryDetailsActivity.sFragmentManager = getFragmentManager();
                /* Start categories detail activity. */
                Intent intent = new Intent(getActivity().getApplicationContext(), CategoryDetailsActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(CategoryDetailsActivity.CATEGORIES_IDS, category.getIDs());
                intent.putExtra(CategoryDetailsActivity.CATEGORY_NAME, category.getTitle());
                getActivity().getApplicationContext().startActivity(intent);
            }
            
            
        }
        
        if(lastPressedPosition == -1)
            return false;
        
        /* Set Pressed Image. */
        Drawable drawable = getActivity().getResources().getDrawable(sDefaultArray[lastPressedPosition]);
        Category category = (Category) gvCategories.getItemAtPosition(lastPressedPosition);
        category.setImage(((BitmapDrawable) drawable).getBitmap());
        CategoriesGridAdapter adapter = (CategoriesGridAdapter)gvCategories.getAdapter();
        adapter.notifyDataSetChanged();
    }

    return false;
}
Community
  • 1
  • 1
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147
  • Did you try using a `selector` drawable as your image? – tyczj Sep 04 '14 at 19:40
  • If you have the two images already made, use a `Button` element in your `GridView` and set the `Background` element of your button to a state selector drawable. refer to this link for xml declarations on making your selector drawable. http://www.mkyong.com/android/android-imagebutton-selector-example/ – kabuto178 Sep 04 '14 at 19:41
  • @tyczj how can I use selector for multiple images, because I have one view and multiple images? Can you show an example please? – Viktor Apoyan Sep 04 '14 at 19:47
  • @kabuto178 but how to set different selectors for single view item? – Viktor Apoyan Sep 04 '14 at 19:48
  • 2
    @ViToBrothers create a different selector for each button. – kabuto178 Sep 04 '14 at 19:51
  • @kabuto178 can you please put your answer in order I can close this post, this really help me an it works, please put some code example. – Viktor Apoyan Sep 04 '14 at 20:54

1 Answers1

1

If you have the two images already made, use a Button element in your GridView and set the Background element of your button to a state selector drawable. refer to this link for XML declarations on making your selector drawable example here . For your scenario you will have to create different selector XML files for each button.

kabuto178
  • 3,129
  • 3
  • 40
  • 61