Move focus from one ImageButton to another - android

I am fairly new to android and stuck on one problem. I need to know how I can response when I use directional keys on android ImageButton. I have been looking for the answer for almost a day now and can't find one.
What I need to achieve is I want to change focus to ImageButton4 from ImageButton3 if/when I click the right arrow key.
I know android handles this on its own but I need to customize it for my application.
I tried implementing OnKeyListener
private OnKeyListener speedTest = new OnKeyListener() {
public boolean onKey(DialogInterface inter, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
}
return false;
}
};
But I'm not sure if this would be the best way to do it since with each click I have to check where is focus and then change it accordingly.
Thanks.

Related

Android Custom Keyboard with Custom Button for each user

I have a specific requirement for providing every user a custom button that will only be available for him on the keyboard. The Custom Button can be any like (%,#,#,etc or may be numeric). How can i add that on the Custom Keyboard any ideas. I have already added the custom keyboard in my app but i also need this button.
Thanks in advance.
Actually you can set custom text to that little blue button. In the xml file just use
android:imeActionLabel="userName"
You can do this programatically as well like
etEditText.setImeActionLabel("userName", EditorInfo.IME_ACTION_DONE);
And if you what the Button onClickListner than use
etEditText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i(TAG,"Enter pressed");
}
return false;
}
});
It should be noted that this will not cause text to appear on all keyboards on all devices. Some keyboards do not support text on that button (e.g. swiftkey). And some devices don't support it either. A good rule is, if you see text already on the button, this will change it to whatever you'd want.

LibGDX - how to catch a backbutton inside a TextField with Android Keyboard visible?

I'm trying to catch the Back button while the TextField is focused (keyboard is visible).
I have already tried with Multiplexer - setting the 'BackProcessor' on the top of the stages - it doesn't work:
InputProcessor backProcessor = new InputAdapter() {
#Override
public boolean keyDown(int keycode) {
if ((keycode == Input.Keys.BACK) )
{
Gdx.app.log("INPUT", "BACK");
}
return false;
}
};
InputMultiplexer multiplexer = new InputMultiplexer(backProcessor,
loginStage,registerStage);
Gdx.input.setInputProcessor(multiplexer);
Also, I tried in the render method with:
if(Gdx.input.isKeyDown(Keys.BACK)
Doesn't work too.
Above solutions work perfectly EXCEPT the moment, when the keyboard is visible.
What I'm trying to achieve?
I need to catch the Back Button when the onScreenKeyboard is visible.
Edit
I also tried with TextFieldListener but 'BackButton' is the one key that hasn't any 'char code' so it can't be catched there:
public void keyTyped(TextField textField, char c)
FINAL EDIT
As LibGDX authors said - there's no way to retrieve this in a normal way cause the back button is proccessed outside of application while it's pressed when keyboard is visible. Android solution is to override EditText's onPreKeyIme() but LibGDX TextField has nothing to do with Android's one and there's no connection.
If there's anyone that could point any solution to this problem, I'd be grateful.
Rewrite your GDX Launcher class using this tutorial:
https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx
Then, you'll have your RelativeLayout so you can override your dispatchKeyEventPreIme() method
which will catch events before they're sent to the IME ;)
RelativeLayout layout = new RelativeLayout(this) {
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
// back pressed }
}
return super.dispatchKeyEventPreIme(event);
}
};
The answer is - impossible. Android doesn't catch the back button while keyboard is active. Ha. They don't even have a way to check if soft-keyboard is visible and there are tricks to catch it's view's height (which gives the keyboard height) which also makes everything wrong because it collides with other ui components like navigation/status bar and the whole immersive mode.
I hope that someday some Google Dev will look here, check this and think about fixing this after 6 years, because we're in 2016 and you're fixing code-style instead of critical bugs which are posted on tracker by greatest coders from here.
In AndroidLauncher, instead of doing this:
initialize(new MyGdxGame(this), configuration);
Do this:
RelativeLayout layout = new RelativeLayout(this) {
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
//here goes your code
}
return super.dispatchKeyEventPreIme(event);
}
};
View gameView = initializeForView(new MyGdxGame(this), configuration);
layout.addView(gameView);
setContentView(layout);

