i've been searching a for a while but couldn't find anything similar to what i'd need. I'd like to know how i can list all languages available in an android application? and say, pick one of them to run as default.
Just create a preferences Activity in your application and display all the supporting languages within it (may be in a list view with a check mark or something), when the user selects a specific language, just save the selected language preference in your Application Shared Preference.
Within every activity of your Application just run a conditional flow and use the Application's Drawables and String values with respect to the language selected in the Application Preference.
EDIT
Here is a good example of Localization
http://www.icanlocalize.com/site/tutorials/android-application-localization-tutorial/
Since you don't want to check the selected language in the phone's settings, but only in the Application's context, then you should create a Preferences Activity specific to your Application. Here is a good Example of saving creating and using Preferences :
http://androidpartaker.wordpress.com/2010/07/11/android-preferences/
After doing that, just run a conditional flow in your Application's Activities, for example:
Class myActivity extends Activity
{
String KEY_SELECTED_LANGUAGE = "selected_language";
onCreate(...)
{
if(getApplicationContext().getSharedPrefrences().getString(KEY_SELECTED_LANGUAGE,"").equalsIgnoreCase("en"))
{
//change all the drawable/String values displayed in your activity to the version of the selected language.
}
}
Related
How can I keep multiple sets of Preferences using the AndroidX Perference library?
For example, the user can choose different profiles which have different values over the same set of settings:
Global preferences:
Selected profile: foo
Other settings: etc
Profile 1:
Name: foo
Setting: Bar
Profile 2:
Name: bar
Setting: Hello
etc
I see two options here:
Use Context.getSharedPreferences(String name, int mode) with different names for different profiles. PreferenceManager has a setDefaultValues method that also takes a name in that case. The downside of this approach is that a settings screen will still use the default shared prefs, and not the ones for whatever profile is selected.
Make your own shared prefs wrapper that extends PreferenceDataStore and implements all the methods. Then call PreferenceManager.setPreferenceDataStore whenever the profile is changed. A settings screen will show the correct values with this approach. Internally, your wrapper can either use a database to store the values, or use the default shared prefs, appending a different prefix for different profiles.
I haven't tried either but I believe it could work.
The Android documentation describes how to create a "preferences" UI using either a PreferenceActivity or a PreferenceFragment. The preferences themselves are defined in a XML file (e.g. preferences.xml) which contain, among other things, the "key" (android:key) to use to store each preference in the app's SharedPreferences.
This is good for apps which have a single set of preferences. However let's assume that there is an app that can have multiple "items" (for example: multiple account in a video chat app), and needs to show a preferences screen for each account.
How can this be done? Is it possible to specify the keys at runtime (so that e.g a different prefix can be used for each account)? Is there a better way to approach this?
Yes, its possible!
you can assign keys on Runtime like, but as you said in comments that you need to change the Keys of the pre-defined Preferences in Xml rather than creating new Preferences explicitly, here's a method -
Preference pref = findPreference("my_pref");
String myPrefix = "prefix_";
pref.setKey(myPrefix + pref.getKey());
I am trying to save the values of various variables which my game is progressing, like logo number or lives available, etc using LIBGDX framework.
Code goes as such:
static Preferences prefs = Gdx.app.getPreferences("My_state");
public static void ContinuePutstate() {
prefs.putInteger("option", MenuScreen.option);
prefs.putInteger("lifes", Loadassets.lifes);
prefs.putInteger("hammertouch", Loadassets.hammertouch);
prefs.putInteger("multilogonum", Loadmultiple.multilogonum);
prefs.putInteger("brushtouch", Loadassets.brushtouch);
prefs.putInteger("leveluser", Loadassets.Leveluser);
prefs.putInteger("iconnumber", CorrectScreen.iconnumber);
System.out.println("HAd saved option "+prefs.getInteger("option")+" and original option is "+MenuScreen.option);
}
When I tried to print that, I am getting option 0 but menuscreen option actually has another value.
after putting all values use
prefs.flush();
this will write the data to preferences
see
https://code.google.com/p/libgdx/wiki/Preferences#Flushing
It is important to note that creating a singular static instance is the proper way to go with the LibGDX Preferences framework, because the Android OS allows you to obtain only one preferences instance, and not more. Meaning, if you tried to get more preferences than just a single one, the key-value pairs would not be saved.
I want to develop an e buy application. I have categories and sub-products and data is kept in external server. If I add a new category on the server I want to add category and product activity automatically. Is it possible? How can I create a new activity automatically and how can I add the activity automatically in the android manifest?
No, you cannot do this. Activity objects must be declared at compile-time and cannot be changed later. You must adapt your code to work in the different situations.
For example, let's say you are fetching a name, birthdate, and description for a human resources entry. You would create one Activity that contained your layout and text fields, then have that Activity fetch the name, birthdate, and description from your server. It would then populate the text fields with the data, or show/hide the fields if there is no data for them.
This is a design issue, the activity should not be binded with your data. You should design an activity for categories data and an activity for products data. If they have different look, just build different sub class of your root activities.
I am looking for a way to create dynamic preferences where I don't need to hard code the preference key and I could have a variable number of preferences.
Basically, my application will let the user create a multiple number of profiles and each of these profiles will save custom values for a fixed number of preferences.
So this way, the user does not have to change the preferences every time he wants this app to run differently, he can just switch the profile.
One way I think will work is by subclassing all the standard Preference classes and calling their setKey method with my custom preference key, containing the profile name, but this is ugly.
So is there a cleaner and more standards compliant way to do this?
Regards,
Dhruwat
You can save different preferences in a different file for each user using the getSharedPreferences method:
getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
That way, you can do something like this:
SharedPreferences settings = getSharedPreferences("prefs_user_"+user_id, 0);
// from now on you can use use the normal way to set or get the preferences
I'm assuming you are using an id in order to identify them users.