I would like to ask which would be better to put config files in Android, since Android doesn't support subfolders under values.
Example:
values/
config/
api.xml
What is the preferred style?
Creating a class with static variable:
package myapp.config;
public static class Api{
public static String CLIENT_ID = "1234567890";
public static String CLIENT_SECRET = "123456789";
}
Add everything in string.xml:
<string name="config_api_client_id">1234567890</string>
<string name="config_client_secret">123456789</string>
I would prefer class with final static variables. It is as constant as using string.xml, but when you use string resources (string.xml), you need a Context object to get the strings.
When changing your code snippet to the snippet below, you can access the variables from each and every piece of code and you know that the value of the variables never change.
public class Api{
public final static String CLIENT_ID = "1234567890";
public final static String CLIENT_SECRET = "123456789";
}
Then you can access the variables like so Api.CLIENT_ID
Related
Refer following code snippet, where fetching default value of SharedPreference is defined in Constant file:
AppConstants.java
public static final String AA_VEHICLE_REG_NUMBER = "aa_vehicleRegNum";
public static final String AA_DEFAULT_VEHICLE_REG_NUMBER = "DL00-AA-0000";
Code usage
value = sharedPreferences.getString(AA_PREF_VEHICLE_ID_REG, AA_DEFAULT_VEHICLE_REG_NUMBER ) ;
In above code fragment, the string AA_DEFAULT_VEHICLE_REG_NUMBER is being stored / retrieved in form of String constant (which poses constraint on internationalization, and for which strings.xml should be used).
What is the downside of storing and retrieving from strings.xml (like the string resource might not be available and in what scenarios it may fail?)
I am using Shared preferences for the first time and getting errors.
my code is like this :
public class MainActivity extends Activity {
static final String ONE = "";
static final String TWO = "";
private static SharedPreferences mSharedPreferences;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences.Editor edi = mSharedPreferences.edit();
edi.putString(ONE, "1");
edi.putString(TWO, "2");
edi.commit();
String one = mSharedPreferences.getString(ONE,"1");
String two = mSharedPreferences.getString(TWO,"2");
System.out.println("Your Numbers: "one+ " " + two);
}
}
Expected Output:
Your Numbers: 1 2
Console Output:
Your Numbers: 2 2
I can't figure out what i am doing wrong in it. Share your views.
You need to add some string to the names/keys. Currently both key names are blank and hence your code is overwriting the same preference value
.
Change the static strings as follows and it should work fine.
static final String ONE = "one";
static final String TWO = "two";
Also try using a helper class to make things simpler with shared preferences. Here is one that i wrote: Android-SharedPreferences-Helper
Because of this:
static final String ONE = "";
static final String TWO = "";
change it to:
static final String ONE = "One";
static final String TWO = "Two";
U need unique values for every preference.
In your case the ONE gets overridden by the TWO.
Extra info
If u look in the android docs here you will see that putString requires two parameters:
key : String: The name of the preference to modify.
value : String: The new value for the preference.
and if u than look at getString here you will notice that it also has two parameters, both the same as putString:
key : String: The name of the preference to retrieve. -- important
defValue : String: Value to return if this preference does not exist.
The name/key is the part that let the get part know from which preference it needs to get the value.
Hope this will make things a bit clearer for u!
Both the strings are empty
static final String ONE = "";
static final String TWO = "";
It should be like :
static final String ONE = "one";
static final String TWO = "two";
why on all demos and tutorials in SQLiteOpenHelper class variables are always: public static final
Have a look:
public static final String ORDER_ID = "order_id";
public static final String ORDER_LOGIN_NAME = "login_name";
public static final String ORDER_RESTO_U_NAME = "resto_uniqe_name";
My question is: I am making an application and its database is too big. So, I will have to make atleast 60-70 variables of these kind. Won't it affect application performance? As these are static variables.
Any help will be highly appreciated....
Well, whether they public or private or package-protected depends on your needs, but final static is a good way of declaring constants per Android guidelines, take a look here for explanation: http://developer.android.com/training/articles/perf-tips.html#UseFinal
Consider the following declaration at the top of a class:
static int intVal = 42;
static String strVal = "Hello, world!";
The compiler generates a class initializer method, called , that is executed when the class is first used. The method stores the value 42 into intVal, and extracts a reference from the classfile string constant table for strVal. When these values are referenced later on, they are accessed with field lookups.
We can improve matters with the "final" keyword:
static final int intVal = 42;
static final String strVal = "Hello, world!";
The class no longer requires a method, because the constants go into static field initializers in the dex file. Code that refers to intVal will use the integer value 42 directly, and accesses to strVal will use a relatively inexpensive "string constant" instruction instead of a field lookup.
I am trying to define a search string and new to android, not sure how to do the same. Any clue?
Here's my code for the same:
public final static String PROD_ENVIRONMENT = "https://mobile13.com/fwd/answers/answers/service/v1/?q=**KEYWORD**%20revenue&ui.theme=novadark&uuid=PADACT-002&userAgent=iphone";
I want to replace the KEYWORD with a dynamic string like %s, which can be recognized with a static string say "public static string KEYWORD" , which i can check ,in turn matches the typed keyword and display the results accordingly
Try this
public final static String PROD_ENVIRONMENT = "https://mobile13.com/fwd/answers/answers/service/v1/?q="+KEYWORD+"%20revenue&ui.theme=novadark&uuid=PADACT-002&userAgent=iphone";
That is if you have the static variable in the same class. If you have it in a different class, say StaticUtils class then you can put it as StaticUtils.KEYWORD.
why will String.format not work?
android documentation
Can I get this string in code?
GetId() returns an int?
EDIT:
If not, is there a way I can assign my textboxes a tag each, which is a string, and then return this in code?
Can I get this string in code?
No. You can get only ID of resource. Reason is because your R.class storing all resources as
public static final int fields
Your R.java can looks similar like this:
public static final class id {
public static final int DataHallTextField=0x7f09000a;
}
Update:
If not, is there a way I can assign my textboxes a tag each, which is
a string, and then return this in code?
So i think you are looking for this:
String name = getResources().getResourceEntryName(R.id.DataHallTextField);
If you want also package you can use
String name = getResources().getResourceName(R.id.DataHallTextField);
You are looking for the resource name:
getResources().getResourceEntryName(intresid);
If you want the resource name include package name then use:
getResources().getResourceName(intresid);