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) {
}
Related
i'm creating a narrative game, for that i have a conversation screen.
The user have to choose a predifined message to send it.
The choosen message is transfered inside the Edit Text but to send it, the user have to open the swift keyboard and click on "send".
I want to create a method that automatically send the message after the user choice. (Skip the manual "send" on the keyboard)
I tried TextWatcher :
EditText userInput;
RecyclerView recyclerView;
List<ResponseMessage> responseMessageList;
MessageAdapter messageAdapter;
String[] listChoices;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chapitretest);
Button btn = (Button) findViewById(R.id.btnChoice);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
listChoices = new String[]{"Mdr","Lol","xd"};
AlertDialog.Builder mBuilder= new AlertDialog.Builder(ChapTest.this);
mBuilder.setTitle("Ton choix:");
mBuilder.setSingleChoiceItems(listChoices, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
userInput.setText(listChoices[i]);
userInput.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) {
String txtbulle = userInput.getText().toString();
}
});
dialogInterface.dismiss();
}
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();
This is my first app, thank you ;)
Update: Why I use EditText : (Conversation method)
userInput = findViewById(R.id.UserInput);
recyclerView=findViewById(R.id.Conversation);
responseMessageList = new ArrayList<>();
messageAdapter=new MessageAdapter(responseMessageList,this);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
recyclerView.setAdapter(messageAdapter);
userInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_SEND){
ResponseMessage message = new ResponseMessage(userInput.getText().toString(),true);
responseMessageList.add(message);
ResponseMessage message2 = new ResponseMessage(userInput.getText().toString(),false);
responseMessageList.add(message2);
messageAdapter.notifyDataSetChanged();
if (!isLastVisible())
recyclerView.smoothScrollToPosition(messageAdapter.getItemCount() - 1);
}
return false;
}
});
}
Could anyone tell me why my button isn't disabled when the text in the edittext is empty? I've tried to do this so many ways but it never works! This is the simplified code I have at the minute.
Code:
public class MapStartActivity extends FragmentActivity {
EditText mapName;
Button NextPageStart;
private TextWatcher textWatcher = 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) {
checkFieldsForEmptyValues();
}
#Override
public void afterTextChanged(Editable s) {
}
};
private void checkFieldsForEmptyValues(){
String s1 = mapName.getText().toString();
if (s1.trim().isEmpty()) {
NextPageStart.setEnabled(true);
} else {
NextPageStart.setEnabled(false);
NextPageStart.setAlpha(.5f);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapName.addTextChangedListener(textWatcher);
}
});
}
}
Add text watcher outside click listener like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
// To disable the button intially
NextPageStart.setEnabled(false);
mapName.addTextChangedListener(textWatcher);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// You can do some click action here
}
});
}
In your code change these below lines only
#Override
public void afterTextChanged(Editable s) {
String textFromEditText = mapName.getText();
if(TextUtils.isEmpty(textFromEditText)){
NextPageStart.setEnabled(false);
} else{
NextPageStart.setEnabled(true);
}
}
I don't know why it's not working not for you, but I guess it should work fine. but you should learn about name convention first.
I am creating an android application, a converter. Every time I am going to press any number, I want it to be displayed automatically on a textfield. I don't know how to use the keypress in android application. Is it just like using keypress in a simple java program, let's say ran on netbeans?
Um ...
public class MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(new EditText());
}
}
Your question is not very clear.. but i think you are saying that your layout contain a EditText and TextView, when user type in a number in EditText at same time the TextView should also set the same text at rumtime. if this is your requirement then you can do it as follows:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.main_activity);
TextView tv = (TextView)findViewById(R.id.textview1);
EditText et = (EditText)findViewById(R.id.edittext1);
et.addTextChangedListener(new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
tv.setText(s);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void afterTextChanged(Editable s)
{
}
});
}
}
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)
I need to replace the text inside the EditText while typing :
Example : if the user pressed "A" it would be stored into a buffer and on the EditText "D" is displayed instead (looks like he pressed "D").
Now I can read the pressed character but I can't display any character in the et to avoid stackoverflow :
final EditText et = (EditText) findViewById(R.id.editTexts);
final TextView tv = (TextView) findViewById(R.id.textView2);
et.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) {
if(s.length() > 0) {
tv.setText(s.toString().substring(s.length()-1));
et.setText("");
}
}
});
You can change it as required::
public class SampleActivity extends Activity {
TextWatcher tt = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText et = (EditText) findViewById(R.id.editText1);
final TextView tv = (TextView) findViewById(R.id.textView1);
tt = new TextWatcher() {
public void afterTextChanged(Editable s){
et.setSelection(s.length());
}
public void beforeTextChanged(CharSequence s,int start,int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count) {
et.removeTextChangedListener(tt);
et.setText(et.getText().toString().replace("A", "C"));
et.addTextChangedListener(tt);
}
};
et.addTextChangedListener(tt);
}
}
In order to change the text interactively, you need to register a TextWatcher. But trying to change the text inside the watcher creates further calls to the watcher. My hack is to temporarily remove the watcher when I want to change the text, and re-register it right after
mEditText.addTextChangedListener(new TextWatcher() {
#Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
#Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
#Override public void afterTextChanged(Editable editable) {
mEditText.removeTextChangedListener(this);
mEditText.setText(//TODO change whatever you like here);
mEditText.setSelection(editable.length()); //moves the pointer to end
mEditText.addTextChangedListener(this);
}