How to Listen Android keyboard Close Event after Typing finished in EditText? - android-edittext

I referred this link to listen the android EditText finish typing event. But from this reference i am getting the Enter key pressed event only. I need to listen an event when user closes keybord after finishing the typing and also when user presses Next. is there any way to listen theses events in android.

finally i got the answer for Next/Done/search AND enter key press like this
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(event !=null){
if(actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_NEXT ||
actionId == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!event.isShiftPressed()) {
Log.e("", "FINISHED Typing : "+v.getText().toString());
return true; // consume.
}
}
}
else{
if(actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE){
Log.e("", "Next/Done/Search Pressed");
return true;
}
}
return false;
}
also for the Keypad dismiss i customized the editText and override the method onKeyPreIme into that. Whenever i need the edittext , i am using this edittext. this solved my issue.
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getAction()!=KeyEvent.ACTION_DOWN){
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
dispatchKeyEvent(event);
//do whatever you want to do here, when keypad dismiss on EditText
return false;
}
}
return super.onKeyPreIme(keyCode, event);
}

Related

how to check if edittext is single line android

I am making a custom android keyboard. I want to make it sure that when user press enter key proper EditorAction took place. Therefore I want to know is there any way to check if edittext is single line or not? Or is there any way to check min and max lines of editext?
In your onStartInput(EditorInfo info) and onStartInputView(EditorInfo info) callbacks EditorInfo is passed in.
To check if it's a multiline you can do:
if (0 != (info.inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE)) {
//This is multiline text field
}
See TYPE_TEXT_FLAG_MULTI_LINE
You can implement it by setting a key listener and and checking getMaxLines() and getMinLines() properties. Note that it is applied for API > 15!
EditText editText = new EditText(this);
editText.setOnKeyListener(new View.OnKeyListener()
{
#Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent)
{
if (view instanceof EditText && ((EditText) view).getMaxLines() == 1 && ((EditText) view).getMinLines() == 1)
{
// this is a single line EditText view
}
return false;
}
});

XNA Touch Location

