Search code examples
javaandroidandroid-layoutimageviewandroid-scrollview

How to resolve ImageView padding issue inside HorizontalScrollView on Android


enter image description here

As shown in the image, I have tabs as in browser tabs. And I have padding between tab images to overlap on each other adjacent images. I need a particular image needs to be on top of adjacent images(image below) when the user touches it and also want to do the same through code.

enter image description here

I have a onTouchListener to the whole scrollview. It works fine with the below code when the user manually touches the image.

tab[id].bringToFront();
tabScrollView.invalidate();

If I do the same through code instead of manual user touch, the selected tab image goes to the right end since it uses bringToFront() method. Is there any way how I can bring it just on top of adjacent images?

Update:

I have already tried,

tab[id].performClick();
tab[id].performLongClick();

And also tried,

tab[id].setOnTouchListener(new MyTouchListener()); //my own listener
// Obtain MotionEvent object
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
float x = 0.0f;
float y = 0.0f;
int metaState = 0;
MotionEvent motionEvent = MotionEvent.obtain(
                downTime, 
                eventTime, 
                MotionEvent.ACTION_UP, 
                x, 
                y, 
                metaState
            );

// Dispatch touch event to view
tab[id].dispatchTouchEvent(motionEvent);

Both the methods mentioned above doesn't help. Do I need to provide any more details?


Solution

  • Solved it with,

    // Obtain MotionEvent object
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis() + 100;
    float x = 0.0f;
    float y = 0.0f;
    int metaState = 0;
    MotionEvent motionEvent = MotionEvent.obtain(
                downTime, 
                eventTime, 
                MotionEvent.ACTION_UP, 
                x, 
                y, 
                metaState
            );
    
    // Dispatch touch event to the view which contains all the images
    tabScrollView.dispatchTouchEvent(motionEvent);