I want to use Gesture Listener so I wrote a simple code to test it out but it is not working. I watched a YouTube tutorial and copied it. I did not test this but it is something very similar to what I am trying to accomplish. This is my first time using Gesture. Here is what I am:
MyClassActivity.java
public class MyClassActivity extends Activity implments GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{
private GestureDetector gestureDetector;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.gestureDetector = new GestureDetector(this, this);
gestureDetector.setOnDoubleTapListener(this);
textView = (TextView) findViewById(R.id.textView);
} //end of Oncreate
#Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public boolean onDown(MotionEvent e) {
textView.setText("onDown");
return false;
}
#Override
public void onShowPress(MotionEvent e) {
textView.setText("onShowPress");
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
textView.setText("onSingleTap");
return false;
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
textView.setText("onScroll");
return false;
}
#Override
public void onLongPress(MotionEvent e) {
textView.setText("onLongPress");
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
Try this at the end of your onCreate method.
View v = //Get your view
v.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e){
return gestureDetector.onTouchEvent(e);
}
});
Can you try with
#Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return true; // paas it true , as you are handling it.
}
Related
I want to apply a doubleClick event on a text field which displays some text on double click event but it keeps giving me errors is there any method to just simply doing this
TextView tv;
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.mytext);
tv.setOnTouchListener(new OnDoubleTapListener() {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return true;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
tv.setText("DoubleTouch");
return true;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
tv.setText("Double Touch ");
return true;
}
}
);
}
You should initialize the Gesture Detector like this
GestureDetector gd = new GestureDetector(this, new GestureDetector.OnGestureListener() {
#Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
#Override
public void onShowPress(MotionEvent motionEvent) {
}
#Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
#Override
public void onLongPress(MotionEvent motionEvent) {
}
#Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
});
Then set on double click listener like this
gd.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
#Override
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onDoubleTap(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent motionEvent) {
return false;
}
});
Finally you apply the listener to your View like this
tv.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
gd.onTouchEvent(event);
return false;
}
});
Refer Android docs for more info Detect Common gestures
I want to insert an alert dialog into my android app.
It's fine when I follow the tutorial to add it in a simple project, which only has a MainActivity.java, activity_main.xml and a layout xml.
However, when I want to insert the code into my project, I get some questions.
My code is:
android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(this)
.setTitle("Exit?")
.setMessage("Are you sure want to QUIT ?")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
pmaControllerManager.OnExit();
}
})
.setNegativeButton("no", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int i) {
dialog.cancel();
}
})
.setNeutralButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.show();
There is a warning symbol in the this in the first line.
How could I fix it?
p.s. below is the function where I call showDialog(I call it in buttons[5]):
private void setbutton()
{
buttons[0]=(ImageButton)menu.findViewById(R.id.button0);
buttons[1]=(ImageButton)menu.findViewById(R.id.button1);
buttons[2]=(ImageButton)menu.findViewById(R.id.button2);
buttons[3]=(ImageButton)menu.findViewById(R.id.button3);
buttons[4]=(ImageButton)menu.findViewById(R.id.button4);
buttons[5]=(ImageButton)menu.findViewById(R.id.button5);
buttons[1].setBackgroundResource(R.drawable.screen);
buttons[2].setBackgroundResource(R.drawable.sketch);
buttons[3].setBackgroundResource(R.drawable.importdraw);
buttons[4].setBackgroundResource(R.drawable.setting);
buttons[5].setBackgroundResource(R.drawable.out);
center.setBackgroundResource(R.drawable.noticeicon_using);
/*center.setOnClickListener(new ImageButton.OnClickListener(){
#Override
public void onClick(View v) {
if(click)
{
closemenu();
//mode=false;
}
else
{
openmenu();
//mode=true;
}
click=!click;
}
});*/
center.setOnTouchListener(new View.OnTouchListener() {
boolean isMove=false;
int touchy,touchx,y;
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action=motionEvent.getAction();
if(action==MotionEvent.ACTION_DOWN)
{
y=params.y;
//Log.i("y:",String.valueOf(y));
touchy=(int)motionEvent.getRawY();
touchx=(int)motionEvent.getRawX();
}
else if(action==MotionEvent.ACTION_MOVE)
{
if(Math.abs(motionEvent.getRawX()-touchx)<30)
{
isMove=true;
params.gravity=Gravity.RIGHT;
params.y=y+(int)motionEvent.getRawY()-touchy;
wm.updateViewLayout(center,params);
wm.updateViewLayout(menu,params);
}
}
else if(action==MotionEvent.ACTION_UP)
{
if(!isMove)
{
if(click)
{
closemenu();
//mode=false;
}
else
{
openmenu();
//mode=true;
}
click=!click;
}
else
isMove=false;
}
return true;
}
});
buttons[1].setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN)
buttons[1].setBackgroundResource(R.drawable.screen_focus);
else if(event.getAction()==MotionEvent.ACTION_UP)
{
buttons[1].setBackgroundResource(R.drawable.screen);
pmaControllerManager.newTask("screen");
}
return true;
}
});
buttons[2].setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN)
buttons[2].setBackgroundResource(R.drawable.sketch_focus);
else if(event.getAction()==MotionEvent.ACTION_UP)
{
buttons[2].setBackgroundResource(R.drawable.sketch);
pmaControllerManager.newTask("sketch");
}
return true;
}
});
buttons[3].setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN)
buttons[3].setBackgroundResource(R.drawable.importdraw_focus);
else if(event.getAction()==MotionEvent.ACTION_UP)
{
buttons[3].setBackgroundResource(R.drawable.importdraw);
}
return true;
}
});
buttons[4].setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN)
buttons[4].setBackgroundResource(R.drawable.setting_focus);
else if(event.getAction()==MotionEvent.ACTION_UP)
{
buttons[4].setBackgroundResource(R.drawable.setting);
pmaControllerManager.Setting();
}
return true;
}
});
buttons[5].setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN)
buttons[5].setBackgroundResource(R.drawable.exit_focus);
else if(event.getAction()==MotionEvent.ACTION_UP)
{
buttons[5].setBackgroundResource(R.drawable.out);
shoaDialog(menuwindow.this);
pmaControllerManager.OnExit();
}
return true;
}
});
}
Pass Context instead of this because this is a reference to the current object. It means if you call it for example from lambda this will be a reference of object which is extended from anonymous class.
so you can make method like
public void showDialog(Context context) {
AlertDialog alartDialog = AlertDialog.Builder(context).
...
.show();
}
and then call it like
showDialog(MainActivity.this); //for activity
showDialog(MainFragment.this.getActivity()); //for fragment
By the way better to make something like Util class for it:
class DialogUtil {
public static void showDialog(Context context) {
AlertDialog alartDialog = AlertDialog.Builder(context).
...
.show();
}
}
Then call it like
DialogUtil.showDialog(MainActivity.this); //for activity
DialogUtil.showDialog(MainFragment.this.getActivity()); //for fragment
UPDATE
Instead of calling it like:
shoaDialog(menuwindow.this);
You can pass Context as I described above or you can get it from View:
buttons[5].setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//some code here
showDialog(v.getContext());
return true;
}
});
replace this to MainActivity.this like :
android.app.AlertDialog alartDialog = new
android.app.AlertDialog.Builder(MainActivity.this)
The AltertDialog requires a context and you are passing it the activity. Try using
new android.app.AlertDialog.Builder(this.getApplicationContext())
instead.
Replace this with MainActivity.this or getApplicationContext()
android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(MainActivity.this)
If you are using dialog look below :
For Activity : ActivityName.this
android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(MainActivity.this)
For Fragment : getActivity()
android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(getActivity())
For Adapter : Context context;
android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(context)
Also, Look into this Difference between getContext() , getApplicationContext() , getBaseContext() and "this"
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
public class MainActivity extends Activity{
TextView datumText;
GestureDetector gestureDetector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Gestures
datumText = (TextView) findViewById(R.id.datumText);
datumText.setText("ma");
gestureDetector = new GestureDetector(this, new GestureListener());
datumText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
});
}
}
class GestureListener implements GestureDetector.OnGestureListener{
MainActivity mainActivity = new MainActivity();
public GestureListener(){
}
#Override
public boolean onDown(MotionEvent e) {
return false;
}
#Override
public void onShowPress(MotionEvent e) {
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
#Override
public void onLongPress(MotionEvent e) {
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
mainActivity.datumText.setText("di");
return true;
}
}
I want to change the text on
TextView datumText;
whenever I do the onFling motion on the TextView on-screen. But I am getting a NullPointerException and I think it is because I am trying to call the setText() method on a null object.
Thanks in advance!
EDIT: Forgot to mention I use a MainActivity class and a second class.
EDIT2: I know what the NullPointerException is caused by but I don't know how to solve it.
You are creating a New Instance of your MainActivity and without inflating any Layout to that, you are trying to access a TextView that don't even exist in that newly created MainActivity Instance. Just change your onFling() to this
#Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
datumText.setText("di"); // you don't need to create instance of your MainActivity to access the TextView
return true; }
dont create a new Instance of MainActivity like that:
MainActivity mainActivity = new MainActivity();
You can pass the TextView itself if you like.
class GestureListener implements GestureDetector.OnGestureListener{
...
...
TextView tv;
public void setTextView(Textview tv){
this.tv = tv;
}
...
}
and then do something like
#Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
tv.setText("di");
return true; }
For my Buttons onClickListener I have the following code:
on_enter_button.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
enter(words, linearLayout, llp, view);
}
});
Instead of pressing enter Button, I simply want to Swipe the screen in any direction and run the same method: enter(words, linearLayout, llp, view);
Currently I think my program does not recognize Swipe gestures at all. I assume it's because I placed enter(words, linearLayout, llp, view); incorrectly. Also, I put all the Gesture recognition code inside the onCreate(). Is that the correct way to program the code?
Can someone please help?
EDITED CODE:
public class Game extends Activity{
public void enter(String[] w, LinearLayout ll, LinearLayout.LayoutParams params, View v){
... //some code
... //some code
... //some code
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return myGestureDetector.onTouchEvent(event);
//error myGestureDetector not recognized since it's created in onCreate so it does not exist here
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game_activity);
SimpleOnGestureListener mySimpleGestureListener = new SimpleOnGestureListener()
{
#Override
public boolean onDoubleTap(MotionEvent e) {
return super.onDoubleTap(e);
}
#Override
//swipe does not work!
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY)
{
enter(words, linearLayout, llp, view);
return super.onFling(e1, e2, velocityX, velocityY);
}
#Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return super.onSingleTapConfirmed(e);
}
private boolean permissibleYVelocity(float velocityY)
{
if ((velocityY < -200) || (velocityY > 200))
{
return false;
}
else
{
return true;
}
}
};
final GestureDetector myGestureDetector = new GestureDetector(getApplicationContext(), mySimpleGestureListener);
//button listener works
on_enter_button.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
enter(words, linearLayout, llp, view);
}
});
}
}
You need to override View.onTouchEvent().
#Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
Call your method in SimpleOnGestureListener.onFling():
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY)
{
enter(words, linearLayout, llp, view);
return true;
}
EDIT:
Setup your GestureDectector in onCreate() as well:
mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener()
{
#Override
public boolean onDoubleTap(MotionEvent e) {
return super.onDoubleTap(e);
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY)
{
enter(words, linearLayout, llp, view);
return super.onFling(e1, e2, velocityX, velocityY);
}
#Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return super.onSingleTapConfirmed(e);
}
private boolean permissibleYVelocity(float velocityY)
{
if ((velocityY < -200) || (velocityY > 200))
{
return false;
}
else
{
return true;
}
}
});
I am not sure how to make a simple onFling simply start an animation. It doesnt matter which direction the swipes go any contact and slide across the screen should cause the animation to start. The other thing I am wondering is how to get it to let the animation run through and when the animation finishes I want it to display a picture. To give you an idea of what I am doing picture a twister spin board. The animation is the spinner spinning and after it finishes spinning it stops and points in a direction. How can I get a similar effect. (The animation is a spinning animation and the pictures are all pointing in differnt directions)
Would appreciate any help anyone can give. Thanks.
one possibility: use a gesturedetector to detect the fling.
psuedocode:
public class WhatEver extends Activity implements OnGestureListener {
private GestureDetector gestures;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestures = new GestureDetector(mContext, this);
View yourViewYouWantToHaveThemFling=(View) findViewById(blah..);
yourViewYouWantToHaveThemFling.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
if (gestures.onTouchEvent(event)) {
return true;
}
return false;
}
});
}
//all your normal stuff
#Override
public boolean onDown(MotionEvent e) {
return true; //must return true to continue
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
//do something
return false;
}
#Override
public void onLongPress(MotionEvent e) {
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}
#Override
public void onShowPress(MotionEvent e) {
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
}
as for the animation, set a listener on your animation,
yourAnimation.setAnimationListener(new DisplayPicture());
then
private final class DisplayPicture implements Animation.AnimationListener {
.
.
public void onAnimationEnd(Animation animation) {
//code to display your picture here
}
.
.
}