I'm trying to cacth done action which comes from softkeyboard. My code is like this;
private TextView.OnEditorActionListener getDoneListener() {
return new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
nextPage();
return true;
}
else {
return false;
}
}
};
}
However even though IDE evaluates "(actionId == EditorInfo.IME_ACTION_DONE)"as true, it's not calling nextPage(), returning false instead. If I cast expression like this, it works fine.
private TextView.OnEditorActionListener getDoneListener() {
return new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((boolean)(actionId == EditorInfo.IME_ACTION_DONE)) {
processPayment();
return true;
}
else {
return false;
}
}
};
}
Any idea what can cause this?
Related
For now I'm using this algorithm:
List<String> constantListDataHeader = playersNames;
String[] listDataHeaderArray = new String[listDataHeader.size()];
playersNames.toArray(listDataHeaderArray);
...
EditText textsearch = view.findViewById(R.id.txtsearch);
textsearch.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
for (String item : listDataHeaderArray) {
if (item.toLowerCase().contains(textsearch.getText().toString().toLowerCase())) {
} else {
listDataHeader.remove(item);
}
}
return false;
}
return false;
}
});
The problem is the fact that if I try to perform search one more time, it won't work because the listDataHeader will consist of suitable for the previous search items only (listDataHeader.remove). Are there any other ways to perform search if I need it to work multiple times?
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 created a class that implements GestureDetector.OnGestureListener. However, I do not know how these methods get called. Is there something like view.setOnTouchListener() for a GestureListener that allows you to receive the events? Thank you in advanced. Here is my class
public class GestureHandler implements GestureDetector.OnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private int direction = -1;
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#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 event1, MotionEvent event2,
float velocityX, float velocityY) {
//Left Swipe
if(event1.getX() - event2.getX() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
direction = 270;
Log.d("Swiped: ", direction + "");
return true;
//Right Swipe
} else if(event2.getX() - event1.getX() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
direction = 90;
Log.d("Swiped: ", direction + "");
return true;
}
//Up Swipe
if(event1.getY() - event2.getY() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
direction = 0;
Log.d("Swiped: ", direction + "");
return true;
//Down Swipe
} else if(event2.getY() - event1.getY() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
direction = 180;
Log.d("Swiped: ", direction + "");
return true;
}
direction = -1;
Log.d("Swiped: ", direction + "");
return false;
}
}
Very simple solution here. :)
Here is how I did this in my project, working with Google Glass -
I didn't actually extend GestureDetector. I had something like this:
public class EXAMPLE {
private GestureDetector gestureDetector;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureDetector = createGestureDetector(this);
}
private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetectorTemp = new GestureDetector(context, new GestureDetector.OnGestureListener() {
#Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
#Override
public void onShowPress(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float distanceX, float distanceY) {
return false;
}
#Override
public void onLongPress(MotionEvent motionEvent) {
}
#Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) {
return false;
}
});
return gestureDetectorTemp;
}
#Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (gestureDetector != null) {
return gestureDetector.onTouchEvent(event);
}
return false;
}
}
That last part is very important. On any generic motion event, if the gestureDetector isn't null, you'll send the event through the gestureDetector for processing.
KEEP IN MIND ALSO that you need to understand what the return false; and return true; things mean. If you return false, then that means that the event wasn't consumed. If you return true, then the event is consumed. In other words, if you return true, then nothing else will activate, because the event gets 'eaten up,' but if you return false, this sends the event on to other functions which may do something when an action is taken.
I hope this helps. Good luck :)
I want to add a search button on the keyboard input and the EditText and searches I proceeded as follows and I have them the following errors
recherche=(EditText)findViewById(R.id.recherche);
recherche.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
new DownloadTask().execute();
return true;
}
return false;
}
});
ERROR : The type new TextView.OnEditorActionListener(){} must implement the inherited abstract method
TextView.OnEditorActionListener.onEditorAction(TextView, int, KeyEvent)
ERROR : KeyEvent cannot be resolved to a type
Here is a sample of my code:
TextView.OnEditorActionListener enterListener = new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if ((keyEvent != null && (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (i == EditorInfo.IME_ACTION_DONE)) {
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
bQServer.performClick();
}
return false;
}
};
//etUserName.setOnEditorActionListener(enterListener);
etPassword.setOnEditorActionListener(enterListener);
You should be able to just change KeyEvent.KEYCODE_ENTER for KeyEvent.KEYCODE_SEARCH
i.e
TextView.OnEditorActionListener enterListener = new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if ((keyEvent != null && (keyEvent.getKeyCode() == KeyEvent.KEYCODE_SEARCH)) {
new DownloadTask().execute();
return true;
}
return false;
}
};
recherche.setOnEditorActionListener(enterListener);
--Edit--
Make sure you have imported KeyEvent and onEditorActionListener
import android.view.KeyEvent;
import android.widget.TextView.OnEditorActionListener;