I have used this line to check the locale.
String local = context.getResources().getConfiguration().locale.getDisplayCountry();
Toast.MakeText(this,local,Toast.LENGTH_SHORT).show();
It shows Australia (this is where I am).
But I need to display different pages based on locale. so what I have done is
if (local=="Australia")
runpage1();
else
runpage2();
But, it doesn't check the local as "Australia", so straight goes to the else line. I tried like
String x;
if (local=="Australia")
x = "1";
else
x="2";
Toast.MakeText(this,x,Toast.LENGTH_SHORT).show();
It shows 2. ( I am trying to write to make it as simple as possible, so nobody gets confused with previously asked questions)
Can anybody please suggest me why I am being unable to treat locale as String value ?
I have tried using this too.
TelephonyManager teleMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String myCode = teleMgr.getSimCountryIso();
But, same thing, myCode is also cannot be checked as String.
Any suggestions will be great.
== for test only reference..equals() for check String values. So you have to compare like this :
String x;
if (local.trim().equals("Australia"))
x = "1";
else
x="2";
Toast.MakeText(this,x,Toast.LENGTH_SHORT).show();
Related
I am using Android Speech to text for speech recognition.
final Intent sttIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
final String language = config.getLanguage().replace('-', '_');
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
sttIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, language);
sttIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
sttIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.getPackageName());
// WORKAROUND for https://code.google.com/p/android/issues/detail?id=75347
sttIntent.putExtra("android.speech.extra.EXTRA_ADDITIONAL_LANGUAGES", new String[]{language});
return sttIntent;
I am having problem while getting phone number and email id.
It is not giving proper results.
Suppose if user say "triple two triple three five hundred one"
In this case STT should give me 222333501, but it is giving some number and string as result.
Suppose if user say "abc_cde#gmail.com", in this case also it is giving at the rate of and underscore as strings.
Is there any native way to solve this problem.
I have referred a java utility Convert words to number to convert string to number if valid but i am not sure that would work in all cases.
Any help is appreciated.
I am working on android application where I want to detect the country code from my phone book where by default country code is not added. For example:
+971529090909,
00971529730000,
0529090909
In "+" and "00" state, I have country codes, but in case of "0" my country code is not available. I want to detect the country code of all contacts even if someone didn't put the country code.
How to detect country code/ Area code with any library or with any code snippet.
You can parse any number with any format using the libphonenumber library (maintained by Google), just specify the default Locale while parsing so it'll know what country code to default into.
You can play with an online version of it here.
TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String simCountryIso = telephony.getSimCountryIso();
if (simCountryIso != null)
return simCountryIso.toUpperCase(loc);
} else {
Locale loc = Locale.getDefault();
return telephony.getNetworkCountryIso().toUpperCase(loc);
}
I need to store all the installed apps icons in an array, and names in another array, so I´ve this code:
List<PackageInfo> apps = getPackageManager().getInstalledPackages(0);
int numberApps = apps.size();
String[] appsNames = new String[numberApps];
Drawable[] appsIcons = new Drawable[numberApps];
for(int i=0;i<apps.size();i++) {
PackageInfo p = apps.get(i);
String pname = p.packageName;
String appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
appsNames[i] = appname;
Drawable appicon = p.applicationInfo.loadIcon(getPackageManager());
appsIcons[i] = appicon;
}
And it works, the problem is that all the apps are displayed, even the system ones, so now i need to change the code for getting just the ones installed or updated by the user. I´ve been searching and I´ve found this and this, but as i´m using a Drawable and string array, and I cannot change that, I don´t know how to make it work.
Can someone help me please??
I made a overkill solution for my university some weeks ago: you can have a look at this class on Github.
We were dealing with Android Security Flow, so I do not think it is trivial to get what the app is about but the class suits perfectly your needs.
You would need to invest a bit of time to bind it to your app because it is designed to be asynchronous but it would work. There are probably simpler options though (but who likes it when it's too simple?)
The options you found would work but why are you stuck to an array, can't you create an adapter of it??
I have several language files in Android application: value/strings.xml, value-en/strings.xml, ...etc
It is possible to load the content of this files in some arrays or something. For example, I would like to load default text strings and english strings in 2 different arrays at run time.
Thanks
Alin
Create a method like this:
Resources getResourcesByLocale( Context context, String localeName ) {
Resource res = context.getResources();
Configuration conf = new Configuration(res.getConfiguration());
conf.locale = new Locale(localeName);
return new Resources(res.getAssets(), res.getDisplayMetrics(), conf);
}
Than you can get resources for any locale you've defined, for example:
Resources res_en = getResourcesByLocale(context, "en");
Resources res_de = getResourcesByLocale(context, "de");
Resources res_fr = getResourcesByLocale(context, "fr");
String some_name_en = res_en.getString(R.string.some_name);
String some_name_fr = res_fr.getString(R.string.some_name);
// etc...
Moreover, you do not need to take care about exceptions if you did not define string for some locale, because anyway default (from res/values/*) will be loaded instead.
Actually the situation is like that. Imagine I have this scenario: some chinese open the application. He has the mobile phone set with ch locale. The application default is xx as language, meaning I have 2 language files values/strings.xml (spanish for eg as default) and another language values-en/strings.xml for english. The default will make no sense for him, so english will be the most appropiate for his understanding, even if he does not understand it very good. So at the app start I open language settings (android language settings), where any selection will set the app in spanish unless he select english. I am forcing him to change the phone locale in english basically, just to use my app. Overall the concept of android is wrong, because i need to be able to see an application in any language I want without changing device language.
What I have done: - I created in values folder one more string_xx.xml file. Now, for a translation string name = "txtTranslation" I have in string_xx file "en_txtTranslation" key. R.java loads them all and in my app, based on a global var selectedLanguage = xx, I attach the write string using this code:
public String translate(String text)
{
String appLanguage = UtilsCentral.getUserLanguage(getApplicationContext());
if (appLanguage != "")
{
return getString(getResources().getIdentifier(appLanguage + "_" + text, "string", this.getPackageName()));
}
else
{
return getString(getResources().getIdentifier(text, "string", this.getPackageName()));
}
}
Indeed, at activity on create i need to set all views with .text = tarnslate("txtTranslation")
Note: UtilsCentral.getUserLanguage(getApplicationContext()) returns app language (user selection)
Conclusion, there is more unuseful work, but lets me do what i need, and what i believe is normal.
I just began in Java and Android. This website already helped me a lot.
Here I'm stuck with something very easy. In other language I never had problem but there..
If I try the following, i have no error on eclipse but once running in the emulator, the software crash at the "if" line.
String message = mOutEditText.getText().toString();
if (message.length() > 4) {
If I use if (8 > 4) { then I have no problem.
I also tried if (message.toString.length() > 4) { without any success.
Thanks for your help.
It's hard to say without knowing the error but just about the only thing that could possibly go wrong with that line is a NullPointerException i.e. when you have this problem, it is because:
String message = mOutEditText.getText().toString();
is effectively resolving to:
String message = null;
Most likely this is because your EditText does not have any text in it (from the name of your variable I'm guessing you are using an EditText).
You can account for this case by checking that message is not null. Here is one way of doing it:
if (message != null && message.length() > 4) {
For instance, you initialize message as a static string like "Stack overflow" and try
message.length();
if it works, then there is no problem with toString() function.
Best of luck..!!
In String There are two way of getting the length of String:-this is working properly
String message="";
protected void onCreate(Bundle savedInstanceState){ s.length();}