Getting the pressed character without using edidtext

I want to get a pressed character in the code without using edittext.
Simply a black screen appear if someone presses "a" or "b" etc.. I want to get this value in my code.
I have done it using editext and textwatcher but the requirement is we don't need to use them now.
Can we make character(alphabet) moving from top to bottom randomly
I have followed some links which have used marquee but it does not work.
In your Activity override the method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_A) {
//Here your black screen if you press A
return true;
}
return super.onKeyDown(keyCode, event);
}
Check this KeyEvent ;

Android: When the on-screen keyboard appears and disappears, are there any listeners that are automatically called?

I was wondering if there is any way to be notified automatically by android when the on-screen keyboard is shown and when it disappears.
For example when we click on a edittext, the ime appears. Will there be any event calls?
And when it disappears when we press back, similarly will there be any even calls?
I found this thread Android: which event fires when on screen keyboard appears? , however no answers have been reached yet.
The purpose is because i need an event to automatically manipulate visibility. I have an activity with an edittext on the top of the screen, below it, a listview and a linearlayout which are sitting on top of each other. To control what the user sees, i manipulate the visibility. By default, the linearlayout is shown initially, however, when the user is entering text, the listview should be shown instead. The listview should disappear when the user has finished typing, which in this case, the on-screen-keyboard will be closed.
I tried acomplishing the change of visibility using onFocusChange, however, even when the on-screen keyboard disappears, the edittext still retains focus and the linearlayout never reappears.
Below is my implementation of the onFocusChange
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if(v.getId()==R.id.search_screen_keyword_textbox)
{
if(hasFocus)
{
filterSection.setVisibility(View.GONE);
autoComSection.setVisibility(View.VISIBLE);
}
else
{
filterSection.setVisibility(View.VISIBLE);
autoComSection.setVisibility(View.GONE);
}
}
else if(v.getId()==R.id.search_screen_location_textbox)
{
if(hasFocus)
{
filterSection.setVisibility(View.GONE);
autoComSection.setVisibility(View.VISIBLE);
}
else
{
filterSection.setVisibility(View.VISIBLE);
autoComSection.setVisibility(View.GONE);
}
}
else
{
filterSection.setVisibility(View.VISIBLE);
autoComSection.setVisibility(View.GONE);
}
}
If anyone has any idea about it do let me know. :D
You can catch the back button when in an edittext, this is what would make the keyboard disappear. Using this method:
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
// Do your thing here
return false;
}
return super.dispatchKeyEvent(event);
}
Search is great: onKeyPreIme or Android API
It seems that this thread has a solution using onConfigurationChanged: How to capture the "virtual keyboard show/hide" event in Android?

OnKeyListner is not working

Hi Any reply is precious for me and appriciated as well
in one edit text i am setting onKeyListener so that when I enter 5 numerics in edittext it will accept n do next process but in samsung galaxy tablet it is not working i am using these lines of code
zipcode.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
System.out.println("setOnKeyListenersetOnKeyListenersetOnKeyListenersetOnKeyListenersetOnKeyListenersetOnKeyListenersetOnKeyListener");
if (event.getAction() == KeyEvent.ACTION_UP && zipcode.getText().length() == 5) {
System.out.println("OnKeyListener11111");
started = true;
searchByZipcode(zipcode.getText().toString());
}
return false;
}
});
searchByZipcode(zipcode.getText().toString());
line takes the text we are writing in to webservice but in galaxy flow doesnt get into onKeylistner can any1 pls help me out thanks
That's odd. Only in Samsung Galaxys? Is this the full Listener? Maybe you have some other method catching KeyEvents that return true?
Try replacing the code for onKey() by onKeyUp(), which is a TextView method:
http://developer.android.com/reference/android/widget/TextView.html#onKeyUp%28int,%20android.view.KeyEvent%29
BTW, why do you add return false;? Is there any other Listener that requires to handle the KeyEvent? If not, I would suggest to change it for return true;
If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

Categories

Resources