Using keypress in android - java

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)
{
}
});
}
}

Related

Disable button upon removing textview content

public class ActivityMain extends AppCompatActivity {
Button button;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button_chatbox_send);
button.setEnabled(false);
tv= findViewById(R.id.edittext_chatbox);
tv.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {
if (charSequence.toString().equals("")) {
button.setEnabled(false);
} else {
button.setEnabled(true);
}
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
}
});
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
//Call other method with string from text view as parameter
}
}
});
}
I added a TextChangedListener to my TextView to disabled the Button, while the text view contains no string. During runtime, after I entered a string into the TextView, the Button is still enabled, even if I deleted all the text. How do I solve this problem, the method which I use during on click can not work with an empty string?
Edit
Problem is solved ty.
Update your button in onTextChanged or after textChanged
tv.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
button.setEnabled(!TextUtils.isEmpty(s.toString())); // Update button here OR
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
button.setEnabled(count>0); // Update button here
}
});
move it to afterTextChanged
#Override
public void afterTextChanged(Editable editable) {
button.setEnabled(!TextUtils.isEmpty(editable.toString()));
}
You should disable the button inside the onTextChanged method of the TextWatcher -
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
button.setEnabled(charSequence.toString().length() > 0);
}

Button not disabling if text missing in edittext

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.

Displaying text without a button

I am trying to display some text that the user inputs on a android app. However, all the examples that I can find online require you to hit a button before some text is displayed. Is it possible to display user input without a button?
Like mentioned #ligi you can use TextWatcher like this:
EditText editText;
TextView tv;
editText.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) {
tv.setText(s);
}
});
you can add a TextWatcher to your EditText

decrease a counter when a key is pressed (ANDROID)

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) {
}

EditText - change text while typing

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);
}

Categories