I am making a bubble wrap game. The kind where you press the bubbles to pop them and they unpop after a little bit. it all works fine, the bubbles pop and unpop as planned. There is one issue though. Whenever I click and drag the bubbles still pop. Is there a way to get them not to pop when i click and drag? Here is my code for the touch location.
foreach (TouchLocation tl in touches)
{
if (tl.State == TouchLocationState.Pressed)
{
if (rectangle.Contains((int)tl.Position.X, (int)tl.Position.Y))
{
popLocationX = (int)tl.Position.X;
popLocationY = (int)tl.Position.Y;
}
}
}
Here is what I do, I use this on my main menu but should be able to transfer over.
Have these added in:
MouseState mouse;
public float mouseDelay = 0.01f;
public float mouseTime = 0.0f;
public bool mouseActive = true;
public bool mouseUsed = false;
public string mouseOn = "None";
public Vector2 mouseLocation;
mouse helps find the location of the mouse.
mouseDelay will stop the the repeating when you have the mouse in the pressed position.
mouseTime will say hey, if I am greater than mouseDelay mouse can be used again.
mouseActive will say if it is pressed or not. If mouse is pressed mouseActive is false.
mouseUsed will see if it is ever used. If the mouse is not clicking on anything but the background then it will be false. If true that means it was used to do something.
mouseOn is used in my main menu to say you pressing on options? Make mouseOn = "Options";and go to it.
The mouseLocation holds where the mouse is.
All this in mind putting this all together will help you with your goal of not having the mouse used when it is already pressed.
In update:
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
mouse = Mouse.GetState();
if (this.IsMouseVisible == true)
{
MouseActive(gameTime);
}
base.Update(gameTime);
}
I have if mouse is visible to stop the process from repeating when the mouse is not there (less processing power).
Remember this was made for a main menu if you want to make this for a game you need to mess around with it a little.
void MouseActive(GameTime gameTime)
{
mouseTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
mouse = Mouse.GetState();
mouseLocation = new Vector2(mouse.X, mouse.Y);
switch (gameState)
{
case GameStates.TitleScreen:
break;
case GameStates.Options:
break;
case GameStates.Credits:
break;
}
if (mouseOn != "None")
{
mouseUsed = true;
switch (gameState)
{
case GameStates.TitleScreen:
if (mouseOn == "Play")
{
}
if (mouseOn == "Quit")
this.Exit();
if (mouseOn == "Options")
gameState = GameStates.Options;
if (mouseOn == "Title")
{
}
break;
case GameStates.Options:
break;
case GameStates.Credits:
break;
}
mouseOn = "None";
}
if (mouse.LeftButton == ButtonState.Pressed)
{
mouseTime = 0.0f;
mouseActive = false;
}
if (mouse.LeftButton == ButtonState.Released && mouseTime > mouseDelay)
{
mouseActive = true;
mouseUsed = false;
}
}
All the code from above is going to be in the main class
Here is a class named TitleScreen.cs were I use the images on the TitlesScreen and the mouse from the main class:
You first need to Initialize the thing so add this method in TitleScreen:
Game1 game1;
public void Initialize(Game1 game1)
{
this.game1 = game1;
}
You need to add in an Initializer to the Main class(Game1.cs)
TitleScreen titlescreen;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
titlescreen = new TitleScreen();
}
protected override void Initialize()
{
titlescreen.Initialize(this);
base.Initialize();
}
Then for the final step you add Update to the TitleScreen:
public void Update(GameTime gameTime)
{
MouseState mouse = Mouse.GetState();
if (mouse.LeftButton == ButtonState.Pressed && game1.mouseUsed == false && game1.mouseActive == true)
{
if (play.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Play";
game1.mouseUsed = true;
}
else if (title.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Title";
game1.mouseUsed = true;
}
else if (options.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Options";
game1.mouseUsed = true;
}
else if (quit.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Quit";
game1.mouseUsed = true;
}
}
}
You also need to add this to the Update in the main class
titlescreen.Update(gameTime);
I've not worked with touch yet, but the logic should be similar across all input methods.
I would create a variable to store the old TouchLocationState. That way, you can check if the user is clicking and dragging. For example:
//TODO: instantiate this
TouchLocationState oldTLState;
...
// This condition will only be true when the user clicks once, dragging will return false as the oldTLSTate is not released
if (tl.State == TouchLocationState.Pressed && oldTLState.State == TouchLocationState.Released)
{
if (rectangle.Contains((int)tl.Position.X, (int)tl.Position.Y))
{
popLocationX = (int)tl.Position.X;
popLocationY = (int)tl.Position.Y;
}
}
//At the end of the Update() method, update oldTLState with the current state
oldTLState = tl;

number doesn't show in editText field when entered (Android 2.2)

