Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9163641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:29:40+00:00 2026-06-17T14:29:40+00:00

I try to make an app with multitouching and I use this code: Multitouchng

  • 0

I try to make an app with multitouching and I use this code:

Multitouchng code

It works, but I’ve got problem, because in my app I’ve got HorizontalScrollView (HSV) and when I press buttons from begining of HSV I’ve got buttons Id (View.getId()) in “downTouchedViewsIndex”, but when I scroll my HSV and press any button I have only layout’s Id – nothing about buttons.

My touch code:

package com.lacrima.pianoo;

public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener, OnTouchListener {

    static private String TAG = "MainActivity";

    public View parent;

    private final ArrayList[] recentTouchedViewsIndex = new ArrayList[10];

    private final ArrayList[] downTouchedViewsIndex = new ArrayList[10];

    private final ArrayList<View> moveOutsideEnabledViews = new ArrayList<View>();

    private List<Integer> list = new ArrayList<Integer>();

    private final int mTouchSlop = 24;

    AssetFileDescriptor des; // deskryptor do opisu wczytywanego pliku muzycznego (miejsce gdzie jesta zapisany dlugość i inne takie)
    MediaPlayer[] mp = new MediaPlayer[36];// = new MediaPlayer();

    //Pierwsza oktawa
    Button button1, button2, button3, button4, button5, button6, button7;
    Button button1_5, button2_5, button4_5, button5_5, button6_5;
    //Druga oktawa
    Button button8, button9, button10, button11, button12, button13, button14;
    Button button8_5, button9_5, button11_5, button12_5, button13_5; 
    //Trzecia oktawa
    Button button15, button16, button17, button18, button19, button20, button21; 
    Button button15_5, button16_5, button18_5, button19_5, button20_5;

    SamplePlayer sound;
    Integer[] soundIDs = new Integer[36];

    private boolean[] whitePlays = new boolean[21];
    private boolean[] blackPlays = new boolean[15];

    private View[] whiteViews = new View[21];
    private View[] blackViews = new View[15];

    private int lastButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //Ustawienie ekranu horuzontalnie na sztywno
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

        parent = findViewById(android.R.id.content).getRootView();
        parent.setOnTouchListener(this);


        SeekBar seek = (SeekBar) findViewById(R.id.seekBar);
        final LockedHorizontalScrollView hsv = (LockedHorizontalScrollView)findViewById(R.id.hsv);

        seek.setMax(948);
        seek.setProgress(474);
        seek.setOnSeekBarChangeListener(this);
        hsv.post(new Runnable() {
            @Override
            public void run() {
                hsv.scrollTo(474, 0);
            }
        });

        sound = new SamplePlayer(this);

        falsePlays();
        buttonListeners();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {

        LockedHorizontalScrollView hsv = (LockedHorizontalScrollView)findViewById(R.id.hsv);
        hsv.scrollTo(progress, 0);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        //uruchamiane w momencie kliknięcia na suwak
        Log.d(TAG, "Tracking on");
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        //uruchamiane w momencie odkliknięcia suwaka
        Log.d(TAG, "Tracking off");

    }


    @Override
    protected void onDestroy() {
        sound.release();
        super.onDestroy();
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {


        // index of the pointer which starts this Event
        final int actionPointerIndex = event.getActionIndex();

        // resolve the action as a basic type (up, down or move)
        int actionResolved = event.getAction() & MotionEvent.ACTION_MASK;
        if (actionResolved < 7 && actionResolved > 4) {
            actionResolved = actionResolved - 5;
        }

        if (actionResolved == MotionEvent.ACTION_DOWN || actionResolved == MotionEvent.ACTION_UP
                || actionResolved == MotionEvent.ACTION_CANCEL) {
            dealEvent(actionPointerIndex, event, v, actionResolved);
        }

        return true;
    }


    private void dealEvent(final int actionPointerIndex, final MotionEvent event, final View eventView,
            final int actionResolved) {

        int rawX, rawY;
        final int location[] = { 0, 0 };
        eventView.getLocationOnScreen(location);
        // Log.v("tag", location + "");
        rawX = (int) event.getX(actionPointerIndex) + location[0];
        rawY = (int) event.getY(actionPointerIndex) + location[1];

        final int actionPointerID = event.getPointerId(actionPointerIndex);
        ArrayList<View> hoverViews = getTouchedViews(rawX, rawY);

        if (actionResolved == MotionEvent.ACTION_DOWN) {
            downTouchedViewsIndex[actionPointerID] = (ArrayList<View>) hoverViews.clone();

            lastButton = hoverViews.get(k-1).getId();

        }

        if(actionResolved == MotionEvent.ACTION_UP){
            int k = hoverViews.size();
            Log.d("klop", "Odklijniecie: " + hoverViews.get(k-1).getId());
            lastButton = eventView.getId();
        }
        // deletes all views which where not clicked on ActionDown
        if (downTouchedViewsIndex[actionPointerID] != null) {
            final ArrayList<View> tempViews = (ArrayList<View>) hoverViews.clone();
            tempViews.removeAll(downTouchedViewsIndex[actionPointerID]);
            hoverViews.removeAll(tempViews);
        }

        if (recentTouchedViewsIndex[actionPointerID] != null) {
            final ArrayList<View> recentTouchedViews = recentTouchedViewsIndex[actionPointerID];

            final ArrayList<View> shouldTouchViews = (ArrayList<View>) hoverViews.clone();
            if (!shouldTouchViews.containsAll(recentTouchedViews)) {
                shouldTouchViews.removeAll(recentTouchedViews);
                shouldTouchViews.addAll(recentTouchedViews);

                final ArrayList<View> outsideTouchedViews = (ArrayList<View>) shouldTouchViews.clone();
                outsideTouchedViews.removeAll(hoverViews);
            }

            recentTouchedViewsIndex[actionPointerID] = hoverViews;
            hoverViews = shouldTouchViews;
        } else {
            recentTouchedViewsIndex[actionPointerID] = hoverViews;
        }

        if (actionResolved == MotionEvent.ACTION_UP) {
            recentTouchedViewsIndex[actionPointerID] = null;
            downTouchedViewsIndex[actionPointerID] = null;
        }
        for (final View view : hoverViews) {
            int x, y;
            view.getLocationOnScreen(location);
            x = rawX - location[0];
            y = rawY - location[1];

            // View does not recognize that the Pointer is
            // outside if the Pointer is not far away (>mTouchSlop)
            if (recentTouchedViewsIndex[actionPointerID] != null) {
                if (pointInView(x, y, mTouchSlop, view.getWidth(), view.getHeight())) {


                    if (!recentTouchedViewsIndex[actionPointerID].contains(view)) {
                        recentTouchedViewsIndex[actionPointerID].add(view);
                    }
                } else if (moveOutsideEnabledViews.contains(view)) {
                    Log.v("tag", "outside but gets event");
                    recentTouchedViewsIndex[actionPointerID].add(view);
                }
            }
            final MotionEvent me = MotionEvent.obtain(event.getDownTime(), event.getEventTime(), actionResolved, x, y,
                    event.getPressure(actionPointerIndex), event.getPressure(actionPointerIndex), event.getMetaState(),
                    event.getXPrecision(), event.getYPrecision(), event.getDeviceId(), event.getEdgeFlags());
            me.setLocation(x, y);

            if (!me.equals(event)) {
                // deals the Event
                view.onTouchEvent(me);
            }

            // debug
            if (actionResolved == MotionEvent.ACTION_MOVE) {
                Log.v("tag", "#" + actionPointerIndex + " Rawx:" + rawX + " rawy:" + rawY + " x:" + x + " y:" + y + " "
                        + view.toString());
            }
        }
    }

    private ArrayList<View> getTouchedViews(final int x, final int y) {

        final ArrayList<View> touchedViews = new ArrayList<View>();
        final ArrayList<View> possibleViews = new ArrayList<View>();

        if (parent instanceof ViewGroup) {
            possibleViews.add(parent);
            for (int i = 0; i < possibleViews.size(); i++) {
                final View view = possibleViews.get(i);

                final int location[] = { 0, 0 };
                view.getLocationOnScreen(location);

                if (((view.getHeight() + location[1] >= y) & (view.getWidth() + location[0] >= x) & (view.getLeft() <= x) & (view
                        .getTop() <= y)) || view instanceof FrameLayout) {
                    touchedViews.add(view);
                    possibleViews.addAll(getChildViews(view));
                }

            }
        }

        return touchedViews;

    }

    private ArrayList<View> getChildViews(final View view) {
        final ArrayList<View> views = new ArrayList<View>();
        if (view instanceof ViewGroup) {
            final ViewGroup v = ((ViewGroup) view);
            if (v.getChildCount() > 0) {
                for (int i = 0; i < v.getChildCount(); i++) {
                    views.add(v.getChildAt(i));
                }

            }
        }
        return views;
    }


    private boolean pointInView(final float localX, final float localY, final float slop, final float width,
            final float height) {
        return localX >= -slop && localY >= -slop && localX < ((width) + slop) && localY < ((height) + slop);
    }

    public void addMoveOutsideEnabledViews(final View view) {
        moveOutsideEnabledViews.add(view);
    }

And xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#0A0A0A"
    android:orientation="vertical" >

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="fill_parent"
        android:layout_height="53dp" />

    <com.lacrima.pianoo.LockedHorizontalScrollView
        android:id="@+id/hsv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <Button...

</RelativeLayout>
    </com.lacrima.pianoo.LockedHorizontalScrollView>

</LinearLayout>

My code is quite long so I puttet it on dropbox (whole project) and you can see what happend

Project

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:29:41+00:00Added an answer on June 17, 2026 at 2:29 pm

    I resolved my problem.

    You don’t need looking for button under touch. You have to take X and Y of your touch position and add your values from scrollView. Then you have to check, there are any button and start method:

    rawX = (int) event.getX(actionPointerIndex) + location[0];
    rawY = (int) event.getY(actionPointerIndex) + location[1];
    
        scrollX = seek.getProgress() + rawX;
    
        if (actionResolved == MotionEvent.ACTION_DOWN) {
            findButton(scrollX, rawY, true);
        }
    
        if(actionResolved == MotionEvent.ACTION_UP || actionResolved == MotionEvent.ACTION_CANCEL) {
            findButton(scrollX, rawY, false);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to make a refund transaction via my django app using code: class
I have a problem with the simulator, I try to make the app with
i try make spinner and button with clickHandler this is my nyoba.java import android.app.Activity;
I try to make simple hello world app for facebook. But when i try
I try to make in app purchase on my app. But I have not
I'm fairly new to C# but I will try to make this quick! ;)
I'm new to Web programming! I try to make an app (something like Client
I try to make an animation with an image in my view but it
I try to make Ext.Data.Store working... With a normal proxy it works great: var
I am trying to make APP native code for Android. The Native code is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.