Created
October 7, 2019 12:49
-
-
Save The-LoneWolf/6cef4f7d66534c784cbb78904c0e39bb to your computer and use it in GitHub Desktop.
numeric formatted edittext with custom postfix string
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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<EditText | |
android:id="@+id/edt" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:digits="01234 56789ریال," | |
android:inputType="number" /> | |
</LinearLayout> |
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 ir.technopedia.iottest; | |
import android.icu.text.DecimalFormat; | |
import android.icu.text.NumberFormat; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.widget.EditText; | |
import java.util.Locale; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
EditText edt = findViewById(R.id.edt); | |
edt.addTextChangedListener(new CurrencyTextWatcher(edt)); | |
} | |
} | |
class CurrencyTextWatcher implements TextWatcher { | |
boolean mEditing; | |
EditText edt; | |
public CurrencyTextWatcher(EditText edt) { | |
mEditing = false; | |
this.edt = edt; | |
} | |
public synchronized void afterTextChanged(Editable s) { | |
edt.removeTextChangedListener(this); | |
try { | |
String originalString = s.toString(); | |
Long longval; | |
if (originalString.contains(" ")) { | |
originalString = originalString.replaceAll(" ", ""); | |
} | |
if (originalString.contains(",")) { | |
originalString = originalString.replaceAll(",", ""); | |
} | |
if (originalString.contains("ریال")) { | |
originalString = originalString.replaceAll("ریال", ""); | |
} | |
longval = Long.parseLong(originalString); | |
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US); | |
formatter.applyPattern("#,###,###,###"); | |
String formattedString = formatter.format(longval); | |
//setting text after format to EditText | |
if (formattedString.length() > 0){ | |
edt.setText(formattedString + " ریال"); | |
edt.setSelection(formattedString.length()); | |
} | |
else | |
edt.setText(""); | |
} catch (NumberFormatException nfe) { | |
nfe.printStackTrace(); | |
} | |
edt.addTextChangedListener(this); | |
} | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
} | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment