Programming a Button In Android Java to simulate HW Keyboard - java

I am writing an Android app in Java and want navigation button at the bottom (up, down, left, right, enter, escape (back), and brightness control). My button presses look like this
public void upButtonPressed(View view)
{
BaseInputConnection bic = new BaseInputConnection(view, true);
bic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP));
}
I have also tried
public void upButtonPressed(View view)
{
new Thread(new Runnable() {
#Override
public void run() {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);
}
});
}
Any idea where I am going wrong? Also, yes I need this, and yes the program works on a VM with my HW keyboard.

You can use dispatchKeyEvent() to dispatch a KeyEvent to a View for it to handle.
So, if you want the view to handle the KeyEvent, then,
public void upButtonPressed(View view)
{
view.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP));
}
Else, if you want the Activity to handle the KeyEvent, then,
public void upButtonPressed(View view)
{
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP));
}

Related

Libgdx - Textfield, show Keyboard input field

im implementing a Login UI for a game on Andoird.
I use a Libgdx Textfield, but sometimes the Android keyboard covers up the Textfield, so the entered text cannot be read.
As solution i would like to show a small input area at the top of the Android keyboard like this:
I saw an option where i replace the default keyboard by a Android Textinput Dialog, but i dont like the dialog wich pops up:
textfield.setOnscreenKeyboard(new TextField.OnscreenKeyboard() {
#Override
public void show(boolean visible) {
//Gdx.input.setOnscreenKeyboardVisible(true);
Gdx.input.getTextInput(new Input.TextInputListener(){
#Override
public void input(String text){
tf_pw1.setText(text);
}
#Override
public void canceled(){
System.out.println("Cancelled.");
}
}, "Title", "Default text...", "Try it out.");
}
});
Is there a build in option in libgdx i could use, or should i implement it by myself?
Please give advice or hints, Thank you! :)
if you want to use builtin mechanism just use the Gdx.input.getTextInput method
void getTextInput(Input.TextInputListener listener,
java.lang.String title,
java.lang.String text,
java.lang.String hint)
in following way:
TextInputListener textListener = new TextInputListener()
{
#Override
public void input(String input)
{
System.out.println(input);
}
#Override
public void canceled()
{
System.out.println("Aborted");
}
};
Gdx.input.getTextInput(textListener, "Your login: ", "placeholder", "");
Second option is to make a normal TextInput and focus the camera on it by zooming and changing camera position so it will be at the top of the screen when user will touch the input - although there is always risk that you will "missed" the input and it will not zoom properly

How to perform an action after button click

I need to perform an action after onClick method of OnClickListener has run.
Here is my code for onClickListener:
View.OnClickListener imgButton0Handler0 = new View.OnClickListener() {
int identifier=0;
public void onClick(View v) {
//check if tile is found and return if it is
if(isFound[identifier]==true) return;
//set tile as open
checkField[identifier]=1;
//set background on predetermined
button0.setBackgroundResource(tiles[identifier]);
}
};
After this has run, and the background is set I would like to call a method checker(int identifier) which will check for other open tiles and change backgrounds accordingly.
This method needs to be run separately because the background is only displayed after onClick finishes, and I need predetermined background shown for a short time before checker method changes it to something else.
How can I accomplish this?
Have You Tried Post Delayed see this,
View.OnClickListener imgButton0Handler0 = new View.OnClickListener() {
int identifier=0;
public void onClick(View v) {
//check if tile is found and return if it is
if(isFound[identifier]==true) return;
//set tile as open
checkField[identifier]=1;
//set background on predetermined
button0.setBackgroundResource(tiles[identifier]);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
checker(identifier) // your method call
}
}, 3000); // 3 second
}
};

Code seems to stop xml layout from showing in Android Studio

