How can you count the number of keypress realtime?? or by keypress?
Here is my code:
public class MyActivity extends Activity implements View.OnClickListener, View.OnKeyListener {
/**
* Called when the activity is first created.
*/
EditText ed
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed=(EditText)findViewById(R.id.editText);
ed.setOnKeyListener(this);
}
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
keyCounter++;
Log.d("key","" + keyEvent.getKeyCode());
return super.onKeyUp(i, keyEvent) ;
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_B:
keyCounter++;
return true;
}
return super.onKeyUp(keyCode, event);
}
}
I tried both the onkeyup and on key.. But it didnt work;
I Toast the value of keyCounter but still zero. Anyone knows how to get the number of keypress in android?? Thanks.
Are you trying with a real keyboard or the software keyboard?
android.view.View.OnKeyListener
Class Overview
Interface definition for a callback to be invoked when a hardware key event is dispatched to this view.
see if you can use a TextChangedListener instead.
myEditText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do stuff
}
});
Other things:
1) use Log.d instead of Toast.
2) Try the keyCounter++; without conditions to see if the onKey is called.
3) change return false/true to return super.onKey(keyCode, event)
Related
i have to decrease a counter When a char is written , but my code is decreasing two chars Instead of one but Only When delete key is pressed , but if
if I press another key does not work
private Button send;
private TextView max;
private TextView msg;
int limit=140;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
max = (TextView) findViewById(R.id.max);
send = (Button) findViewById(R.id.send);
msg = (TextView) findViewById(R.id.msg);
msg.setOnKeyListener(this);
}
#Override
public void onClick(View view) {
}
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
max.setText(String.valueOf(limit--));
return false;
}
The problem is that onKey is called twice, once for down and one for up.
You can use another method such as onKeyDown:
onKeyDown documentation
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
Or use a filter using onKey.
onKey gets fired twice. Once when the key is pressed down, and once when it is released.
Or you can try:
onKeyDown()
onKeyUp()
KeyEvent.getAction().
In this java image, you can see that the person made both a released and a pressed method. This picture is for java, but the concept for android is the same. Use the methods I gave you in that numbered list.
Otherwise, right now, you are listening for two events.
On the right is key pressed, and the left is key released:
If this was helpful, please mark as best answer. If you need more help, let me know!
Okey i have found the answer,
first of all msg is not a TextView, is a EditText
and then we can use addTextChangeListener.
thanks all of you who responds
private Button send;
private TextView max;
private EditText msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
max = (TextView) findViewById(R.id.max);
send = (Button) findViewById(R.id.send);
msg = (EditText) findViewById(R.id.msg);
msg.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
max.setText(String.valueOf(140- (msg.getText().toString().length())));
if(msg.getText().toString().length()>=140){
send.setEnabled(false);
}else
send.setEnabled(true);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
#Override
public void onClick(View view) {
}
I have looked at many posts and similiar questions have been asked. But none of the solution is working. First of all I've tried using onKeyListener, but in many posts, it is stated that it does not work for soft keyboard. So I tried to use TextWatcher instead, but it still does not printout anything.
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{
private MainThread thread;
private EditText editText;
private Bitmap textBitmap;
public GamePanel(Context context){
super(context);
//Add callback to the surfaceview to intercept events
getHolder().addCallback(this);
//Make GamePanel focusable so it can handle events
setFocusable(true);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format,int width, int height){}
#Override
public void surfaceDestroyed(SurfaceHolder holder){}
#Override
public void surfaceCreated(SurfaceHolder holder){
editText = new EditText(getContext());
editText.setSingleLine(true);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setDrawingCacheEnabled(true);
editText.layout(0, 0, WIDTH - 200, 100);
editText.buildDrawingCache();
textBitmap = Bitmap.createBitmap(editText.getDrawingCache());
thread = new MainThread(getHolder(), this);
//Start the game loop
thread.setRunning(true);
thread.start();
/*editText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
Toast.makeText(getContext(), "ABCD", Toast.LENGTH_SHORT).show();
System.out.println("KEY PRESSED");
}
return true;
}
});*/
/*editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
System.out.println("KEY PRESSED");
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println("KEY PRESSED");
}
});*/
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
System.out.println("ABC");
return true;
}
return false;
}
});
}
#Override
public boolean onTouchEvent(MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_DOWN ){
editText.setFocusableInTouchMode(true);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
//imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
return true;
}
return super.onTouchEvent(event);
}
#Override
public void draw(Canvas canvas){
if (canvas != null) {
canvas.drawBitmap(textBitmap,50,50,null);
canvas.restoreToCount(savedState);
}
}
Does drawing EditText with canvas.drawBitmap have anything to do with those solutions not working? Or is there any mistakes on implementing them?
Any solutions are welcomed, need explanations if possible. Thanks!
EDIT : tried to use onEditorActionListener
First of all check
Did you include this line in your xml inside the EditText
android:singleLine="true"
if not, please add it first.
Next for e.g i wanna use Done button inside my soft keyboard, than here is what you can do.
<EditText
android:id="edName"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:singleLine="true" //this line to take input in single line (if you don't include this line, Done button will not take any actions)
android:imeOptions="actionDone"/> //this line to provide done button
Now to take the input from keyboard for done button, add this code after findViewById in your onCreate
//TODO softKey "Done" listener for keyboard
edName.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//Do what you want here
return true;
}
return false;
}
});
I want to execute a java method whenever the user presses a key on the onscreen keyboard, in android. For instance, the user opens up the keyboard and presses "T" (or any other key), the function is then called. If a new key is pressed, the function is called again, etc. etc.
I looked at other similar questions on the site, but none of them seemed to work. This is the code I'm currently using:
EditText chatbox = (EditText) findViewById(R.id.chatbox);
chatbox.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
is_typing();
Log.d("Key pressed", "A key was definitely pressed");
}
return true;
}
});
Try with "TextWatcher".Below is the sample code for the same
EditText editText = new EditText(this);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// do the stuff , you need to do
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
I'm using this code below, to make my button disabled until first input box received some numbers.
Problem is when I press the button before second input recieve numbers, then app is crashing.
First input var is firstEdittext and second is secondEditText
firstEditText.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) {
Btn.setEnabled(!(firstEditText.getText().toString().trim().isEmpty());
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
Is it possible to use this TextWatcher for both?
Or I need to use another method for make my button disable
The error is caused by trying to parse empty text from secondEditText. In this case, you need to set the TextWatcher to both EditTexts.
You can do it by defining a named TextWatcher in the class, then set both EditTexts to use this. You also need to fix the checking condition for enabling the button.
Inside onCreate():
TextWatcher tw = 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) {
Btn.setEnabled(!TextUtils.isEmpty(firstEditText.getText()) && !TextUtils.isEmpty(secondEditText.getText()));
}
#Override
public void afterTextChanged(Editable s) {
}
};
firstEditText.addTextChangedListener(tw);
secondEditText.addTextChangedListener(tw);
I'm making a search box. when user types down something in here, I'll show suggestions which take a bit amount of time. Considering the buffering, I'd like to show progressbar for users.
To make this feature, I made AsyncTask to show up progressbar and TextWatcher of search box to execute async task.
The problem is that simply, progressbar doesn't show up. As far as I guess, this is probably because TextWatcher doesn't update UI.
Is there any solution to help me out?
Below is the code I've made.
Any suggestions will do. Thanks.
mSearchEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
mProcessTask = new ProgressTask();
mProcessTask.execute();
"do something taking time here"
}
And below is the code for AsyncTask
private class ProgressTask extends AsyncTask <Void,Void,Void>{
#Override
protected void onPreExecute(){
super.onPreExecute();
mProgressBar.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... arg0) {
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPreExecute();
mProgressBar.setVisibility(View.GONE);
}
}
I've found the possible solution.
The code attached in the question starts working properly when I changed full screen mode to no titlebar mode.
mSearchEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
mProgressBar.setVisibility(View.GONE);
}
Why dont you use the progressbar in the textwatcher itself ...