I have Actors that I need to move when the keyboard becomes visible (When I press a TextField), or they are stuck behind it. I do this by moving the camera up:
stage.getViewport().getCamera().position.set(stage.getWidth()/2, stage.getHeight()/3, 0);
stage.getViewport().getCamera().update();
This works fine. It also works fine to move it back when I touch something outside the TextField and call stage.unfocusAll();
My problem is, when I'm in a TextField and press Androids Back button, it hides the keyboard, but does not call the code I have inside the Inputprocessor (THIS CODE CAPTURES BACK-BUTTON ALWAYS EXCEPT WHEN INSIDE A TEXTFIELD AND KEYBOARD IS VISIBLE):
InputProcessor backProcessor = new InputAdapter() {
#Override
public boolean keyDown(int keycode) {
if ((keycode == Input.Keys.ESCAPE) || (keycode == Input.Keys.BACK) )
{
moveBack();
}
return false;
}
};
I looked around and read that it is not possible to catch the back-button when inside a TextField. Which leads me to my questions:
This must be a common scenario (moving UI to work with keyboard), how do other people do it?
If other people do like me (move camera), how do you handle the Android back-button?
EDIT: This answer captures the back-key while inside a TextField, however it has to be done in the Android Launcher, so I can't reach the elements I need to reach. I also overwrites all other calls to the BACK-button from inside the LibGdx project.
Ok so with the help of This I got a solution working, but it's not very pretty.
Basically I rewrite my GDX Launcher class, and use the following code as the layout:
RelativeLayout layout = new RelativeLayout(this) {
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
game.setMoveBack(true); }
}
return super.dispatchKeyEventPreIme(event);
}
};
Then, in the render-method of my MainScreen (Screen that all other Screens inherit from), inside the render-method I call:
if(game.isMoveBack()){
stage.getViewport().getCamera().position.set(stage.getWidth()/2, stage.getHeight()/2, 0);
stage.getViewport().getCamera().update();
game.setMoveBack(false);
}
If anyone has an easier way to do this please post and Ill accept that as an answer, but for now, this works if anyone finds themselves where I am :)
Related
I have a certain activity that begins when I tap my smart watch's screen. There is a timer and bunch of stuff that happens, but the process is crucial, so I am handling certain cases or things that might happen that would disturb the flow of things.
So basically, I want to prevent the home button of my watch to exit the app and go to the homescreen while my timer is running. I keep looking this up and most people say to override the onBackPressed method. But this was for the back button, and I I realized the button is a home button not a back button.
frameLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
clicked = clicked + 1;
if (clicked == 2)
{
Toast.makeText(toolandmode.this, "Clicks:" + clicked, Toast.LENGTH_SHORT).show();
startTimer();
}
else if (clicked >= 4)
{
Toast.makeText(toolandmode.this, "Clicks:" + clicked, Toast.LENGTH_SHORT).show();
AlertMessage();
}
}
return true;
}
});
this is the main method I use
Just override the onBackPressed function.
#Override
public void onBackPressed ()
{
//Control the flow
}
Use third part library in case if it solves your problem.
Here is the link:
https://github.com/shaobin0604/Android-HomeKey-Locker
The general consensus is that you can't override the home button behavior on a Wear OS device, just like you can't override the home button on an Android phone. This is by design to prevent developers from preventing the user to leave an application. Even if there is a hacky way to do it, this is not officially supported and may stop working at any time in future OS versions. I highly suggest not doing it since it goes against the basic navigation model of the device.
More details, and some workarounds for common use cases where people think they need to override the home button can be found in this blog post.
I use the following method to delete values from EditText when user presses the delete button of an android device.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
onDeleteKey();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void onDeleteKey() {
if (edt_passcode1.isFocused()) {
} else if (edt_passcode2.isFocused()) {
edt_passcode1.requestFocus();
edt_passcode1.setText("");
} else if (edt_passcode3.isFocused()) {
edt_passcode2.requestFocus();
edt_passcode2.setText("");
} else if (edt_passcode4.isFocused()) {
edt_passcode3.requestFocus();
edt_passcode3.setText("");
}
}
I want to delete the previous EditText value when user presses the delete button on their device, but it's not called when the delete button is pressed. But onKeyDown() method doesn't called.
Please help me about it.
Thanks.
From the docs of activity:
Called when a key was pressed down and not handled by any of the views
inside of the activity. So, for example, key presses while the cursor
is inside a TextView will not trigger the event (unless it is a
navigation to another object) because TextView handles its own key
presses.
Im guessing thats your problem?
Link:
http://developer.android.com/reference/android/app/Activity.html#onKeyDown%28int,%20android.view.KeyEvent%29
EDIT:
One possible solution would be to attach a custom onKeyListener to your TextView. It is not necessary to subclass TexView like a commenter suggested, although thats an option as well.
You can get a template for the keyListener from this SO-Question:
Get the key pressed in an EditText
NEXT EDIT:
I just realized the first solution will only work for hardware keyboards.
If you want Virtual Keyboard Support, my best guess wild be an TextWatcher.
See a Sample implementation here:
Validating edittext in Android
NEXT AND HOPEFULLY FINAL EDIT:
Some googling turned up Activity's dispatchKeyEvent() - method. just implement it like you did with onKeyDown, you will be passed a KeyEvent that will tell you all about the pressed key.
Here's a link to the docs:
http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent%28android.view.KeyEvent%29
I'm currently trying to build an Android app with Xamarin Studio (aka monodroid). My problem is as follows:
In my Activity's OnCreate I call
this.RequestWindowFeature(WindowFeatures.ActionBar);
this.RequestWindowFeature(WindowFeatures.ActionModeOverlay);
Also, I added this override to the Activity:
public override Boolean OnTouchEvent(MotionEvent e)
{
if (this.ActionBar.IsShowing)
{
this.ActionBar.Hide();
}
else
{
this.ActionBar.Show();
}
return base.OnTouchEvent(e);
}
This, however, doesn't work as I expect it to: When I first tap on the Activity, the bar is hidden. The next tap shows it, but it doesn't even fully slide into view before it retreats again. Also, it seems to "cram" the contents of the Activity, which it shouldn't do since I added WindowFeatures.ActionModeOverlay.
What am I missing?
EDIT:
I am certain that the method itself is only triggered once per touch (I logged a counter to diagnose).
Also, I don't show()/hide() the ActionBar anywhere else.
EDIT 2: I was certain, but probably messed up the diagnostic approach. :/
After adding
if (e.Action == MotionEventActions.Cancel
|| e.Action == MotionEventActions.Up)
{
return base.OnTouchEvent(e);
}
it behaves as expected.
Motion Event is catching an event when you are releasing a touch, you need to filter the event further using e.Action for touch down and release events. I think down is triggering show, and release is hiding it again
I have an activity which has multiple screens depending on which buttons the user clicks.
What should I do if I need to handle back button in this activity. i.e. When I press to back button it has to go previous screen of my activity.
I am really new to android. can any body help me to solve this problem
Thanks for reading.
I guess that by multiple screens you mean you have some layouts and change them with setContentView(). You'll have to override the back button's behavior, keep a history of user navigation between various screens (if there's no forced path) and have the back button code set content to the previous screen.
Overriding the back button is easy if you're on API >= 5: see onBackPressed().
If you want also backward compatibility you'll find something here and here.
As this is usually all done automatically by Android with activities, consider having multiple activities instead of a single activity with multiple screens.
override onBackPressed() function in your activity and write the desired code in it for the intent firing.
You can do this by handling the KeyDown event and adding some condition( like taking a static variable and at each activity assign different value to the static variable ) at each actvity.
See the following code:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if(condition 1)
{
Intent i=new Intent("com.prac.A"); // A is your another activity in com.prac package
startAcitvity(i);
}
else if(condition 2)
{
Intent i=new Intent("com.prac.B");
startAcitvity(i);
}
else
{
Intent i=new Intent("com.prac.C");
startAcitvity(i);
}
}
return false;
}
Hope this will help you.
I am trying to handle the back button event on my app but its not working at all. I have inplemented ActivityGroup in my app according to the post Android: TabActivity Nested Activities
I have added the following code according to many posts in this website
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(this.getClass().getName(), "back button pressed: " + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Log.d(this.getClass().getName(), "back button pressed");
return true;
}
return super.onKeyDown(keyCode, event);
}
but for some reason i still dont know i am not getting the lines to be logged, it goes back to the home screen. I know that the onBackPressed will not work for me because I need to have this app implemented using api level 4 and it is not available at this level.
My ActivityGroup has only two activities, one list view and a details view. I have put this code on all the three classes to try something different, but still cant get it working. I see "No keyboard for id 0" in the logs, but i dont think it means something that can be related to the problem.
I do appreciate any answer to this.
Many thanks
T
Add a log statement right above the return line and see what KeyEvent is happening.
like this:
Log.w(keyCode, "This is the key code that is returned");
return super.onKeyDown(keyCode, event);
Now look at the returned value and verify/compare it to KeyEvent.KEYCODE_BACK and this may point you in the direction of your problem.