How to calculate the value of two edittexts without a button? - java

I'm new to android studio. I'm working on a simple project, where the user puts two inputs in two edittext fields. There is a calculation to be made with those inputs, and i need the tesult to be shown automaticaly in another text view, without making the user click a button. How can i do that?

for this purpose, you can use TextWatcher and listen for text changes on edittexts
editText.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) {
// do whatever you want
}
#Override
public void afterTextChanged(Editable editable) {
}
});

Related

What is the equivalent of OnInputListener in Android

I have an editText in android and i'ld like to listen for when an input or text has been added.
i am setting the text dynamically and i want a situation where once that is done, a new event is triggered.
code.setText(savedcode);
code.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) {
Toast.makeText(Verification.this, "New Input" , Toast.LENGTH_SHORT ).show();
}
#Override
public void afterTextChanged(Editable editable) {
}
});
i've used the text watcher but it doesn't trigger event when input is set dynamically unless i edit the field then it does. i know in javascript, on input listener can do that so i'm looking for its android equivalent. Thank you
Well, the thing is that your code seems to be correct, the one thing that is done wrong is the order.
Practically,you want to know when the textView's text has changed,but you change it before adding a text to it.
So the logical way to go is this:
TextView TV;
TV = (TextView) rootView.findViewById(R.id.theID);
TV.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) {
Toast.makeText(Verification.this, "New Input" , Toast.LENGTH_SHORT ).show();
}
#Override
public void afterTextChanged(Editable editable) {
}
});
TV.setText(savedcode);
This way, you change the text once you actually have a listener there, not before.That should fix your problem.
P.S: I am not sure how you declare your textView, don't mind the variables name, no inspiration at this moment.

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

I want to make something when done from typing EditText

what is the method I should use to type what will happen after being finished from typing in the EditText?
Please use simple statements and use an example with comments of what happens in that line and what to type in that line.
PS
I wish you got the lines above!!!
you need to add a text watcher to the text view
Example:
myEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Your query to fetch Data
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
// Your logic here
}
});
just add a texcchangedlistener with textwatcher to your edittext
https://developer.android.com/reference/android/text/TextWatcher.html
myeditext.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s)
{
//do something here!!!
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
}
});

Play sound when typing in EditText in android application

Hi i am making an android application.
I want to play a default sound effect when i am typing in an EditText field.
Any ideas how to do that?
Thanks
you can use textwatcher for that purpose
youredittext.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) {
MediaPlayer mp = MediaPlayer.create(this, R.raw.yourmp3); // add mp3 file
mp.start();
}
});
Note : add yourmp3 in res->raw folder

Is it possible to use [TextWatcher for button disable control] for two EditText?

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

Categories