Hi All I have followed the following example http://www.google.com/codesearch#search/&q=NumberFormattingTextWatcher&exact_package=android&type=cs
I have CurrencyTextWatcher as a seperate class. I need this as I will be applying to several pages.
I can’t figure out why, but if I use setContentView(text) it will work as only 1 big text box, then I can’t see the rest of my xml .
If I use setContentView(R.layout.main); my xml works properly except for the TextWatcher wont fire for my txta EditText box
Java
public class CalcTestActivity extends Activity {
private EditText txta;
private TextView txtb;
private TextView txtc;
private EditText text;
private double a = 0;
private double b = 0;
private double c = 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();
text = new EditText(this);
text.addTextChangedListener(new CurrencyTextWatcher());
//setContentView(text);
}
private String FormatValue(double value)
{
NumberFormat nf = NumberFormat.getInstance();
return "$ "+ nf.format(value);
}
private void initControls() {
txta = (EditText)findViewById(R.id.txta);
txtb = (TextView)findViewById(R.id.txtb);
txtc = (TextView)findViewById(R.id.txtc);
buttonCalc = (Button)findViewById(R.id.buttonCalc);
buttonCalc.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {calculate(); }
private void calculate() {
a=Double.parseDouble(txta.getText().toString());
b=Math.round(a*.88);
txtb.setText(FormatValue(b));
c=Math.round((a*.87)-(b*.28));
txtc.setText(FormatValue(c));
}
});
}
}
CurrencyTextWatcher 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
}
}
XML
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/txta"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:numeric="integer"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtb"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Answer is"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:hint="0" />
<Button
android:id="@+id/buttonCalc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate" />
</LinearLayout>
I took your code. I observed that the code you have shared here is getting all views from xml.
In this case you are calling
in your onCreate method, wherein text is done using java. You wont get a call back for your onTextChanged, beforeTextChanged or afterTextChanged because all your views are taken from xml. So please after your
in onCreate() add below line
and comment
that line is not needed. I have verified its working fine.
if works vote and accept the answer