Displaying text without a button - java

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

Related

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

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

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

Using keypress in android

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

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