Can't use button click with gestures please help

0 Chintan Dholakiya · May 26, 2015
I don't know when ever i try to integrating a button with click functionality with gestures it stops working

package com.example.chintan.gesturevesture;

import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;
import android.widget.Button;
import android.view.View;

public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {

private TextView cMsg;
private GestureDetectorCompat gestureDetector;
public Button csButton;

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

csButton.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
cMsg.setText("Thats Click");
}
}
);

cMsg = (TextView) findViewById(R.id.cMsg);
this.gestureDetector = new GestureDetectorCompat (this,this);
gestureDetector.setOnDoubleTapListener(this);

}


@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
cMsg.setText("onSingleTapConfirmed");
return true;
}

@Override
public boolean onDoubleTap(MotionEvent e) {
cMsg.setText("onDoubleTap");
return true;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
cMsg.setText("onDoubleTapEvent");
return true;
}

@Override
public boolean onDown(MotionEvent e) {
cMsg.setText("onDown");
return true;
}

@Override
public void onShowPress(MotionEvent e) {
cMsg.setText("onShowPress");

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
cMsg.setText("onSingleTapUp");
return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
cMsg.setText("onScroll");
return true;
}

@Override
public void onLongPress(MotionEvent e) {
cMsg.setText("onLongPress");

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
cMsg.setText("onFling");
return true;
}

//////////////end of gestures//////////////////////

@Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

Help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Post a Reply

Replies

Oldest  Newest  Rating
+1 Janki Patel · May 28, 2015

package com.janki.myinteractiveproject;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.support.v4.view.GestureDetectorCompat;


public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener,
        GestureDetector.OnDoubleTapListener{


    private TextView jtext;
    private GestureDetectorCompat gd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        jtext = (TextView)findViewById(R.id.jtext);
        Button jbutton = (Button)findViewById(R.id.jbutton);
        this.gd = new GestureDetectorCompat(this,this);
        gd.setOnDoubleTapListener(this);
        jbutton.setOnClickListener(
                new Button.OnClickListener(){
                    public void onClick(View v){
                        jtext.setText("You just tapped on a button");

                }
            }
        );
        jbutton.setOnLongClickListener(
                new Button.OnLongClickListener(){
                public boolean onLongClick(View v){
                    jtext.setText("You longclicked haha");
                    return true;
            }
        }
        );
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        jtext.setText("You onSingleTapConfirmed haha");
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        jtext.setText("You onDoubleTap haha");
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        jtext.setText("You onDoubleTapEvent haha");
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        jtext.setText("You onDown haha");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        jtext.setText("You onShowPress haha");

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        jtext.setText("You onSingleTapUp haha");
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        jtext.setText("You onScroll haha");
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        jtext.setText("You onLongPress haha");
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        jtext.setText("You onFling haha");
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.gd.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
0 Chintan Dholakiya · May 31, 2015
Please, can u give me an explanation too
0 Chintan Dholakiya · May 31, 2015
Hey checked Janki, your code didn't worked,
+1 Janki Patel · June 1, 2015
well, It worked for me.
Did you replace all mine widget's id. Mine have different id than yours.
+1 Janki Patel · June 1, 2015
well, It worked for me.

Did you replace all mine widget's id. Mine have different id than yours.
0 Chintan Dholakiya · June 3, 2015
Yes of course I replaced all of them
0 Chintan Dholakiya · June 4, 2015
the code u sent is already tried before, but as it didn't worked i made some changes by introducing button before onCreate
  • 1

Java / Android Development

107,006 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator