I am having issues with addTextChangedListener. I have 2 edittext (edittext1 and edittext2) that when the text (in this case numbers) are added or changed I need it to run a calculation and output it to a textview called resultstext.
I have found many examples and have modified them to do what I need them to do, but when I call the editText1.addTextChangedListener.inputTextwatcher; it tells me it cant find the symbol addTextChangedListener. Below is the code, how to straighten this out?
package com.example.MyProject;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.FocusFinder;
import android.view.Menu;
import android.widget.EditText;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private android.widget.EditText editText1;
private android.widget.EditText editText2;
private android.widget.TextView resultsText;
private void onItemSelected() {
editText1 = (EditText)findViewById(R.id.textView13);
editText2 = (EditText)findViewById(R.id.textView15);
resultsText = (android.widget.TextView)findViewById(R.id.textView16);
}
android.text.TextWatcher inputTextWatcher = new android.text.TextWatcher()
{
public void afterTextChanged(android.text.Editable s) {
calculateResult();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
};
editText1.addTextChangedListener.inputTextWatcher;
editText2.addTextChangedListener.inputTextWatcher;
private void calculateResult() throws NumberFormatException {
Editable editableValue1 = editText1.getText(),
editableValue2 = editText2.getText();
double value1 = 0.0,
value2 = 0.0,
result;
if (editableValue1 != null)
value1 = Double.parseDouble(editableValue1.toString());
if (editableValue2 != null)
value2 = Double.parseDouble(editableValue2.toString());
result = ((0.5 * value1) / 6.1) * value2;
resultsText.setText(String.valueOf(result));
}
}
Placement of some of your code was wrong. I have attempted to fix it. See if the following works for you:
package com.example.MyProject;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.FocusFinder;
import android.view.Menu;
import android.widget.EditText;
public class MainActivity extends Activity {
private android.widget.EditText editText1;
private android.widget.EditText editText2;
private android.widget.TextView resultsText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText)findViewById(R.id.textView13);
editText2 = (EditText)findViewById(R.id.textView15);
resultsText = (android.widget.TextView)findViewById(R.id.textView16);
android.text.TextWatcher inputTextWatcher = new android.text.TextWatcher() {
public void afterTextChanged(android.text.Editable s) {
calculateResult();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
};
editText1.addTextChangedListener(inputTextWatcher);
editText2.addTextChangedListener(inputTextWatcher);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void calculateResult() throws NumberFormatException {
Editable editableValue1 = editText1.getText();
Editable editableValue2 = editText2.getText();
double value1 = 0.0,
value2 = 0.0,
result;
if (editableValue1 != null)
value1 = Double.parseDouble(editableValue1.toString());
if (editableValue2 != null)
value2 = Double.parseDouble(editableValue2.toString());
result = ((0.5 * value1) / 6.1) * value2;
resultsText.setText(String.valueOf(result));
}
}
try to use
private TextWatcher watcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
//your code here
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
};
Then add watcher to your edittext
youredittext.addTextChangedListener(watcher);
Related
I want to Android:Disable Button on app launch enable only when Edit text is filled. help needed. thanks in advance. I am Android Beginner.
Tried using TextWatcher. app is running fine when i don't add this feature.
please suggest how i can implement the TextWatcher part. rest of the app is working fine.
here is the code:
package com.example.intentopennextscreen;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public TextView editText1;
public Button submit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//editText1.addTextChangedListener(ebert);
}
/*TextWatcher ebert = 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) {
String message = editText1.getText().toString().trim();
submit.setEnabled(!message.isEmpty());
}
#Override
public void afterTextChanged(Editable s) {
}
}; */
/* Called when the user taps the Send button */
public void sendMessage(View view) {
/* Creating instance of Intent object */
Intent intent = new Intent(this, DisplayMessageActivity.class);
/* finding the user-input provided in the editText as an object */
EditText editText1 = (EditText) findViewById(R.id.editText1);
/*Finding Button Element*/
Button submit = (Button) findViewById(R.id.button);
/* converting the user-input of editText as String */
String message = editText1.getText().toString().trim();
/* sending the message to other activity as Key-Value Pair */
intent.putExtra("Value", message);
/* starting the intent */
startActivity(intent);
}
}
You can move the button manipulation inside afterTextChanged
#Override
public void afterTextChanged(Editable s) {
submit.setEnabled(!s.isEmpty());
}
Try this below code,
Add this in your onCreate() method
submit.setEnabled(false);
editText1.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) {
if (editText1.getText().length() > 0) {
submit.setEnabled(true);
} else {
submit.setEnabled(false);
}
}
});
I have 3 editText fields of the type Number(Decimal). Two of which have onTextChanged listeners supposed to carry out mathematical functions when any integer is entered. My problem is the edittext fields with the listeners have the cursors stuck on the starting position i.e if i try to type 25, it ends up being 52.
CODE:
package com.example.Prototype;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class productsFragmentTab extends Fragment {
Button newProduct, clear;
Spinner productList;
EditText quantity, unit, total;
String invoice_id;
ShowAlert alert = new ShowAlert();
int unitcost, totalcost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.product_layout, container, false);
newProduct = (Button)rootView.findViewById(R.id.button4);
clear = (Button)rootView.findViewById(R.id.button5);
productList = (Spinner)rootView.findViewById(R.id.spinner2);
quantity = (EditText)rootView.findViewById(R.id.editText5);
unit = (EditText)rootView.findViewById(R.id.editText7);
total = (EditText)rootView.findViewById(R.id.editText8);
invoice_id = GlobalApp.data().id;
newProduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Save current records in array
//Clear all textboxes
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//clear all textboxes
}
});
unit.addTextChangedListener(new TextWatcher() {
boolean isChangingByCode = false;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (isChangingByCode) {
return;
}
String qty = quantity.getText().toString();
if (qty.matches("")) {
} else {
totalcost = Integer.parseInt(qty) * Integer.parseInt(unit.getText().toString());
isChangingByCode = true;
total.setText(Integer.toString(totalcost));
isChangingByCode = false;
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
total.addTextChangedListener(new TextWatcher() {
boolean isChangingByCode = false;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (isChangingByCode) {
return;
}
String qty = quantity.getText().toString();
if (qty.matches("")) {
} else {
unitcost = Integer.parseInt(total.getText().toString()) / Integer.parseInt(qty);
isChangingByCode = true;
unit.setText(Integer.toString(unitcost));
isChangingByCode = false;
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
return rootView;
}
}
Use setSelection() method to set cursor position at last.
Add following code:
total.setText(Integer.toString(totalcost));
total.setSelection(total.getText().length());
in place of total.setText(Integer.toString(totalcost)); in onTextChanged() callback.
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (isChangingByCode) {
return;
}
String qty = quantity.getText().toString();
if (qty.matches("")) {
} else {
totalcost = Integer.parseInt(qty) * Integer.parseInt(unit.getText().toString());
isChangingByCode = true;
total.setText(Integer.toString(totalcost));
total.setSelection(total.getText().length());
isChangingByCode = false;
}
}
i am building an app where if the user enters an "o" or "O" in the start of the edit text then the following should be executed:
replace the entered "o"or "O" with "P" at runtime in the EditText and set the TextView to "ORANGE".
Similarly, if the user enters "g" or "G" then the entered "g"or "G" should be replace with "P" at runtime in EditText and set the TextView to "Green".
and so on....
how can i accomplish this.I have use TextWatcher() but unable to achieve the goal.Help will be appreciated
package com.example.answer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class data extends Activity implements OnClickListener {
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
Button start, startFor;
EditText sendET;
TextView gotAnswer, result;
int flag = 1;
TextWatcher tt = null;
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
final EditText sendET = (EditText) findViewById(R.id.sendet);
final TextView result = (TextView) findViewById(R.id.tv);
tt = new TextWatcher() {
public void afterTextChanged(Editable s) {
sendET.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) {
if (sendET.getText().toString() == "o") {
flag = 0;
}
sendET.removeTextChangedListener(tt);
if (flag == 0) {
sendET.setText(sendET.getText().toString()
.replaceAll("o", "P"));
result.setText("ORANGE");
}
sendET.addTextChangedListener(tt);
}
};
sendET.addTextChangedListener(tt);
}
private void initialize() {
start = (Button) findViewById(R.id.button1);
sendET = (EditText) findViewById(R.id.sendet);
start.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
String bread = sendET.getText().toString();
result = (TextView) findViewById(R.id.tv);
result.setText(bread);
/*
* Bundle basket = new Bundle(); basket.putString("key", bread);
* Intent a = new Intent(data.this, openedclass.class);
* a.putExtras(basket); startActivity(a); break;
*/
}
}
}
if (sendET.getText().toString() == "o")
This is wrong. It's not how you check string equality in Java. Correct usage:
if(sendET.getText().toString().equals("o"))
Also, you remove your listener and add it again. I don't know your app logic, but I guess you don't want addTextChangedListener:
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (sendET.getText().toString() == "o") {
flag = 0;
}
sendET.removeTextChangedListener(tt); // REMOVING
if (flag == 0) {
sendET.setText(sendET.getText().toString()
.replaceAll("o", "P"));
result.setText("ORANGE");
}
sendET.addTextChangedListener(tt); // ADDING AGAIN
}
I'm making an app in which I'm using Spannble class to create a highlighter mechanism in an EditText, which works fine. Now, I want to store the span position(s) in a SQLiteDatabase, so that I can reload them and have the Spanned Text to show it in a TextView.
Here's my TextView alongwith the highlighter mechanism -
package com.Swap.RR;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.Spannable;
import android.text.TextWatcher;
import android.text.style.BackgroundColorSpan;
import android.view.Menu;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ToggleButton;
public class Add_New_Note extends Activity {
private EditText note;
private int mStart;
private Typeface Roboto_lt;
private int end;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__new__note);
note = (EditText) findViewById(R.id.notecontent);
ToggleButton highlighter = (ToggleButton) findViewById(R.id.highlightToggle);
Roboto_lt = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
note.setTypeface(Roboto_lt);
mStart = -1;
final TextWatcher highlightWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if(mStart > 0)
{
end = note.getText().length();
note.getText().setSpan(new BackgroundColorSpan(getResources().getColor(R.color.highlighter_green)), mStart, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
public void afterTextChanged(Editable s) {
}
};
highlighter.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (isChecked == true) {
//start highlighting when the ToggleButton is ON
if(mStart == -1) mStart = note.getText().length();
else mStart = -1;
mStart = note.getText().length();
note.addTextChangedListener(highlightWatcher);
} else {
//stop highlighting when the ToggleButton is OFF
note.removeTextChangedListener(highlightWatcher);
}
}
});
}
public void noteDone(View v) {
String noteContent = note.getText().toString();
Intent i = new Intent("com.Swap.Notes_page");
i.putExtra("NOTE", noteContent);
setResult(RESULT_OK, i);
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_add__new__note, menu);
return true;
}
}
Please help me with some suggestions on how to do it. Thanks!
Your best bet is to use Html.toHtml() to convert the Spannable into HTML. You can then use Html.fromHtml() to convert the HTML back into a Spannable when you query the database later on. You will want to do adequate testing, though, as toHtml() and fromHtml() are not guaranteed to support the same HTML tags.
I've followed this tutorial http://www.androidpeople.com/android-listview-searchbox-sort-items and I have the search working on the list. The only change I'd like to make is to have the list view hidden initially and the results only appearing when the user starts typing. Is there a way to do this?
EDIT: Added code.
package com.example.testapp;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private ListView lv1;
private EditText ed;
private String lv_arr[]= {"Android","Cupcake","Donut","Eclairs","AndroidPeople","Froyo",};
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.activity_main);
lv1=(ListView)findViewById(R.id.ListView);
ed=(EditText)findViewById(R.id.EditText01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
ed.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(lv1.getVisibility() != View.VISIBLE)
lv1.setVisibility(View.VISIBLE);
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength=ed.getText().length();
arr_sort.clear();
for(int i=0;i<lv_arr.length;i++)
{
if(textlength<=lv_arr[i].length())
{
if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength)))
{
arr_sort.add(lv_arr[i]);
}
}
}
lv1.setAdapter(new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1 , arr_sort));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
I would do it like this:
EditText etSearch = (EditText) getView().findViewById(R.id.etSearch);
etSearch.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
if(view.getVisibility() != View.Visible)
view.setVisiblity(View.Visible);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});