Created
December 19, 2017 05:10
-
-
Save ChathuraHettiarachchi/1e87f7987f14fe55d29ad38bfe26b588 to your computer and use it in GitHub Desktop.
This is a simple text watcher to resolve "How to set thousands separator in Android?" quiz.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.chootdev.myapplication; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.util.Log; | |
import android.widget.EditText; | |
import java.text.DecimalFormat; | |
import java.text.NumberFormat; | |
import java.util.Locale; | |
/** | |
* Created by Choota on 12/8/17. | |
*/ | |
public class ThousandSeparatorWatcher implements TextWatcher { | |
private static ThousandSeparatorWatcher thousandSeparator; | |
private static String separatorThis = ","; | |
private static EditText editTextThis; | |
public static ThousandSeparatorWatcher init(EditText editText) { | |
editTextThis = editText; | |
if (thousandSeparator == null) | |
thousandSeparator = new ThousandSeparatorWatcher(); | |
return thousandSeparator; | |
} | |
@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 (editTextThis != null) { | |
editTextThis.removeTextChangedListener(this); | |
try { | |
String originalString = s.toString(); | |
Long longval; | |
if (originalString.contains(separatorThis)) { | |
originalString = originalString.replaceAll(separatorThis, ""); | |
} | |
longval = Long.parseLong(originalString); | |
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US); | |
formatter.applyPattern("#,###,###,###"); | |
String formattedString = formatter.format(longval); | |
//setting text after format to EditText | |
editTextThis.setText(formattedString); | |
editTextThis.setSelection(editTextThis.getText().length()); | |
} catch (NumberFormatException nfe) { | |
nfe.printStackTrace(); | |
} | |
editTextThis.addTextChangedListener(this); | |
} else { | |
Log.e("ThousandWatcher", "EditText must not be null"); | |
} | |
} | |
public static String resolveWithoutSeparator(EditText editText) throws ThousandSeparatorException { | |
if (editText != null && editText.getText() != null && !editText.getText().toString().trim().isEmpty()) { | |
return editText.getText().toString().replaceAll(separatorThis, ""); | |
} else { | |
if (editText == null) | |
throw new ThousandSeparatorException("EditText must not be null"); | |
else | |
return ""; | |
} | |
} | |
public static class ThousandSeparatorException extends Exception { | |
ThousandSeparatorException(String s) { | |
super(s); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment