Libgdx - Textfield, show Keyboard input field - java

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

Related

How to override a method when a button is tapped?

I was thinking of creating a button that when tapped can disable the phone back button. I wanted to be able to enable it back again by pressing another button. However, the way that I found to disable the back button was with an override. Could somebody lend me a hand on how I could do that? Thanks!
What I tried was to put the override inside the button listener and onClick method, but it highland the override in red. I then tried putting the override in a different class and then calling the class when the the button is tapped.
I figured it out, but for anyone wondering the same thing, I'll post what I did. I implemented the following code inside MainActivity.class with the buttons:
private boolean backButtonEnabled = true;
#Override
public void onBackPressed() {
if (backButtonEnabled) {
super.onBackPressed();
}
}
public void disableBackButton() {
backButtonEnabled = false;
}
public void enableBackButton() {
backButtonEnabled = true;
}

How to reverse recyclerView layout like a chat app with FirebaseUI FirestoreRecyclerAdapter?

I am using FirestoreRecyclerAdapter to make a chat app in java. When adding a message it works fine but I want messages to show at bottom not on top. I tried
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
Now it reverse it's position which is good but it's not automatically scrolling to bottom position. And when I add a new message I need to scroll manually to see it.
Recyclerview automatically scroll to bottom after adding below lines
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
recyclerView.smoothScrollToPosition(0);
}
});
}
}, 1000);
above solution will not work if parent layout is horizontal scroll.
Other solution is
recyclerView.scrollToPosition(0);
Both line of code work for me. you can use any of the solution
The best way to do it is use the following piece snippet whenever there is a new message. You can use a snapshot listener for that
y = (Here you need to try 2 things. My coding laptop is not with me so I cannot test and tell but either 0 should work or the chatlist.size()-1);
layoutManager.scrollToPosition(y);
OR
layoutManager.smoothScrollToPosition(y);// I do not recommend this for chat apps since it will take time to go down if there are a lot of texts below the user
This is what I used:
#Override
public void onStart() {
super.onStart ();
adapter.startListening();
recyclerView.scrollToPosition (0);
}
Also use
#Override
public void onDataChanged() {
super.onDataChanged ();
recyclerView1.smoothScrollToPosition (0);
}
in adapter
I was using
recyclerView.scrollToPosition (0);
in onCreate and adapter was starting in onStart. Now it's working

Programming a Button In Android Java to simulate HW Keyboard

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));
}

LikeView (android view) in libgdx

I'm trying to place a Like button in my app. After much searching I found that it is impossible to use me own custom button, so I am left only with implementing the default like button from the facebook sdk.
Since this LikeView seems to be a native android like view I don't really know how to put this into my libGDX app.
I would like to use this button only in a specific Screen and set its bounds so that it fits with the rest of my UI. Does anyone have an example of how to create this like button without using the XML (as it is done in all the documentation I have found so far).
Adding the following functions to my application makes it show in the correct position. Unfortunately the LikeView does not get the correct size, but is centered inside the view, which means changing the width/height just moves it.
public void GenerateLikeButton()
{
application.runOnUiThread(new Runnable(){
#Override
public void run() {
float x = 560 * game.global_scale;
int width = (int) (440 * game.global_scale);
int height = (int) (152* game.global_scale);
float y_from_bottom = game.screen_height - ((56+152+70) * game.global_scale + game.ad_height);
Gdx.app.log("like", "from bottom: "+ y_from_bottom);
likeButton = new LikeView(application);
likeButton.setLikeViewStyle(LikeView.Style.BUTTON);
likeButton.setX(x);
likeButton.setY(y_from_bottom-height);
likeButton.setObjectId(LIKE_URL);
likeButton.setVisibility(View.GONE);
application.layout.addView(likeButton,width,height);
likeButton.invalidate();
}
});
}
#Override
public void ShowLikeButton(final boolean visible)
{
application.runOnUiThread(new Runnable()
{
#Override
public void run()
{
if(visible)
likeButton.setVisibility(View.VISIBLE);
else
likeButton.setVisibility(View.GONE);
}
});
}

Android show keyboard and register keys

I have an application that is just a surfaceview. All i do is draw stuff on the surfaceview and whatnot. So one functionality i want is if the user touches a corner of the surfaceview it shows the keyboard and then they can type into it. Note that there are no EditTexts or Textboxes in my app anywhere. How do i call the keyboard to pop up and then how do i get all the keys that the user is pressing? I don't want the keys necessarily, i just want the string that they typed. How do i go about accomplishing this?
This is not a trivial task.
First of all, you'll need to override the method onCreateInputConnection()
#Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return new MyInputConnection(this, surfaceView, false);
}
Then you'll have to implement this input connection class, deriving from BaseInputConnection.
class MyInputConnection extends BaseInputConnection{
private MyActivity activity;
public MyInputConnection(MyActivity activity, View targetView, boolean fullEditor)
{
super( targetView, fullEditor );
mActivity = activity;
}
public boolean commitText(CharSequence text, int newCursorPosition){
myActivity.drawText((String) text);
return true;
}
There are more methods you'll want to override (see the reference), but start by focusing on commitText(). DrawText() is a method that renders the text on your surface, you'll have to come up with an implementation that suits your needs.

Categories