I want to check if a specific language pack (Google Text-to-Speech) is available or not.
t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(t1.isLanguageAvailable(new Locale("bn_BD")) >= TextToSpeech.LANG_AVAILABLE)
{
// Set language
}
}
});
but the problem is t1.isLanguageAvailable(new Locale("bn_BD")) is always returing false even the language is available and also working this voice.
You must construct the Locale object using separate strings for the language and country like this:
new Locale("bn", "BD")
see this and this. :)
Related
#Override
public void getLeaderboardGPGS() {
if (gameHelper.isSignedIn()) {
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(), getString(R.string.event_score)), 100);
}
else if (!gameHelper.isConnecting()) {
loginGPGS();
}
}
#Override
public void getAchievementsGPGS() {
if (gameHelper.isSignedIn()) {
startActivityForResult(Games.Achievements.getAchievementsIntent(gameHelper.getApiClient()), 101);
}
else if (!gameHelper.isConnecting()) {
loginGPGS();
}
}
Can anyone explain to me what these methods do? I have them as part of implementing a GoogleApi interface I made in the context of a tutorial. I especially don't understand the 100 / 101 parts, but the whole thing, in general, is quite confusing for me.
PS. I am making a game in LibGDX and this is my first time touching the Google Play API (or I think any API for that matter)
First Method getLeaderboardGPGS show you Leaderboard above your Activity
if you are already Signed in otherwise it start signing process.
Above method definition is from Libgdx wiki but it should be
private final static int REQUEST_CODE_UNUSED = 9002;
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(), getString(R.string.leaderboardId)), REQUEST_CODE_UNUSED);
REQUEST_CODE_UNUSED is an arbitrary integer for the request code
getString(R.string.leaderboardId) is LEADERBOARD_ID
taken from Google wiki
Second Method getAchievementsGPGS is used to show a player's achievements, call getAchievementsIntent() to get an Intent to create the default achievements UI.
startActivityForResult(Games.Achievements.getAchievementsIntent(gameHelper.getApiClient()), REQUEST_ACHIEVEMENTS);
where REQUEST_ACHIEVEMENTS is an arbitrary integer used as the request code.
This question already has answers here:
Change app language programmatically in Android
(34 answers)
Closed 6 years ago.
I need to switch between two languages inside the android application itself.
Now, I have string values for two different languages in two strings.xml files in two different folders, one is under Values folder(by default) and another one is under values-fr(for french) which is created when tried Edit translation under language in android studio.But I don't know how to switch between the languages.
It shows the default language (i.e., English) but don't know how to implement a way to switch to other language.
Does any one have easy way of implementing it...?
You can use Android-LocalizationActivity
Here an excerpt from the documentation:
Keep calm and stay easy with multiple language supported in your
android application.
It's basic for android application to be supported multiple languages.
Yeah! It's very easy because android has String Resource. Developer
just had to prepare the text for different languages then android
system will use itself. But frequently problem is "On-time Language
Changing". Because the String Resource was designed to be depending on
current device language. but if we want to change the language by
click some button. It will be difficult to handle it. This problem
will solved because I have created a new library to handle application
language. It called "Localization Activity" library.
Here the example to use it from the documentation:
import android.os.Bundle;
import android.view.View;
import com.akexorcist.localizationactivity.LocalizationActivity;
public class MainActivity extends LocalizationActivity implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
findViewById(R.id.btn_th).setOnClickListener(this);
findViewById(R.id.btn_en).setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btn_en) {
setLanguage("en");
} else if (id == R.id.btn_th) {
setLanguage("th");
}
}
}
In the example above, when user click on a button. It will change to English or Thai language.
You can use the locale class to do this .Hope this helps
public class LocaleLanguage {
private static Locale mLocale;
public static void setLocale(Locale locale) {
mLocale = locale;
if(mLocale != null) {
Locale.setDefault(mLocale);
}
}
public static void updateConfig(ContextThemeWrapper wrapper) {
if(mLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Configuration configuration = new Configuration();
configuration.setLocale(mLocale);
wrapper.applyOverrideConfiguration(configuration);
}
}
public static void updateConfig(Application app, Configuration configuration) {
if(mLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
//Wrapping the configuration to avoid Activity endless loop
Configuration config = new Configuration(configuration);
config.locale = mLocale;
Resources res = app.getBaseContext().getResources();
res.updateConfiguration(config, res.getDisplayMetrics());
}
}
}
here is the application class
public class App extends Application {
public void onCreate(){
super.onCreate();
// get user preferred language set locale accordingly new locale(language,country)
LocaleUtils.setLocale(new Locale("iw"));
LocaleUtils.updateConfig(this, getBaseContext().getResources().getConfiguration());
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LocaleUtils.updateConfig(this, newConfig);
}
}
here is the your activity
public class MainActivity extends Activity {
public BaseActivity() {
LocaleUtils.updateConfig(this);
}
}
And for more refer this link for whole tutorial refer this link
I am setting my TextToSpeech to use a particular language (English - UK), using the locale "en_GB". But it always uses my devices default language. Is there no way to set it programmatically? I have downloaded the files required for the language and when I change my TTS's default language to 'English - UK' it works but when the default is different the programmatic approach does not work. I have scoured the web to my best but am unable to resolve this issue.
String ttsEngine = "com.google.android.tts";
txt2Speech = new TextToSpeech(this, this, ttsEngine);
//Locale ttsLocale = new Locale("eng", "GBR");
txt2Speech.setLanguage(new Locale("en_GB"));
Tried several methods, but none are working.
Can I not set my TTS's language programmatically?
Thank You
EDIT: In response to 'A Honey Bustard'
Other Code:
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener
My onInit()
public void onInit(int status) {
// TODO Auto-generated method stub
}
Also I'm calling .setLanguage() in my onCreate(), as soon as my TextToSpeech is initialized. Is that correct? Also I'm only calling it once. It is not required to call it every time right? Also I'm testing on a GS7
You need to set the language once the Text to Speech Engine has initialised correctly.
public void onInit(int status) {
switch (status) {
case SUCCESS:
// Set the language here
break;
case ERROR:
// Something went wrong. You can't set the language
break;
}
}
That should do it.
Try the second Constructor from Locale that takes two Strings like this :
txt2Speech.setLanguage(new Locale("en", "GB"));
EDIT :
Yes it is usually ok to do instantiate it in onCreate, and it usually only needs and should be done once.
All I can do is show you my working code, I am setting the default language after instantiating in onCreate() :
textToSpeech = new TextToSpeech(getApplicationContext(), this);
textToSpeech.setLanguage(Locale.getDefault());
In my app there are buttons in which you can change the language, which trigger this code (case British English) :
textToSpeech.setLanguage(new Locale("en", "GB"));
Maybe it is not available somehow , there are some checks you can validate if the language and country is available. You might find your error there.
if (textToSpeech.isLanguageAvailable(new Locale("en", "GB")) == TextToSpeech.LANG_COUNTRY_AVAILABLE
&& textToSpeech.isLanguageAvailable(new Locale("en", "GB")) == TextToSpeech.LANG_AVAILABLE)
should return true.
I'm trying to implement a Google Fit Listener when data is updated into Google Fit services.
In this link of Google Fit documentation there is a simple example, however, it is not 100% clear. For that reason, I have two problems:
I don't know how to implement mResultCallback variable (there aren't any examples in this documentation).
When I define a simple ResultCallback (it seems to work but I'm not sure) and I launch the application, it gives me a result error code: java.lang.SecurityException: Signature check failed
The code within the HistortyApi lists one of android.permission.ACCESS_FINE_LOCATION or android.permission.BODY_SENSORS as being required.
Adding those permissions to my code hasn't resolved the same problem though.
Confirmed bug in Google Fit services. See discussion in https://plus.google.com/110141422948118561903/posts/Lqri4LVR7cD
mResultCallback is a ResultCallback<Status> so you need to implement a class of that type. Documentation is here, but there's only one method you need to implement:
public abstract void onResult (Status result)
The standard way is to do this using an anonymous class either when you declare mResultCallback or when you're using it as a parameter. Below is an example from Google's BasicRecordingAPI example:
Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_ACTIVITY_SAMPLE)
.setResultCallback(new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
if (status.isSuccess()) {
if (status.getStatusCode()
== FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
Log.i(TAG, "Existing subscription for activity detected.");
} else {
Log.i(TAG, "Successfully subscribed!");
}
} else {
Log.i(TAG, "There was a problem subscribing.");
}
}
});
If you want to use a member variable you can simply make an assignment instead:
ResultCallback<Status> mResultCallback = new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
...
}
});
Of course you can define a non-anonymous class, but if you did that for every callback you had you would end up creating a LOT of classes.
I am building a user interface in netBeans (coding by hand, more flexible) with multiple toolbars.
What I am trying to do is create an actionListener for each button. I am retrieving names of the functions from XML and parse them to string. I will write implementations for those functions in a separate class, but my problem is the following:
How do I make the link between the function name and the string containing it's name?
Example: String is Open(), function will be Open(someParameter) and in the definitions class there will be static void Open(param).
First of all, consider my comment about your idea of dynamic button behavior resolved from strings being a wrong approach. However if you still need exactly what you asked, what you need is Reflection API.
Here's an example:
Class c = SomeClassWithMethods.class;
Method m = c.getMethod("someMethodName", String.class, Integer.class, Integer.TYPE);
m.invoke(baseObjectFromWhichToCallTheMethod, "stringParam", 10, 5);
Added:
Another option, which is a little bit prettier than reflection, but still a messy design, would be to use a map to link those Strings to methods. The code is a bit longer, but from the Java perspective it is much better than using reflection for your task (unless you have some specific requirement of which I'm not aware). This is how it would work:
//Interface whose instances will bind strings to methods
interface ButtonClickHandler {
void onClick();
}
class SomeClassYouNeed {
//One of the methods that will be bound to "onButtonOneClick()"
public void onButtonOneClick() {
log.info("ButtonOneClick method is called");
}
public void onButtonTwoClick() {
log.info("ButtonTwoClick method is called");
}
//Map that will hold your links
private static Map<String, ButtonClickHandler> buttonActionMap;
//Static constructor to initialize the map
static {
buttonActionMap = new Map<String, ButtonClickHandler>();
buttonActionMap.put("onButtonOneClick()",new ButtonClickHandler() {
#Override
public void onClick() {
onButtonOneClick();
}
});
buttonActionMap.put("onButtonTwoClick()",new ButtonClickHandler() {
#Override
public void onClick() {
onButtonTwoClick();
}
});
}
public void callByName(String methodName) {
final ButtonClickHandler handler = buttonActionMap.get(methodName);
if (handler == null) {
throw new IllegalArgumentException("No handler found by name: "+methodName);
}
handler.onClick();
}
}
After you call callByName("onButtonTwoClick()") it will fetch the respective instance of ButtonClickHandler which will use the static method onButtonTwoClick() to process the click of the button.
It seems to me that you are looking for the equivalent of JS "eval" function in Java. This might help. Nevertheless it is generally not a good idea as #Max stated, you might want to rethink your design.
If i have understood your question correctly you are trying to generate your code files based on some strings taken from a XML file. I can suggest you this library to generate your codes.
For tutorials you can visit this link.
You may even use the Java Reflection API. Here is a link for the tutorial.
Its upto you, that which of the above two you use.