Send backspace key event to edit text - android

I don't know how to send a backspace key event to a EditText from my own button. Here is what i tried:
Button backSpace=(Button)findViewById(R.id.backSpace_tab);
backSpace.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
text.dispatchKeyEvent(new KeyEvent(KeyEvent.KEYCODE_DEL,KeyEvent.KEYCODE_P));
}
});

From the Android developer docs:
public KeyEvent (int action, int code)
Create a new key event.
Parameters
action Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
code The key code
The first parameter should be an action code. In your case you should use ACTION_DOWN, because you want to simulate a keypress:
public static final int ACTION_DOWN
getAction() value: the key has been pressed down.
So this should work:
#Override
public void onClick(View v)
{
text.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
}

Related

How to use EditText onClick method while remaining focused

I have an EditText that has a ListPopupWindow attached to it with recent search queries listed. When a user clicks the EditText, the popup should show, then when the user starts typing, the popup should disappear.
This is mostly working using OnFocusChangeListener, OnTextChangedListener, and OnEditorActionListener. However, if a user has clicked on the EditText, began to type, then clicks the EditText again, I need the popup to come back up. I have tried using an OnClickListener instead of an OnFocusChangeListener but can never get the popup to show with an OnClickListener.
How can I get the ListPopUpWindow to show when the use clicks the EditText if it already has focus?
searchBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean gainFocus) {
//onFocus
if (gainFocus) {
popUpWindow.show();
}
}
});
To dismiss popup once user start typing in EditText field
searchBox.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) {
}
#Override
public void afterTextChanged(Editable s) {
popUpWindow.dismiss();
}
});
To clear focus, dismiss popup, and execute search
searchBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId & v.getImeOptions()) == actionId) {
if (event != null && event.getAction() == KeyEvent.ACTION_DOWN) {
if (searchBox.getText().toString().length() == 0) return true;
searchBox.clearFocus();
popUpWindow.dismiss();
fetchResults();
Util.hideKeyboard(v);
return true;
}
}
return false;
}
});
You can try replacing the focus change listener with an on touch listener and showing the popup when a MOTION_UP event occurs:
editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if ((event.getAction() == MotionEvent.ACTION_UP) {
// Show popup here
}
return false;
}
});
I found the problem. It turns out I wasn't actually using an EditText object, but rather a custom class called MaterialEditText that provides a wrapper around an EditText object to give it Material UI features. The class passes the listeners I had been using onto it's EditText member, however, it was not doing so for either OnClickListener or OnTouchListener, so these listeners were never getting set for the EditText object. Once I added methods to pass these listeners on to the EditText object, I fixed my issue by keeping the code listed above and adding the following:
searchBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Show Popup
}
});

How do I remove numbers on phonepad if I press backspace button

I am trying to make a a phonepad in android and I need to know how I make my backspace button work when I press it, it remove my last number and so on.
b13.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int len=text.length();
text.setText(text.getText().toString());
String res=text.substring(0,len-1);
}
});
b13 is button id for remove button
You're on the right track. Try this:
#Override
public void onClick(View view) {
String newText = myTextView.getText().toString()
.substring(0, myTextView.getText.toString.length()-1);
myTextView.setText(newText);
}
Take a look at KeyEvent, there is a constant called KEYCODE_BACK that should achieve this behavior

Reload the same activity when press Home

I have an application. I tried to disable home button. Many people said that it doesn't possible to do in android 4.0 and above. So i decided to reload the same activity when press home button. I followed the below code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_HOME) {
System.out.println("==============================");
Intent i = new Intent(getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true;
}
return false;
}
I can't get any response when press home button. Can you tell me what's my wrong?
This key cannot be intercepted, so KEYCODE_HOME won't be send to you.
Its impossible to override the home button.
public static final int KEYCODE_HOME
Added in API level 1
Key code constant: Home key. This key is handled by the framework and is never delivered to applications.
Source:
http://developer.android.com/reference/android/view/KeyEvent.html
This is a refresh button method, but it works well in my application. in finish() you kill the instances
refresh = (Button)findViewById(R.id.refresh);
refresh.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onRestart();
}
});
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Intent i = new Intent(lala.this, lala.class); //your class
startActivity(i);
finish();
}

clicklistener and longclicklistener on the same button?

