Gallery problem in pressing back button android - android

Hie team gud eve, I had a set of images like 1 2 3 4 5 6. on clicking on 1 (i.e first image) the one should show in another activity. When we swipe with the finger it should move to the next image. If i stop swiping near the image position 4. while pressing the back button on the device. The main Activity should show near the position of 4.
I already done with the swipe with the help of onFling(). The problem is when i press the back button the image is moving to the first position.I need in the same position......
Thanks in advance

manually you can override that key
i think this code will help u...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
try {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
//start the what activity you want
}
}

If you're targeting Android 2.0 or newer, you can use onBackPressed() to decide what happens when you hit the back button. This is slighlty easier to do.
#Override
public void onBackPressed() {
// Do something
}
To select a item in the Gallery:
gallery.setSelection(int selection);

Related

LibGdx: Move camera with Keyboard visible, capture Android BACK button

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 :)

Android onKeyDown() not execute on pressing delete button

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

disable "BACK" button in Android? [duplicate]

This question already has answers here:
Disable back button in android
(18 answers)
Closed 9 years ago.
Is their a way of disable a BACK button on my mobile? because while i'll transfer it to my mobile the BACK button is activated so that it can back the previous page. Please help me to disable BACK button while my app is running.
Override the onBackPressed() of your activity
#SuppressLint("MissingSuperCall")
#Override
public void onBackPressed()
{
// super.onBackPressed(); // Comment this super call to avoid calling finish() or fragmentmanager's backstack pop operation.
}
You may suppress lint's error by adding #SuppressLint("MissingSuperCall") as per Matthew Read pointed out.
This way works for all versions of Android, and will work with libraries that may override the default functionality in their Activities (such as cocos2d-x):
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return (keyCode == KeyEvent.KEYCODE_BACK ? true : super.onKeyDown(keyCode, event));
}
This is very simple.. put this code in every activity of your android code if you wish to disable the back button for complete aplication
#Override
public void onBackPressed()
{
//thats it
}
and you are done

Menu buttonĀ“s activity programming

I want to do a button that when I click it does the same what the menu button of the telephone would do.
I know for example that if I use onBackPressed(); it does the same that the back button of the telephone would do.
Anybody knows how to do that for the menu button?
Thank you
I found a link suggesting that you do this:
#Override
public boolean onKeyDown(int keycode, KeyEvent event ) {
if(keycode == KeyEvent.KEYCODE_MENU){
//WHATEVER YOU'D LIKE TO DO
}
return super.onKeyDown(keycode,event);
}
Is this what you are looking for?
Source of the code
If you are trying to open the app's option menu, as the Menu button does, take a look here
Android Option Menu on Button click

How to handle back button in activity if I have multiple screens in an activity

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.

Categories

Resources