Reputation: 105
i want when type number in edittext show persian or arabic number instead english number,"its working well with method settext() but not work when i'm typing." i use this code for show persian number and for TextView and Button work well but not work in EditText in typing!
public class FormatHelper {
private static String[] persianNumbers = new String[]{"۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"};
public static String toPersianNumber(String text) {
if (text.isEmpty())
return "";
String out = "";
int length = text.length();
for (int i = 0; i < length; i++) {
char c = text.charAt(i);
if ('0' <= c && c <= '9') {
int number = Integer.parseInt(String.valueOf(c));
out += persianNumbers[number];
} else if (c == '٫') {
out += '،';
} else {
out += c;
}
}
return out;
}
}
public class LoginActivity extends AppCompatActivity {
myEditText myEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_page);
myEditText = findViewById(R.id.user_pnumber);
myEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String s = FormatHelper.toPersianNumber(charSequence.toString());
Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show();
myEditText.setText(s);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
i think must use addTextChangedListener
UPDATE i get this error for force close app:
FATAL EXCEPTION: main
java.lang.StackOverflowError
at android.text.SpannableStringBuilder.getTextRunAdvances(SpannableStringBuilder.java:1212)
at android.graphics.Paint.getTextRunAdvances(Paint.java:2100)
at android.graphics.Paint.getTextRunAdvances(Paint.java:2064)
at android.text.TextLine.handleText(TextLine.java:969)
at android.text.TextLine.handleRun(TextLine.java:1178)
at android.text.TextLine.measureRun(TextLine.java:617)
at android.text.TextLine.measure(TextLine.java:480)
at android.text.TextLine.metrics(TextLine.java:454)
at android.text.Layout.getLineExtent(Layout.java:993)
at android.text.Layout.getLineStartPos(Layout.java:519)
at android.text.Layout.getHorizontal(Layout.java:856)
at android.text.Layout.getHorizontal(Layout.java:827)
at android.text.Layout.getPrimaryHorizontal(Layout.java:811)
at android.widget.TextView.getFocusedRect(TextView.java:5834)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:120)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:94)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:65)
at android.view.ViewGroup.focusSearch(ViewGroup.java:681)
at android.view.ViewGroup.focusSearch(ViewGroup.java:683)
at android.view.ViewGroup.focusSearch(ViewGroup.java:683)
at android.view.ViewGroup.focusSearch(ViewGroup.java:683)
at android.view.ViewGroup.focusSearch(ViewGroup.java:683)
at android.view.ViewGroup.focusSearch(ViewGroup.java:683)
at android.view.ViewGroup.focusSearch(ViewGroup.java:683)
at android.view.View.focusSearch(View.java:6547)
at android.widget.TextView.onCreateInputConnection(TextView.java:6489)
at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1213)
at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:1164)
at android.widget.TextView.setText(TextView.java:4231)
at android.widget.TextView.setText(TextView.java:4148)
at android.widget.EditText.setText(EditText.java:104)
at android.widget.TextView.setText(TextView.java:4123)
at LoginActivity$2.onTextChanged(LoginActivity.java:76)
at android.widget.TextView.sendOnTextChanged(TextView.java:8284)
at android.widget.TextView.setText(TextView.java:4314)
at android.widget.TextView.setText(TextView.java:4148)
at android.widget.EditText.setText(EditText.java:104)
at android.widget.TextView.setText(TextView.java:4123)
Upvotes: 3
Views: 1596
Reputation: 3573
You just need using a Persian font that support "Persian numbers" for the view. For example use shabnam_fd font as font family in styles.xml file:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="drawerArrowStyle">@style/DrawerIcon</item>
<item name="android:fontFamily">@font/shabnam_fd</item>
</style>
And add shabnam_fd.ttf font in font folder.
Upvotes: 0
Reputation: 13585
use this code
ok you need global var like et_change
private boolean et_change = false;
and in TextWatcher use it like below
myEditText.addTextChangedListener(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 (et_change) {
et_change = false;
return;
}
et_change = true;
String s = FormatHelper.toPersianNumber(charSequence.toString());
myEditText.setText(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
hope it helps you
Upvotes: 2
Reputation: 959
The exception comes because you are setting up the text inside the onTextChanged
event. That triggers the event itself again, and this new event triggers it again ad infinitum.
I suggest you to use some kind of boolean condition to only trigger the event once.
Upvotes: 1