This is my activity
String name =et.getText().toString();
String a = "a";
String a1 = "\u24B6";
String b = "b";
String b1 = "\u24B7";
String c = "c";
String c1 = "\u24B7";
String d = "d";
String d1 = "\u24B9";
String e = "d";
String e1 = "\u24BB";
name = name.replaceAll(a,a1);
name = name.replaceAll(b,b1);
name = name.replaceAll(c,c1);
name = name.replaceAll(d,d1);
name = name.replaceAll(e,e1);
tv.setText(name);
when write a or b or c in edittext show space in textview
what is solution?
Use a TextWatcher on your EditText.
et.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this);
// make changes to s here
et.addTextChangedListener(this);
}
});
Related
I am building a proto social network and I give the possibility to my users to Tag another user with the # , I'm using an autocomplete textview to show the dialog with the users # searched but I need to know when a user typed "#" and the letters following in the editext . I found this answer and it's exaclty what I need BUT I dont want to only get one character. I want the whole word to make a search in my database . Example, user types "#Jordan" in the middle of his paste text . I need to get the "#" and the "#Jordan " . How can I do it ?
Here s an example of my code
private final 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) {
if (!TextUtils.isEmpty(s) && start < s.length()) {
if (!mentionAdapter.isEmpty()) {
mentionAdapter.clear();
}
String lastWord = s.toString().substring(s.toString().lastIndexOf(" ") + 1);
if (lastWord != null){
if (lastWord.length() != 0) {
switch (lastWord.charAt(0)) {
case '#':
if (getAdapter() != hashtagAdapter) {
setAdapter(hashtagAdapter);
}
break;
case '#':
if (getAdapter() != mentionAdapter) {
setAdapter(mentionAdapter);
}
break;
}
}
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
I am aiming you are working on android java so here is the answer to your question
EditText editText = (EditText)findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
String capturedString = getText(s);
} catch (Exception e) {
e.printStackTrace();
}
}
});
this function will work whenever you tab spacebar "#Jordan " you will get string after '#' and before ' ' means you will get "Jordan" as a string
public String getText(String s) {
String startChar = "#";
String endChar = " ";
String output = getStringBetweenTwoChars(s, startChar, endChar);
System.out.println(output);
}
here is getStringBetweenTwoChars function
public String getStringBetweenTwoChars(String input, String startChar, String endChar) {
try {
int start = input.indexOf(startChar);
if (start != -1) {
int end = input.indexOf(endChar, start + startChar.length());
if (end != -1) {
return input.substring(start + startChar.length(), end);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return input;
}
You can do that by following code, If you want to #java from the string
**Hello this is #java the best programming language **
edt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable editable) {
String str = editable.toString();
String seperator = "#";
int seoPos = str.indexOf(seperator);
Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(str);
boolean found = matcher.find();
if (seoPos != -1 && !found){
Log.d("TextChanged0","current Char "+str.substring(seoPos-1+seperator.length()));
}
}
});
String last = s.toString().substring(s.toString().lastIndexOf(" ") + 1);
I've written a program that calculates the sum of 4 inputed values which are typed in by the user.
I want to limit the result to a maximum value of 6 (with any result >6 returning a value of "6").
For example if the sum of the 4 numbers is 10 then it will automatically be changed to 6.
The java code is:
EditText editText3;
EditText editText4;
EditText editText6;
EditText editText8;
TextView textViewResult;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.calculator);
editText3 = (EditText) findViewById(R.id.editText3);
editText4 = (EditText) findViewById(R.id.editText4);
editText6 = (EditText) findViewById(R.id.editText6);
editText8 = (EditText) findViewById(R.id.editText8);
textViewResult = (TextView) findViewById(R.id.textViewResult);
editText3.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(addNumbers());
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
editText4.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(addNumbers());
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
editText6.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(addNumbers());
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
editText8.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(addNumbers());
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
private String addNumbers() {
int number1;
int number2;
int number3;
int number4;
if(editText3.getText().toString() != "" && editText3.getText().length() > 0) {
number1 = Integer.parseInt(editText3.getText().toString());
} else {
number1 = 0;
}
if(editText4.getText().toString() != "" && editText4.getText().length() > 0) {
number2 = Integer.parseInt(editText4.getText().toString());
} else {
number2 = 0;
}
if(editText6.getText().toString() != "" && editText6.getText().length() > 0) {
number3 = Integer.parseInt(editText6.getText().toString());
} else {
number3 = 0;
}
if(editText8.getText().toString() != "" && editText8.getText().length() > 0) {
number4 = Integer.parseInt(editText8.getText().toString());
} else {
number4 = 0;
}
return Integer.toString((number1*2) + (number2*4) + (number3*2) + (number4*2));
}
}
change: "return Integer.toString((number1*2) + (number2*4) + (number3*2) + (number4*2));" to this:
int sum = (number1*2) + (number2*4) + (number3*2) + (number4*2);
if(sum>6) sum = 6;
return sum+"";
This is my activity when run activity is stopped.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et = (EditText) findViewById(R.id.et);
final TextView tv = (TextView) findViewById(R.id.textView8);
final String a = "a";
final String b = "b";
final String c = "c";
final String d = "d";
final String e = "e";
final String f = "f";
final String g = "g";
final String h = "h";
final String a1 = "\u24B6";
final String b1 = "\u24B7";
final String c1 = "\u24B7";
final String d1 = "\u24B9";
final String e1 = "\u24BB";
final String f1 = "\u24BB";
final String g1 = "\u24BD";
final String h1 = "\u24BD";
et.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
}
#Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
String name = et.getText().toString();
name = name.replaceAll(a,a1);
name = name.replaceAll(b, b1);
name = name.replaceAll(c, c1);
name = name.replaceAll(d, d1);
name = name.replaceAll(e, e1);
name = name.replaceAll(f, f1);
name = name.replaceAll(g, g1);
name = name.replaceAll(h,h1);
tv.setText(name.trim());
#Override
public void afterTextChanged(final Editable s) {
}
});
}
}
what is the solution?
Please write correct code because all code in stack overflow is mistake.
Please help me with this problem as it is very hard.
You can use the URLEncoder
String encodedURL = URLEncoder.encode(yourInputString, "UTF-8");
Then decode with URLDecoder as needed on wherever you will use it.
The app force shuts when i enter a number in edittext. I want the app to automatically calculate the BMI when values are entered without pressing on a button.
I know nothing about TextWatcher yet i researched and came up with this code. Its not working though. Whats wrong with it?
public class BodyMassIndex extends Activity implements View.OnClickListener,
TextWatcher {
double result;
EditText age, height, weight;
ToggleButton sex, cmin, kglb;
TextView tvResult;
double heightInt = 1;
double weightInt = 0;
int ageInt;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
initialize();
sex.setOnClickListener(this);
cmin.setOnClickListener(this);
kglb.setOnClickListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
age = (EditText) findViewById(R.id.etAge);
height = (EditText) findViewById(R.id.etHeight);
weight = (EditText) findViewById(R.id.etWeight);
sex = (ToggleButton) findViewById(R.id.tbSex);
cmin = (ToggleButton) findViewById(R.id.tbCMIN);
kglb = (ToggleButton) findViewById(R.id.tbKGLB);
tvResult = (TextView) findViewById(R.id.tvResult);
age.addTextChangedListener(this);
height.addTextChangedListener(this);
weight.addTextChangedListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.tbCMIN:
heightInt = heightInt * 0.0254;
break;
case R.id.tbKGLB:
weightInt = weightInt * 0.45359237;
break;
}
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
calculate();
}
private void calculate() {
// TODO Auto-generated method stub
Editable H = height.getText(), W = weight.getText();
if(H != null)
heightInt = Double.parseDouble(H.toString());
if (W != null)
weightInt = Double.parseDouble(W.toString());
result = weightInt / (heightInt * heightInt);
String textResult = "Your BMI is " + result;
tvResult.setText(textResult);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
}
'
From http://developer.android.com/reference/android/text/TextWatcher.html about afterTextChanged method: "but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively....you can use setSpan(Object, int, int, int) in onTextChanged(CharSequence, int, int, int) "
you already have :
height = (EditText) findViewById(R.id.etHeight);
weight = (EditText) findViewById(R.id.etWeight);
only change calculate() method:
private void calculate()
{
//remove the Editable thing declaration...
heightInt = Double.parseDouble(height.getText().toString());
wightInt = Double.parseDouble(weight.getText().toString());
result = weightInt / (heightInt * heightInt);
String textResult = "Your BMI is " + result;
tvResult.setText(textResult);
}
P.S: Here we're assuming that EditText contains only a number and not letters. Letters will force close the app. So you'll have to check for that.
Currently I am using my onEditTextchanger. To format my currency. What I am wondering if it is possible to call and run the Calculate function from my main java whenever the user types in the edittext field .
I would like it to work similar the the javascript function onkeyup="calc(this.form)"
Since I am not sure how to implement this. Any ideas would be appreciated. Once Again Thanks in advance for your help.
class
public class CurrencyTextWatcher implements TextWatcher {
boolean mEditing;
public CurrencyTextWatcher() {
mEditing = false;
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(!mEditing) {
mEditing = true;
String digits = s.toString().replaceAll("\\D", "");
NumberFormat nf = NumberFormat.getCurrencyInstance();
try{
String formatted = nf.format(Double.parseDouble(digits)/100);
s.replace(0, s.length(), formatted);
} catch (NumberFormatException nfe) {
s.clear();
}
mEditing = false;
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
}
Java
public class CalcTestActivity extends Activity {
private EditText txta;
private EditText txtb;
private TextView txtc;
private TextView txtd;
private double a = 0;
private double b = 0;
private double c = 0;
private double d = 0;
private Button buttonCalc;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
txta.addTextChangedListener(new CurrencyTextWatcher());
txtb.addTextChangedListener(new CurrencyTextWatcher());
}
private void initControls() {
txta = (EditText)findViewById(R.id.txta);
txtb = (EditText)findViewById(R.id.txtb);
txtc = (TextView)findViewById(R.id.txtc);
txtd = (TextView)findViewById(R.id.txtd);
buttonCalc = (Button)findViewById(R.id.buttonCalc);
buttonCalc.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {calculate(); }});}
private void calculate() {
//textViewResult.setText(addNumbers());
a=Double.parseDouble(txta.getText().toString().replace("$", "").replace(",", ""));
b=Double.parseDouble(txtb.getText().toString().replace("$", "").replace(",", ""));
c=Math.round(a*.88);
txtc.setText(GlobalMoney.FormatValue(c));
d=Math.round((a*.87)+(b*.61)*(c*.25));
txtd.setText(GlobalMoney.FormatValue(d));
}
}
You can call it right in afterTextChanged(), is there any problem you bump into when doing so?