i am creating a call/dial button, when i click on that call/dial button, a call will be made based on the input that is displayed in the edittext. I managed to do that part. can you guys advise me whether i can do a longer click on that same call/dial button, so that a toast can come out to ask user to choose something else??
I did some research on "setOnLongClickListener" but i am not sure if i can combine it in the same call/dial button? I have attached on the working dial function which i managed to do, wondering if the "setOnLongClickListener" can be combined together somehere in the code?
private void dialANumber() {
try {
buttonCall = (ImageButton) findViewById(R.id.imageButton2);
buttonCall.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (display != null) {
Intent callNumber = new Intent();
callNumber
.setAction(android.content.Intent.ACTION_CALL);
callNumber.setData(Uri.parse("tel:" + display.getText()));
startActivity(callNumber);
}
}
});
} catch (ActivityNotFoundException anfe) {
Log.e("DialANumber", "Dialing the number failed", anfe);
}
this code is working. i hope a longer click can be made on the same call/dial button so the button can have a normal click to make a call, and longer click to pop out a toast. Thanks in advance.
Note that returning "false" on the long click listener will have the UI responding to the long click as a short click too. Return "true" if you want to kill that off. "True" means "yes, I used this event" and "false" means "whether I used it or not, the environment is free to respond as well." (I know this because I just used AkashG's answer in my own app.)
A GestureDetector with a SimpleOnGestureListener would help you differentiate between the different types of presses. A GestureDectector is a class which can read different types of touch events (for example, single taps and long presses), and sends them to a listener which handles each type differently. Here's the documentation on the Detector and Listener.
http://developer.android.com/reference/android/view/GestureDetector.html
http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html
First, set up your SimpleOnGestureListener, the important methods for you to override will be onSingleTapUp and onLongPress. In your onCreate, create an instance of GestureDetector that references your listener. Then, attach an OnTouchListener to your button that sends the event to your detector. You'll want it to look something like this:
//Set up the Detector
GestureDetector.SimpleOnGestureListener myGestureListener = new GestureDetector.SimpleOnGestureListener()
{
#Override
public boolean onSingleTapUp(MotionEvent e)
{
//Your onClick code
return false;
}
#Override
public void onLongPress(MotionEvent e)
{
//Your LongPress code
super.onLongPress(e);
}
};
//And make a variable for your GestureDetector
GestureDetector myGestureDetector;
...
#Override
onCreate(Bundle b)
{
...
myGestureDetector = new GestureDetector(myActivity.this, myGestureListener);
...
}
...
//And finally, wherever you are setting up your button
button.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
myGestureDetector.onTouchEvent(motionEvent);
return false;
}
There a a bunch of other types of events this class can interpret in case you want to get even more fancy. GestureDetector is a very good class to do a little research on, it can be very useful and isn't too complex. Hope this helps.
Yes you can do this:
XML file:
<Button
android:id="#+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALL"/>
<ImageButton
android:id="#+id/callBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
For button click event:
Button button=(Button) findViewById(R.id.call);
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(), "Long CLick", Toast.LENGTH_SHORT).show();
return false;
}
});
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (display != null) {
Intent callNumber = new Intent();
callNumber
.setAction(android.content.Intent.ACTION_CALL);
callNumber.setData(Uri.parse("tel:" + display.getText()));
startActivity(callNumber);
}
}
});
For imageButton:
ImageButton imageButton=(ImageButton) findViewById(R.id.callBtn);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(check==false){
Toast.makeText(getBaseContext(), "CLick", Toast.LENGTH_SHORT).show();
}
imageButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
check=true;
if(check){
Log.d("bool", check+"");
Toast.makeText(getBaseContext(), "Long CLick", Toast.LENGTH_SHORT).show();
check=false;
}
return false;
}
});
Declare this at the top(golbally):
boolean check=false;

How to prevent button onClick firing repeatly?

I have a Button which sets both onLongClickListener & onClickListener :
onLongClickListener :
#Override
public boolean onLongClick(View v) {
do something ...
return true;
}
onClickListener :
#Override
public void onClick(View v) {
do something else ...
}
When I long click the button, onLongClick fires repeatly
(sometimes onClick fires too when I release the button, it's weird ##")
What I want is to make the onLongClick be triggered only once for one long press.
So I modified the code :
onLongClickListener :
#Override
public boolean onLongClick(View v) {
do something ...
myButton.setLongClickable(false);
return true;
}
onClickListener :
#Override
public void onClick(View v) {
myButton.setLongClickable(true);
do something else ...
}
Unfortunately, the onClick callback was locked too after onLongClick fires!
I cant unlock the button anymore :|
Whats wrong with my code? Also, why onClick sometimes works when I release my button after a long click?
I've got the code you need, give me a minute to post it.
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//DO STUFF GRAH!
}
});
btn.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
//OTHER STUFF
return true;
}
});
This worked fine for me. I made an int and onLongClick added one and displayed it in a toast. Always incremented by one, and didn't do the onClick (reset it to 0).

Categories

Resources