In my activity I have an ImageView. It has pinch-zoom feature.
When I touch on ImageView I show thumbnails layout.
But when I pinch on ImageView thumbnail layout shows. I want to block it?
How can I do it?
Here is my code to show thumbnail layout:
image.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Boolean openThumbnails=true;
if(event.getAction() == MotionEvent.ACTION_UP && openThumbnails){
Log.e("event.getAction()", "MotionEvent.ACTION_UP");
if(thumbnailsLayout.getVisibility()==View.GONE && header.getVisibility()==View.GONE && openThumbnails){
thumbnailsLayout.setVisibility(View.VISIBLE);
header.setVisibility(View.VISIBLE);
header.bringToFront();
}
else{
thumbnailsLayout.setVisibility(View.GONE);
header.setVisibility(View.GONE);
}
}
else if(event.getAction() == MotionEvent.ACTION_DOWN){
Log.e("event.getAction()", "MotionEvent.ACTION_DOWN");
return true;
}
else if(event.getAction()==MotionEvent.ACTION_MOVE){
Log.e("openThumbnails before", openThumbnails.toString());
openThumbnails=false;
Log.e("openThumbnails and after", openThumbnails.toString());
}
return false;
}
});
Your openThumbnails is always true. You set it at the start of method. Every touch event (e.g. Action_up, action_move) you set openThumbnail to true. That's why you always show it.
image.setOnTouchListener(new OnTouchListener() {
Boolean openThumbnails=true;
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP && openThumbnails){
Log.e("event.getAction()", "MotionEvent.ACTION_UP");
if(thumbnailsLayout.getVisibility()==View.GONE && header.getVisibility()==View.GONE && openThumbnails){
thumbnailsLayout.setVisibility(View.VISIBLE);
header.setVisibility(View.VISIBLE);
header.bringToFront();
}
else{
thumbnailsLayout.setVisibility(View.GONE);
header.setVisibility(View.GONE);
}
}
else if(event.getAction() == MotionEvent.ACTION_DOWN){
Log.e("event.getAction()", "MotionEvent.ACTION_DOWN");
return true;
}
else if(event.getAction()==MotionEvent.ACTION_MOVE){
Log.e("openThumbnails before", openThumbnails.toString());
openThumbnails=false;
Log.e("openThumbnails and after", openThumbnails.toString());
}
return false;
}
});
Related
Problem is on a service view back button is giving me a response but Home and Recent Apps button is not triggering
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_HOME: //Not getting
minimizeApp();
return true;
case KeyEvent.KEYCODE_BACK: //getting
finish(false);
return true;
case KeyEvent.KEYCODE_APP_SWITCH: //Not getting
//minimizeApp();
return true;
}
return true;
}
in the same service, I have tried to add and remove different flags but can't gets what I want.
mLayoutParams =new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN|
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
|WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
,
PixelFormat.TRANSLUCENT);
I am getting Back pressed but I want to catch Event of home and recent app pressed.
Updated
I got the solution
li = LayoutInflater.from(this);
LinearLayout lia= new LinearLayout(this) {
//home or recent button
public void onCloseSystemDialogs(String reason) {
if (reason.equals("homekey")){
finisha(false);
}
if (reason.equals("recentapps")){
finisha(false);
}
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
|| event.getKeyCode() == KeyEvent.KEYCODE_APP_SWITCH
|| event.getKeyCode() == KeyEvent.KEYCODE_HOME
) {
//The Code Want to Perform.
// return true;
}
if (event.getKeyCode() == KeyEvent.KEYCODE_HOME){
}
if (event.getKeyCode() == KeyEvent.KEYCODE_APP_SWITCH){
}
return super.dispatchKeyEvent(event);
}
};
lia.setFocusable(true);
final View root = (View) li.inflate(R.layout.layout_alias_locker, lia);
I am trying to disable left and right on the keyboard with this code but viewPager on the press of left and right button change the state of the pager.
edComment.setOnEditorActionListener(
new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_FLAG_NAVIGATE_PREVIOUS
|| actionId == EditorInfo.IME_FLAG_NAVIGATE_NEXT
|| actionId == EditorInfo.IME_ACTION_PREVIOUS
|| actionId == EditorInfo.IME_ACTION_NEXT
) {
return false;
}
// Return true if you have consumed the action, else false.
return false;
}
});
You can extend the ViewPager and override the arrowScroll method like this:
public class Wizard extends ViewPager {
#Override
public boolean arrowScroll(int direction) {
// Never allow pressing keyboard arrows to switch between pages
return false;
}
}
The keyboard app can show whatever keys it wants. There is no way to force it not to show certain buttons, and even if there was another keyboard wouldn't follow the same rules.
According to #Gabe Sechan button are not hide or disable so I am doing like that
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.i("your tag", "Keycode: " + keyCode);
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
Log.e("Click","left");
/* for (int n = 0; n < count; n++) {
setCurrentPage(arrayListView.get(n + 1));
}*/
pager.setCurrentItem(getItem(+1), true);
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Log.e("Click","right");
pager.setCurrentItem(getItem(-1), true);
return true;
case KeyEvent.KEYCODE_DPAD_UP:
Log.e("Click","up");
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
Log.e("Click","Down");
return true;
default:
return super.onKeyUp(keyCode, event);
}
}
I am writing an Android app that needs to respond to touch events. I want my app to change the color of my list item to a custom color. I have written the following code, but only the MotionEvent.ACTION_DOWN section is working. The LogCat shows that ACTION_CANCEL and ACTION_UP aren't called at all. Could you please help me understand why my code isn't working.
This is my code...
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
view.setBackgroundColor(Color.rgb(1, 1, 1));
Log.d("onTouch", "MotionEvent.ACTION_UP" );
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
view.setBackgroundColor(Color.rgb(23, 128, 0));
Log.d("onTouch", "MotionEvent.ACTION_DOWN" );
}
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
view.setBackgroundColor(Color.rgb(1, 1, 1));
Log.d("onTouch", "MotionEvent.ACTION_CANCEL" );
}
return false;
}
});
If you return false from onTouch method, no further events get delivered to the listener. You should return true at least in case of event.getAction() == MotionEvent.ACTION_DOWN.
Refactor your code as given below:
view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
view.setBackgroundColor(Color.rgb(1, 1, 1));
Log.d("onTouch", "MotionEvent.ACTION_UP" );
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
view.setBackgroundColor(Color.rgb(23, 128, 0));
Log.d("onTouch", "MotionEvent.ACTION_DOWN" );
return true;
}
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
view.setBackgroundColor(Color.rgb(1, 1, 1));
Log.d("onTouch", "MotionEvent.ACTION_CANCEL" );
}
return false;
}
});
In my code I want to add zoom in on double-tap and zoom out on two-fingers-tap (like Google Maps). I'm using this code:
gestureDetector = new GestureDetector(new DoubleTapDetector());
touchListener = new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
final int action = motionEvent.getAction();
final int fingersCount = motionEvent.getPointerCount();
if ((action == MotionEvent.ACTION_POINTER_UP) && (fingersCount == 2)) {
onTwoFingersTap();
return false;
}
return gestureDetector.onTouchEvent(motionEvent);
}
};
Double-tap works fine, but when I trying to pinch map, it zoom as usual but zoom out by one step because of onTwoFingersTap(); is catched too.
How can I avoid this?
return true if your condition is satified:
touchListener = new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
final int action = motionEvent.getAction();
final int fingersCount = motionEvent.getPointerCount();
if ((action == MotionEvent.ACTION_POINTER_UP) && (fingersCount == 2)) {
onTwoFingersTap();
return true;
}
return gestureDetector.onTouchEvent(motionEvent);
}
};
I'm trying to use setontouchlistener. What I would like to do if I hold an imagebutton for an example 5 seconds, I get alerted... this is how I would do it:
final ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
imageButton1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// do something
return false;
}
});
How can I implement something like this with setontouchlistener after x seconds?
Working solution:
imageButton1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mTouchDownTime = event.getEventTime();
Log.v("CustomDebug", "Message: " + mTouchDownTime);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
long elapsedTime = event.getEventTime() - mTouchDownTime;
Log.v("CustomDebug", "Message: " + elapsedTime);
}
return false;
}
});
onTouch gets called twice, the first time with a MotionEvent ACTION_DOWN and a second time with the MotionEvent ACTION_UP
so pseudocode:
if Event == ACTION_DOWN
saveTime = time();
elseif Event == ACTION_UP and time()-savetime > 5
doStuff because the guy hold it for 5sec.