The problem is that I have an Android app that doesn't seem to show the xml layout when I put this while loop into the class file. The loop is as follows:
while(!clicked){
button_a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player= MediaPlayer.create(GameActivity.this, R.raw.a);
player.start();
clicked = true;
letterTapped = 0;
}
});
}
The whole project works completely fine without it so I'm pretty sure that there must be something wrong with the loop that I am overlooking.
If you want me to put any other bits of code up here I will be more than happy to.
To stop listening as soon as the button is pressed, you can use this code:
button_a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player= MediaPlayer.create(GameActivity.this, R.raw.a);
player.start();
letterTapped = 0;
// Ignore further clicks
button_a.setOnClickListener(null);
// Disable button so the user knows that he can't click again
button_a.setEnabled(false);
}
});

How to disable multiple clicks on button in Android

When you click button in my app if you are fast enough before the screen/popup loads it loads them multiple times. I know how to disable the click on the button but that's not an option, because when you close the popup or return to the previous screen the button is disabled. I tried with Handler and Runnable to wait for 1s before the button is active again but this solution is not optimal in case if the OS needs more time to open the next screen. So I am searching for the most optimal solution. Any ideas?
Edit: setClickable(false) and then setting it back to true doesn't work because it loads my screen/popup slower than expected the button is enabled again and it opens the screen/popup multiple times again.
You can disable the multiple click at the same time using the following code
private boolean isClicked;
#Override
public void onClick(final View v) {
if(isClicked) {
return;
}
isClicked = true;
v.postDelayed(new Runnable() {
#Override
public void run() {
isClicked = false;
}
}, 1000);
}
Implement logic in your onClick to determine whether you want to ignore the click.
You can disable the button. When you close the popup enable the button and when the popup is visible make it disable. Keep listening the actions for popup and when the user get back to the previous screen.
Maintain one variable on button onClick listener and change the value to determine when you want to click button..
You can stop multiple operations by this way.
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick()
{
performOperation();
}
});
public void performOperation()
{
static boolean working = true;
if(working)
{
return;
}
working = true;
//Do you work here;
working = false;
}

Android - hidden text --> show text for 2 seconds --> hide text again

I am developing a board game that user plays with android. Since android is quite fast, I want to fake that android is performing some tough calculations and thus needs time for its next move.
What I want to do:-
User turn - he moves.
Android turn - android shows text "I am thinking" for 2 seconds
Android hides that text and and only after that moves his turn.
I tried doing:-
onAndroidTurn(){
textView1.setVisibility(View.VISIBLE);
Thread.sleep(2000);
textView2.setVisibility(View.GONE);
}
But what happens is that thread sleeps but text is not shown (okay I know why).
Then searching on stackoverflow, I learnt a way:-
onAndroidTurn(){
textView1.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable(){
void run() {
textView1.setVisibility(View.GONE);
}
}, 2000);
}
Now what this does is that it runs that text in another thread and android's turn is updated on screen and after moving it's turn android showing "Thinking" is total stupidity.
What can I do for this?
Try the following:
Show "I'm thinking"
Calculate your move but don't actually do the move, just store it for a while
Schedule timer
When timer runs out remove the text and do the move
Something like this:
onAndroidTurn(){
textView1.setVisibility(View.VISIBLE);
saveMove(calculateNextMove());
new Handler().postDelayed(new Runnable(){
void run() {
textView1.setVisibility(View.GONE);
doNextMove(restoreMove());
}
}, 2000);
}
Maybe you could just use an AsyncTask (like this pseudo-code):
private class ShowTextTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute(Void... result) {
textView.setText("Initital");
}
protected Long doInBackground(Void... urls) {
Thread.sleep(2000);
}
protected void onPostExecute(Void... result) {
textView.setText("After 2 seconds");;
}
}
Okay i did it like this:-
public void androidThinking(){
textView1.setVisibility(View.VISIBLE);
androidThinking = true; //explained below
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
droidThink.setVisibility(View.GONE);
//CODE for android's turn
androidThinking = false; //explained below
}
}, 2000);
androidThinking when set to true prevents the user from moving his turn by forcing the listener for user's button to return prematurely
btn.setOnClickListener(new View.onClickListener(){
#Override
public void onClick(View v) {
if(androidThinking)
return;
Thanx everyone for your reply.

Categories