I am trying to use an animation for that hides and shows a toolbar by detecting swiping gestures.It works but the issue is that when the toolbar is there(visible) it will repeat if you swipe up again and again and again. How can I prevent this from happening? I've try using variable to count each step then reset at the ending but that does not work either. Helping me past this would save a lot more time I've been stuck here for 2 weeks now.
CoreActivity.java
//Swipe Events WebSwipe
WebSwipe = new Swipe(350, 700);
WebSwipe.setListener(new SwipeListener() {
#Override
public void onSwipingLeft(final MotionEvent event) {
}
#Override
public void onSwipedLeft(final MotionEvent event) {
}
#Override
public void onSwipingRight(final MotionEvent event) {
}
#Override
public void onSwipedRight(final MotionEvent event) {
}
#Override
public void onSwipingUp(final MotionEvent event) {
}
#Override
public void onSwipedUp(final MotionEvent event) {
Animation ToolbarGone = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_toolbar_gone);
m_Toolbar.startAnimation(ToolbarGone);
}
#Override
public void onSwipingDown(final MotionEvent event) {
}
#Override
public void onSwipedDown(final MotionEvent event) {
Animation ToolbarVisible = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_toolbar_visible);
m_Toolbar.startAnimation(ToolbarVisible);
}
});
}
Use visibility of view
if it is visible don't start animation.
Use animation listeners, so that you can set your view visibility to GONE
Try this code::
Add boolean to manage swipe gesture.
private boolean isSwipeUp=falese;
//Swipe Events WebSwipe
WebSwipe = new Swipe(350, 700);
WebSwipe.setListener(new SwipeListener() {
#Override
public void onSwipingLeft(final MotionEvent event) {
}
#Override
public void onSwipedLeft(final MotionEvent event) {
}
#Override
public void onSwipingRight(final MotionEvent event) {
}
#Override
public void onSwipedRight(final MotionEvent event) {
}
#Override
public void onSwipingUp(final MotionEvent event) {
}
#Override
public void onSwipedUp(final MotionEvent event) {
if(!isSwipeUp)
{
Animation ToolbarGone = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_toolbar_gone);
m_Toolbar.startAnimation(ToolbarGone);
isSwipeUp = true;
}
}
#Override
public void onSwipingDown(final MotionEvent event) {
}
#Override
public void onSwipedDown(final MotionEvent event) {
Animation ToolbarVisible = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_toolbar_visible);
m_Toolbar.startAnimation(ToolbarVisible);
isSwipeUp = false;
}
});
}
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 set automatic scroll for each EditText(VerifiedEditText in my case) I have.
I did it this way but I don't think it is the way it should be done (looks too complicated, messy and too long), would you please recommend me making it anyway simpler? I am still learning by myself, so an explanation to attached code would be really appreciated. Thank you
#Override
public void onFocusScroll() {
final ScrollView mScrollView;
mScrollView = (ScrollView) rootView.findViewById(R.id.register_scroll);
final VerifiedEditText editText0 = (VerifiedEditText) rootView.findViewById(R.id.register_ticket);
final VerifiedEditText editText1 = (VerifiedEditText) rootView.findViewById(R.id.register_sektor);
final VerifiedEditText editText2 = (VerifiedEditText) rootView.findViewById(R.id.register_row);
final VerifiedEditText editText3 = (VerifiedEditText) rootView.findViewById(R.id.register_seat);
editText0.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mScrollView.post(new Runnable() {
public void run() {
mScrollView.smoothScrollTo(0, mScrollView.getTop());
}
});
}
}
});
editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mScrollView.post(new Runnable() {
#Override
public void run() {
mScrollView.smoothScrollTo(0, mScrollView.getBottom());
}
});
}
}
});
editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mScrollView.post(new Runnable() {
#Override
public void run() {
mScrollView.smoothScrollTo(0, mScrollView.getBottom());
}
});
}
}
});
editText3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mScrollView.post(new Runnable() {
#Override
public void run() {
mScrollView.smoothScrollTo(0, mScrollView.getBottom());
}
});
}
}
});
}
implement class by `View.OnFocusChangeListener`
and set Focus Listener to the current instanace of class because treat as View.OnFocusChangeListener instnace
and Override onFocusChange() medthod
v.getTop() return top position of current focused view
editText0.setOnFocusChangeListener(this);
editText1.setOnFocusChangeListener(this);
editText2.setOnFocusChangeListener(this);
editText3.setOnFocusChangeListener(this);
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mScrollView.post(new Runnable() {
public void run() {
mScrollView.smoothScrollTo(0, v.getTop());
}
});
}
}
}
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"
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.
}
I'm trying to use this code to set an "ontouchlistener". It says there are no errors in the code but when I try to run it I get a force quit... what is going wrong? any ideas?
final Handler handler = new Handler();
Runnable mLongPressed = new Runnable() {
public void run() {
}
};
#Override
public boolean onTouchEvent(MotionEvent event, View v){
if(event.getAction() == MotionEvent.ACTION_DOWN)
handler.postDelayed(mLongPressed, 1000);
if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() ==
MotionEvent.ACTION_UP))
handler.removeCallbacks(mLongPressed);
return false;
}
In OnTouchListener the event function is:
public abstract boolean onTouch (View v, MotionEvent event)
which is called when a touch event is dispatched to a view. But you are using this one?:
public boolean onTouchEvent(MotionEvent event, View v)
To implement the listener, we can easily do this:
myImgView.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
// put your code here
return false;
}
});