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

  • SEARCH
  • Home
  • 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 7507775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:33:14+00:00 2026-05-29T22:33:14+00:00

I want an action to be performed when the view is touched. However, the

  • 0

I want an action to be performed when the view is touched. However, the touches do not respond. The app doesn’t crash, it just seems to ignore it.

public class CustomDrawableView extends View implements OnTouchListener
{
    static final int width = 100;
    static final int height = 50;

    public CustomDrawableView(Context context)
    {
        super(context);
        setFocusable(true);
        setOnTouchListener(mCustomDrawableView);            
        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff74AC23);
        mDrawable.setBounds(x, y, x + width, y + height);
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
         if(event.getAction()==MotionEvent.ACTION_DOWN )
         {

        x = 400;  
        return true;
         }
         else {
             x = 300;
            return false;
        }
    }

    protected void onDraw(Canvas canvas)
    {      
        int mCanvasHeight = canvas.getHeight();
        int mCanvasWidth = canvas.getWidth();
        canvas.save();
        canvas.rotate(R,x,y);


        if (y >= mCanvasHeight-100) {
            y = 0;
        }
        RectF oval = new RectF(x, y, x + width, y
                + height); // set bounds of rectangle
        Paint p = new Paint(); // set some paint options
        p.setColor(Color.BLUE);
        canvas.drawOval(oval, p);
        canvas.restore();

        invalidate();


    }
}

I have tried a bunch of different code to fix it. None of it does anything except if I change setOnTouchListener(mCustomDrawable) to mCustomDrawableView.setOnTouchListener(this) the app crashes. There is a bunch more code in the activity that I did not put up.

  • 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-05-29T22:33:16+00:00Added an answer on May 29, 2026 at 10:33 pm

    I’m guessing you believe that the onTouch() isn’t firing because you’re not seeing your graphics change in response to changes in x or whatever. If that’s the case, it looks like you’re missing a call to invalidate() in your touch handler to cause the View to redraw itself again (via a call to onDraw()).

    Also, you have an invalidate() actually inside onDraw() itself which really shouldn’t be there. It would certainly cause your View to redraw itself over and over – I suppose – so actually, I guess you should be seeing updates because that’s there. But that isn’t the way you should make a View animate; you should instead use a Thread or Handler to schedule a regular, periodic invalidate() – or some other means to regularly schedule an update.

    Also there should be no need to implement ‘OnTouchListener’ when you can just override onTouch() as you have done. There are a couple of ways you can detect touch events for a View: (1) Override onTouch() as you have done, to get touch events on that View. (2) Register a listener using setOnTouchListener(). This latter option enables you to have a listener that listens to touch events from one View or multiple Views, and it also ‘sees’ touch events before a registered View‘s own onTouch() sees them.

    Another thing I see is that you’re setting x to 400 when you get an ACTION_DOWN event but then you’re setting it to 300 for any other kind of event such as an ACTION_MOVE. Considering it’s actually quite difficult to keep your finger still enough to never cause a string of ACTION_MOVE events immediately after an ACTION_DOWN, perhaps you’re just never seeing the graphics when x is at 400 or something.

    You really need to post a complete example (for instance your code as you’ve posted has setOnTouchListener(mCustomDrawableView) where mCustomDrawableView has not been defined anywhere) together with specific errors from Logcat. Also, you should use the debugger to see if your onTouch ever executes (stick a breakpoint in it).

    Also, you should accept some answers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view containing a button. And i want to perform an action
i just want an Action to print a barcode image, but i can´t get
I want to implement an action bar for my own android app like the
I want to perform action on change in count of rows of an HTML
I want to perform rubyf action in VIM when I press F5 if the
I want to perform a action when a button is highlighted and perform another
I want to create an application that performs a particular action when the device
I want each action taken on certain tables to be logged. I want logging
in this tutorial of Yii framework http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#action i want to put my Actions from
In my application there is a table view.What I want to do is when

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.