I have a few drawables:
R.drawable.pres01
R.drawable.pres02
R.drawable.pres03
I want to use SharedPreference to display the proper drawable:
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
String someId;
ImageView ivPresident;
int inAdd;
prefs = this.getSharedPreferences("pNum", Context.MODE_PRIVATE);
someId = prefs.getString("presNum", "");
inAdd = Integer.parseInt(someId);
ivPresident = (ImageView) findViewById(R.id.imgViewPresident);
I converted the String to Integer, now how do I set the image source of ivPresident to the number based on the saved string.
So for example:
if someId is 01 the drawable for ivPresident is R.drawable.pres01
if someId is 02 the drawable for ivPresident is R.drawable.pres02
if someId is 03 the drawable for ivPresident is R.drawable.pres03
I tried the following but did not work:
ivPresident.setBackground(R.drawable.pres + inAdd);
How do I fix the code to achieve it?
What you want is:
Resources.getIdentifier(String name...)
See here
Build the string the way you are trying now.
EDIT:
String someId = prefs.getString("presNum", "");
int id = getResources().getIdentifier("pres0" + someId, "drawable", getPackageName())
ivPresident.setBackgroundResource(id);
Related
I have a spinner with 2 selections, "text1" and "text2". When I tried to get "text1" and log "text1" to see if I got it or not, I got this instead: "androidx.appcompat.widget.AppCompatSpinner{d963757 VFED..CL. ........ 389,1177-883,1230 #7f080094 app:id/spinner1}".
What am I doing wrong?
String getSpinnerName = "spinner" + "1";
Spinner spinner = (Spinner) findViewById(getResources().getIdentifier(getSpinnerName, "id", getPackageName()));
final String spinnerText = spinner.toString();
Log.d(String.valueOf(LOG), spinnerText);
you are converting an object into String that's why.
final String spinnerText = spinner.toString();
if you want to get the values from spinner use below one.
final String spinnerText = spinner.getSelectedItem().toString();
I want to select an image id every 24 hours . for example at special time like 12 a.m , i able to select a random id to set it to my imageview.
I know to use alarm manager , but I do not know how to use it.
here I just selected my random id like this ans set to SharedPreferences and finally it get it.
i want to this every 24 hours.
would you please help?
I am new with android.
private void setBG(int bg) {
SharedPreferences.Editor share1 = getSharedPreferences("share1", Context.MODE_PRIVATE).edit();
share1.putInt("bg", bg);
share1.commit();
}
private int getBG() {
SharedPreferences share1 = getSharedPreferences("share1", Context.MODE_PRIVATE);
return share1.getInt("bg", R.drawable.pg19);
}
private void showBGS() {
Random rand = new Random();
int rndInt = rand.nextInt(18) + 1;
String drawableName = "pg"+ rndInt;
int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
setBG(resID);
}
I'm trying to save marker with Latlng and two strings with SharedPreferences and i'm doing a for loop to retrieve it when the activity is lunched again i had another question before because i couldn't delete the marker from the SharedPreferences So my mind is so missed up i can't figure it out what is the issue now please check the code below and any suggestions that could help i appreciated
I'm Using the et String so i match it to Marker title and on Marker Click i delete the Marker from sharedPreferences according to its title and already initilized the SharedPreferences in onCreated Method like so SharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
So I save the values like this,
int Int,cycler,IncreaseAmount;
String testSample;
private static final String StringLat= "Latitude",StringLng="Longitude",StringDis="Dis",StringName="Name";
String SinputNames = inputName.getText().toString();
Sinputdiscription = inputdiscription.getText().toString();
dLatitude = AddedLatLng.latitude;
dLongitude = AddedLatLng.longitude;
SharedPreferences.Editor editor = SharedPrefs.edit();
IncreaseAmount++;
editor.putInt("IJSS",IncreaseAmount);
IncreaseAmount = SharedPrefs.getInt("IJSS",0);
//SJSS = SinputNames;
//SJSS = Double.toString(dLatitude);
editor.putString("ErrorTest","Pulling up the info is working");
editor.putLong(SJSS+StringLat,Double.doubleToLongBits(dLatitude));
editor.putLong(SJSS+StringLng,Double.doubleToLongBits(dLongitude));
editor.putString(SJSS+StringName,SinputNames);
editor.putString(SJSS+StringDis,Sinputdiscription);
// editor.putString("lat"+ Integer.toString((IntCoords)), Double.toString(point.latitude));
editor.putString("ForLooper"+Integer.toString((IncreaseAmount)),SinputNames);
editor.apply();
and then pull it off from sharedPreferences
String CheckErrortest = SharedPrefs.getString("ErrorTest","Not working!");
Log.i("AlertSinput:",CheckErrortest);
cycler = SharedPrefs.getInt("IJSS",0);
if(cycler !=0) {
String Name="";
double Lat321,Lng321;
// Log.i("ifCycler","if Cycler != 0 is working");
Int = SharedPrefs.getInt("IJSS",0) +1;
for(int i=0; i< Int ; i++ ) {
Log.i("ForLoop:","The for loop is also working");
// editor.putString("ForLooper"+Integer.toString((IncreaseAmount)),SJSS+StringLat);
//Here i can't pull up the info
String iCheck = SharedPrefs.getString("Forlooper"+Integer.toString((Int)),"");
Log.i("TheMarkerName:","Should be"+iCheck);
Name = SharedPrefs.getString(iCheck+StringName,"Marker");
Lat321 = Double.longBitsToDouble(SharedPrefs.getLong(iCheck+StringLat,0));
Lng321 = Double.longBitsToDouble(SharedPrefs.getLong(iCheck+StringLng,0));
Log.i("Markertitle:#",Name);
Log.i("TestTheInteger:#",Integer.toString((Int)));
if(Lat321 !=0) {
AddedLatLng = new LatLng(Lat321,Lng321);
drawMarker(AddedLatLng,Name);
}
}
}
So the CheckErrortest and Log.i("ForLoop:","The for loop is also working");
are working Just fine but the String String iCheck = SharedPrefs.getString("Forlooper"+Integer.toString((Int)),"");
Is not working when i pull up the info for some reason and i couldn't figure out what is the issue anything that could help is appreciated thank you very much
I don't see you're commiting your changes. You need to do
editor.apply();
or
editor.commit();
All changes you make in an editor are batched, and not copied back to the original SharedPreferences until you call commit() or apply()
You need to apply your changes after work with editor.
editor.commit();
or
editor.apply();
I sent data from Activity A to Activity B through means of an intent.
I then extracted the results of the intent via the following.
String IdString = questionData.getString("id");
TextView mstudentId = (TextView) findViewById(R.id.ID);
mstudentId.setText(IdString);
I would now like to take the value of IdString which is equal to the value of the string "id" and place it inside a Url. For example,
final static String URL_ANSWER = "http//:myFake/(IdString)/Url"
How do I go about doing this in Android Studio?
Like this,
final static String URL_ANSWER = "http//:myFake/" + IdString + "/Url";
OR
UPDATE
String IdString = questionData.getString("id");
TextView mstudentId = (TextView) findViewById(R.id.ID);
mstudentId.setText(IdString);
final static String URL_ANSWER = "http//:myFake/" + mstudentId.getText() + "/Url";
I am Following this link to use the SharedPreferences.Am trying to Apply in my Application but The Shared value returns the null value
Here my code To assign the Shared Variable
SharedPreferences sharedPreferences = getSharedPreferences("pref",Activity.MODE_WORLD_READABLE);
SharedPreferences.Editor editor= sharedPreferences.edit();
String l="hello";
editor.putString(l,"imagepath");
editor.commit();
here code to access the Shared variable
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String prefName = myPrefs.getString("imagepath",null);
Toast.makeText(getBaseContext(),"create banner"+prefName,Toast.LENGTH_LONG).show();
here prefName returns null value.It Cannot Shared.
The preference name is "hello", not "imagepath".
String prefName = myPrefs.getString("hello", null);
And I think it's better to use getString("hello", ""). This way, prefName will never be null.
You have written "myPrefs" in (SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);)
Change it like this (SharedPreferences myPrefs = this.getSharedPreferences("pref", MODE_WORLD_READABLE);)
here i have change "myPrefs" in ("myPrefs", MODE_WORLD_READABLE) to "pref" .
Now you the coorect one be ("pref", MODE_WORLD_READABLE)
you are using wrong key.
get string like this:
String prefName = myPrefs.getString("hello",null);
you are also using two different sharedPreference name:
SharedPreferences sharedPreferences = getSharedPreferences("pref",Activity.MODE_WORLD_READABLE);
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
Please make sure you write and read in same SharedPreference.
Your code to write in SharedPreference should look like this:
SharedPreferences sharedPreferences = getSharedPreferences("pref",0);
SharedPreferences.Editor edito r= sharedPreferences.edit();
String l="hello";
editor.putString(l,"imagepath");
editor.commit();
To read from SharedPreference should look like this:
SharedPreferences myPrefs = this.getSharedPreferences("pref", 0);
String prefName = myPrefs.getString("hello",null);
Toast.makeText(getBaseContext(),"create banner"+prefName,Toast.LENGTH_LONG).show();
Switch this around:
editor.putString("imagepath", l);
also you will want to change the possible return value of "null" to a more acceptable error return value, maybe 0 or -1.
String prefName = myPrefs.getString("imagepath","0");
Source Android Doc for Editor:
abstract SharedPreferences.Editor putString(String key, String value)
//Set a String value in the preferences editor, to be written back once commit() or apply() are called.