Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force textview to show numbers with only english digits

I use below code to force my textview to show its text numbers with English digits:

txtScale.setText("x" + String.format(Locale.ENGLISH, String.valueOf(scaleValue)));

but it is not working and keep showing the numbers with selected locale. If application locale set to Arabic, it show numbers to Arabic, if set to English it show them English but I want to force to show numbers in English at all states.

For example I want to show below text in Arabic:

۱۲۳۴ //are one two three four

As:

1234

If it helps, I use below code for changing the language of the app manually:

    Locale locale = new Locale(CurrentLanguage);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    context.getResources().updateConfiguration(config,
            context.getResources().getDisplayMetrics());
like image 260
Husein Behboudi Rad Avatar asked Sep 13 '15 05:09

Husein Behboudi Rad


2 Answers

Not very sure what you need, but I guess you need this:

txtScale.setText(String.format(Locale.ENGLISH, "x%d", scaleValue));

String.valueOf(scaleValue) there error was here, where you have converted the number based on default Locale

like image 180
Derek Fung Avatar answered Sep 28 '22 01:09

Derek Fung


I have had the same problem, the solution was to concatenate the number variable with an empty string. For example like this :

public void displayPoints(){
    TextView scoreA = findViewById(R.id.score_id);
    scoreA.setText(""+points);
}

I used this

scoreA.setText(""+points);

instead of this

scoreA.setText(String.format("%d",points));

this will even give you a warning that hardcoded text can not be properly translated to other languages, which exactly what we want here :) .

like image 34
Omar Avatar answered Sep 28 '22 02:09

Omar