Android. How to catch the moment when keyboard gets hidden? - java

I'm trying to catch the event of removing keyboard from the screen and i'm using the following code:
searchTextField.setOnEditorActionListener(new OnEditorActionListener()
{
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
System.out.println("ACTION ID " + actionId);
if(actionId == EditorInfo.IME_ACTION_DONE)
{
System.out.println("ACTION DONE!!!!!!!!!!");
return true;
}
return false;
}
searchTextField.setOnFocusChangeListener( new View.OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
System.out.println("HAS FOCUS");
else
System.out.println("FOCUS LOST");
}
});
But unfortunately it doesn't work. onEditorAction just never called, no matter if i start editing or finish. Regarding onFocusChange method it's called just for the very first time when the keyboard goes up. When the keyboard goes down or when it goes up for the second time it's not called. Can anyone explain what am i doing wrong?

I used GlobalLayoutListener on the activity rootView for checking that whether keyboard is hidden or visible:
It works as follows:
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
... do something here
}
}
});

Related

Add dollar sign to editText when field is clicked android

I would like to add a dollar sign ($) automatically when the editText field is clicked. I've attempted to use addTextChangedListener but it doesn't work as expected.
I do not want to add it after the use has finishes entering all the text. Just want it to be added once the field is clicked or becomes the focus.
EditText editTextAd;
editTextAd.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
editTextAd.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL){
keyDel = true;
}else{
keyDel = false;
}
return false;
}
});
if (!keyDel) {
String str = s.toString();
if (s.length() == 0) {
str += "$";
editTextAd.setText(str);
editTextAd.setSelection(str.length());
//keyDel=false;
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
setOnKeyListener is listening keys from Keyboard(hardware).
Change EditText.text in onTextChanged will cause a never-end loop which actually won't work.
when the editText field is clicked
The event is according to setOnClickListener or setOnTouchListener.
becomes the focus
The event is setOnFocusChangeListener.
Step - 1: First call edittext.setOnfocusChangeListner so that on user click on your editText it detect its focus.
Step - 2: Call edittext.setText("$"); which set $ sign in first position on editText.
Step - 3: Call editText.setSelection(1) so that your cursor will start at position 1 not 0.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
editText.setText("$");
editText.setSelection(1);
}
}
});

Android - OnTouchListener() is triggered too early

I have an activitiy where I have a button and when a click on this button I want to set a TextView with some value, so I used onClickListening and it is working:
ButtonPlus.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
ponts = ponts + 1;
resultadoTextView.setText(Integer.toString(ponts));
}
});
But the problem is that I want to keep increasing this textView's value while the button keep being pressed so I tried to use the OnTouchLister:
ButtonPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ponts = ponts + 1;
resultTextView.setText(Integer.toString(ponts));
}
});
the problem is that when I give a fast click in the button it increments the TextView's value too much and I want the onTouchListener to be activated just after some time that the button was pressed.
any help please?
Use some additional counter.
int additionalCounter = 0;
ButtonPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
++additionalCounter;
if (additionalCounter % X == 0) {
ponts = ponts + 1;
resultTextView.setText(Integer.toString(ponts));
}
}
});
You can set X as you want, i.e. setting it to 5 would make touch event working 5 times slower.
Try this code.
ButtonPlus.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ponts = ponts + 1;
resultTextView.setText(Integer.toString(ponts));
ButtonPlus.setClickable(false);
//wait 1 second
ButtonPlus.postDelayed(new Runnable() {
#Override
public void run() {
ButtonPlus.setClickable(true);
}
}, 1000);
return false;
}
});

Start and stop recording with button click

I'm making a simple android app for recording sound. I have a startRecording() and stopRecording() methods. Now I implemented a ToggleButton named "Touch to record" so as you can already imagine, when the button is checked, you have to hold the record button to record sound and when the button is on "off" you have to click to start and then click to stop.
This is the current code:
touchToRecord.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked)
{
recBtn.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
chTimer.start();
chTimer.setTextColor(Color.GREEN);
startRecording();
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
chTimer.stop();
stopRecording();
nameAlert();
}
return true;
}
});
}
else
{
//onClickListener
}
}
});
Now I'm not sure how to make the onClickListener. If I try to do it like this:
recBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
It won't work because it underlines setOnClickListener and says:
The method setOnClickListener (View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})
Also, one more thing after I get this working; how do I check if the method is already running with if statement? I want to do something like this:
if (startRecording == isRunning)
{
stopRecording();
}
You could try android toggle button. Please see more information on:
http://developer.android.com/guide/topics/ui/controls/togglebutton.html
Solved the problem!
I avoided using onClickListener by setting onClick in XML and then just creating the method. Here's the code:
public void recordBtnClick(View v){
final ToggleButton touchToRecord = (ToggleButton)findViewById(R.id.tBtn1);
final ImageButton recBtn = (ImageButton) findViewById(com.whizzappseasyvoicenotepad.R.id.recButton);
if (touchToRecord.isChecked() == false)
{
if (recorder == null)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
chTimer.start();
chTimer.setTextColor(Color.GREEN);
startRecording();
}
else if (recorder != null)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
chTimer.stop();
stopRecording();
nameAlert();
}
}
else
{
//DO NOTHING
}
}

Android: Code to catch every possible type of "Enter" key (hard and soft)?

Good day!
I have an Android app (v2.3.3 and up) that allows you to search via an EditText control. I'm using the following code to detect when the user is done and either presses a hardkey or a softkey to perform the search:
EditText editText = (EditText)findViewById(R.id.search_box);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
(event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
{
performSearch();
return true;
}
return false;
}
});
This, apparently, does not catch EVERY possible way of expressing "Enter" as a beta tester just told me the "Enter" key on their HTC Evo soft keyboard doesn't do anything. The search hardkey (magnifying glass) works, but this code isn't catching the event from the soft keyboard.
Reading some posts, most answers to this type of question contain some, or all, of the logic above. I've also read that HTC does some keyboard stuff differently.
Does anyone have some bullet-proof code for detecting "Enter" input on ANY device?
Thank you!
#Override
public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
performSearch();
return true;
}
return false;
}
I already had this same problem. Remove the check for the actionId, just check for event.getAction() == KeyEvent.ACTION_DOWN in your if statement:
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == KeyEvent.ACTION_DOWN)
{
performSearch();
return true;
}
return false;
}
I think textchangelistener can capture enter key, try this
commentt.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
// if enter is added to your CharSequence
// {
// do search
// }
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});

Android custom double tap location

I'm currently creating a custom double tap using the onClickListener with the following code:
newImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
counterTap++;
Timer t = new Timer("Double tap counter");
t.schedule(new TimerTask() {
#Override
public void run() {
counterTap = 0;
}
}, 0, 300);
if(counterTap >= 2) {
newImage.setVisibility(FrameLayout.GONE);
counterTap = 0;
}
}
});
The problem I'm facing is as follows:
Whenever I tap the ImageView the event does fire. However, the second time I tap the ImageView, the above code only executes when clicking on the exact same position on the ImageView as before.
Rather use a onTouchListener. In the touch listener in the onTouch method you can return false the first time you tapped which means the event was not handled, the second time you return true and the touch listener will handle the event as finished. See my example below, you can use a similar method
new OnTouchListener() {
boolean doubleTap = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (!doubleTap) {
doubleTap = true;
return false;
} else {
return true;
}
}
}
};
this might solve your issue.
EDIT : This is a better opotion
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDown(MotionEvent e) {
return true;
}
// double tap event
#Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}
}
This question might also help you get to the best answer

Categories

Resources