Back to project page divider.
The source code is released under:
Apache License
If you think the Android project divider listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package se.urvancevav.divider; /*//from w ww . ja va 2 s . c om * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.app.DialogFragment; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.text.NumberFormat; import java.util.Currency; import java.util.Locale; import de.greenrobot.event.EventBus; public class AddNewItemDialog extends DialogFragment implements View.OnClickListener { private EditText paidText; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getDialog(); View v = inflater.inflate(R.layout.add_new_item_dialog, null); v.findViewById(R.id.saveButton).setOnClickListener(this); paidText = (EditText) v.findViewById(R.id.itemPaidText); paidText.addTextChangedListener(new TextWatcher() { private String current = ""; @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 (!s.toString().equals(current)) { paidText.removeTextChangedListener(this); String cleanString = s.toString().replaceAll("[^\\d.]", ""); BigDecimal parsed = new BigDecimal(cleanString); BigDecimal divisor = new BigDecimal(Math.pow(10, Currency.getInstance(Locale.getDefault()).getDefaultFractionDigits())); parsed = parsed.divide(divisor, Currency.getInstance(Locale.getDefault()).getDefaultFractionDigits(), RoundingMode.HALF_EVEN); // A little bit of magic to omit currency symbol DecimalFormat formatter = (DecimalFormat) NumberFormat.getCurrencyInstance(); DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols(); symbols.setCurrencySymbol(""); formatter.setDecimalFormatSymbols(symbols); String formatted = formatter.format(parsed).replaceAll("\\s*", ""); current = formatted; paidText.setText(formatted); paidText.setSelection(formatted.length()); paidText.addTextChangedListener(this); } } @Override public void afterTextChanged(Editable s) { } }); return v; } @Override public void onClick(View v) { EventBus.getDefault().post(new AddNewItemEvent()); dismiss(); } }