1
Button button = (Button)findViewById(R.id.button1);

button.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN ) {
            //Insert desired code here
            return true;
        }
        return false;
    }
});

public void backtogreen(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    startActivity(intent);
}

How to fix this code? I have that errors:

  • cannot resolve symbol 'setOnTouchListener' - for setOnTouchListener
  • invalid method declaration; return type required - for onTouchListener
  • Annotations are not allowed here - for @override above public boolean
  • cannot resolve symbol 'event' for bothe 'events'
  • cannot return a value from a method with void result type for return true and return false

Please help!

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
Rediner
  • 83
  • 1
  • 7

3 Answers3

6

You can't run code other than primitive assignment in the class body. Your code needs to be in some method, like onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.button1);
    button.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN ) {
                //Insert desired code here
                return true;
            }
            return false;
        }
    });
}
tachyonflux
  • 20,103
  • 7
  • 48
  • 67
  • My main target is to make button perform on a touch like it was clicked. Now I struggle with errors under my code :( – Rediner Oct 03 '16 at 16:53
0

Your method signature is incorrect. Try this code:

button.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN ) {
                //Insert desired code here
                return true;
            }
            return false;
        }
    });
Shaishav
  • 5,282
  • 2
  • 22
  • 41
  • This was an asignment, I would like to know how to deal with these errors. What should I code to e.g. make "View v" correct? – Rediner Oct 03 '16 at 16:48
  • @Rediner The other answer should have fixed all the issues in the code. If you want click behavior, then use `button.setOnClickListener()`. – Shaishav Oct 03 '16 at 16:54
  • Do you know if it is possible to 'swipe' over some buttons with onTouchlistener and make them perform in a row? – Rediner Oct 03 '16 at 16:57
  • @Rediner Not entirely sure what you mean by -- *"perform in a row?"*. But, you can check [this question](http://stackoverflow.com/questions/20606802/how-to-swipe-a-button-ontouch-event-in-android) – Shaishav Oct 03 '16 at 17:00
  • I mean you would not need to release finger to trigger another button. I want a row of buttons to perform the same action, you will need to keep swiping over them to keep them doing action. – Rediner Oct 03 '16 at 17:02
  • @Rediner Still not entirely clear but, I think this concerns the modularity of your code. In this case, you can define what would happen when the button click happen in a method and then call that method from your button with swipe action. – Shaishav Oct 03 '16 at 17:09
  • It cannot be that complicated. I would like button to perform click on a touch and that is all. Do i need to only set ontouchlistener and it will do its job? – Rediner Oct 03 '16 at 17:15
  • @Rediner See the reason why your query isn't quite clear to me is because you are talking about both click and swipe action on the button. If click is what you want just use `onClickListener`. To call click on other buttons you can use `buttonInstance.performClick()` or you can use the define the actions to be performed on lick in a method and call that method. – Shaishav Oct 03 '16 at 17:20
0

Use the following method

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 Button button = (Button) findViewById(R.id.button1);



        OnTouchListener m_onTouchListener = new OnTouchListener() {
       @Override

        public boolean onTouch(View p_v, MotionEvent p_event) {

           if (event.getAction() == MotionEvent.ACTION_DOWN ) {
            //Insert desired code here
            return true;
        }
        return false;
        }
    };

    button.setOnTouchListener(m_onTouchListener);

}
jalu
  • 1
  • 1