I have two editText fields, with following TextWatchers in their own addTextChangedListeners. Input is read and handled correctly but the number I enter is simply not shown on the display in the second field (editText2) (even though I wait (sleep() in afterTextChanged()) a while before proceeding with setting the values of both fields to null end setting focus to the first field.
What happens is on entering a number in the first field: number is diplayed in the field and focus is moved to the second field. What happens on entering a number in the second field: cursor (blinking vertical line) is frozen, no number is shown, after two seconds: cursor is moved to first field and both fields all empty. All of this is meant to happen except that the number entered in the second field should show and then the system should be frozen showing that number for a while before setting to null and moving on to the first field.
What is wrong and how to solve?
public class Spel extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spel);
editText1 = (EditText) findViewById(R.id.editText1);
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (before==0) { // alleen doen als editText1 leeg was
String v = s.toString();
if (v.equals("0") || v.equals("1") || v.equals("2") || v.equals("3") || v.equals("4") || v.equals("5") || v.equals("6") || v.equals("7") || v.equals("8") || v.equals("9")) {
editText2.requestFocus();
int baanWorpScore = Integer.parseInt(v);
banenWorpScore[0] = baanWorpScore;
}
else {
// blijf wachten op goede invoer
editText1.setText(null);
}
}
}
});
editText2 = (EditText) findViewById(R.id.editText2);
editText2.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (before==0) {
String v = s.toString();
if (v.equals("0") || v.equals("1") || v.equals("2") || v.equals("3") || v.equals("4") || v.equals("5") || v.equals("6") || v.equals("7") || v.equals("8") || v.equals("9")) {
editText1.requestFocus();
editText1.setText(null);
editText2.setText(null);
int baanWorpScore = Integer.parseInt(v);
banenWorpScore[1] = baanWorpScore;
}
else {
// blijf wachten op goede invoer
editText2.setText(null);
}
}
});
}
Found following solution, putting the delayed operations in a runnable, passed to method postDelayed of a (new) handler. This does exactly what I wished. Refer here for a post that helped me find where to look for the solution.
(I actually don't know if method removeCallbacks must actually be called)
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
Runnable mFilterTask = new Runnable() {
#Override
public void run() {
editText1.setText(null);
editText2.setText(null);
editText1.requestFocus();
}
};
Handler mHandler = new Handler();
if (before==0) {
String v = s.toString();
if (v.equals("0") || v.equals("1") || v.equals("2") || v.equals("3") || v.equals("4") || v.equals("5") || v.equals("6") || v.equals("7") || v.equals("8") || v.equals("9")) {
mHandler.removeCallbacks(mFilterTask);
mHandler.postDelayed(mFilterTask, 2000);
int baanWorpScore = Integer.parseInt(v);
banenWorpScore[1] = baanWorpScore;
}
}
}

How to get text through key press in Blackberry

I need to get the text of the keys at the event of key press in Blackberry. This happens when the user presses a key from the the keypad in order to type text. How is that possible?
you can get the pressed key text by overriding keyChar like this
public boolean keyChar(char key, int status, int time)
{
if (key == Characters.ESCAPE)
{
int result = Dialog.ask(Dialog.D_YES_NO,"Are you sure you want to exit?");
if (result == Dialog.YES) {
closePopup();
}
return(true);
}
else
if (key == Characters.ENTER)
{
processLocation();
return(true);
}
else
{
//the pressed key is key
return(super.keyChar(key,status,time));
}
}
This helps you:
protected boolean keyChar(char ch, int status, int time)
{
if(ch == Characters.ESCAPE || ch == Characters.ENTER)
{
//Nothing to do;
}
else
{
pressedKey=pressedKey+ch;
}
return super.keyChar(ch, status, time);
}
Then you can get the values in pressedKey(it is a String variable you have to declare it first).

problem with back button

when i am pressing the back button a pop screen is displayed which shows three button save, discard and cancel button i don't want this screen to be popped up. is this possible.
Thanks in advance
The default behaviour of the back button is to save changes for dirty screens. Rewrite the onClose() method to overwrite the default behaviour.
public boolean onClose() {
int choice = Dialog.ask(Dialog.D_YES_NO, "¿Do you want to exit?", Dialog.YES);
if (choice == Dialog.YES) {
//write a close() routine to exit
close();
}
return true;
}
You return true because you managed the ESC button pressed event. Review the Screen class docs.
You can also change the default behaviour of the ESC button rewriting the keyChar method as follows:
protected boolean keyChar(char character, int status, int time) {
if (character == Keypad.KEY_ESCAPE) {
onClose();
return true;
}
return super.keyChar(character, status, time);
}
close() should be somenthing like:
public void close() {
System.exit(0);
}
Override the onSavePrompt method. Then that screen will not come. Actually that popup screen will come only when something is changed on your screen. So it will ask you for the appropriate action.
protected boolean onSavePrompt() {
return true;
}
Skip the saving prompt with it
protected boolean onSavePrompt() {
return false;
}
Override onClose() method like this:
public boolean onClose() {
close();
return true;
}
you will not get that annoying alert message.

Resources