Search code examples
androidlistviewnine-patch

Gap between the border and the nine patch background of a list element (LinearLayout)


I use a ListView with different "styled" component. The first and the second component from the picture use a programmatically drawn background. The third and the fourth element has a nine-patch background. When I click on a element the selector from the ListView colorize and covers the selected element.

My problem is know that I have a gap between border and nine-patch background. Give it any solution to eliminate this gap?

Screenshot with selected second list element (programmatically background)

Screenshot with selected fourth list element (nine-patch background)


Solution

  • I have found for me a solution. I remove all selector states from the ListView (setSelector(new StateListDrawable())) and add a OnTouchListener to the ListView.

    The OnTouchListener trigger to different methods to set or delete a ColorFilter on the drawn background from the view element. The ColorFilter places only over the painted (nine-patch or programmatically background) area and not over the gap.

    public class ClickAndReleaseListener implements View.OnTouchListener
    {
        /**
         * Activity with integrated Adapter (from ListView) to do the triggered methods
         */
        public IClickAndReleaseListener releaseListener;
    
        /**
         * Call from Activity (with the ListView) ex. listView.setOnTouchListener(new ClickAndReleaseListener(Activity.this))
         * @param releaseListener => Activity with implemented Interface
         */
        public ClickAndReleaseListener(IClickAndReleaseListener releaseListener)
        {
            this.releaseListener = releaseListener;
        }
    
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //Aenderung der Visualisierung für das angeklickte Element.
            switch(event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    //method find the triggered element and set a ColorFilter at the drawable background
                    //ex. visibleElement.getBackground().setColorFilter(ColorSpace.getHighlightColorFilter)
                    releaseListener.setColorFilter(event);
                    break;
                case MotionEvent.ACTION_UP:
                    //method iterate to all elements and set a null ColorFilter at the drawable background
                    releaseListener.releaseColorFilter();
                    break;
            }
    
            return false;
        }
    
    }