I am developing an app in which i have two images namely "a" and "b" when user open activity i want to show image "a" and second time when user again open activity want to show image"b" same for 3 4 5 6. I have tried a lot and also googled but can't found any solution.
Below code will help you.
Create a Shared Preferences for store the value of index.Every time Activity will create index will increment and accordingly image would change.
public class ImageShow extends Activity{
Integer image[]={R.drawable.icon,R.drawable.ic_launcher,R.drawable.icon_audio};
int index=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedpreferences=this.getSharedPreferences("imagevalue", 0);
index=sharedpreferences.getInt("key", 0);
ImageView imv=new ImageView(this);
imv.setImageResource(image[index]);
Editor editor = sharedpreferences.edit();
editor.putInt("key", ++index);
editor.commit();
}
}
for this first you should put your images in your Drawble or assets folder after that you can show images in imageview and save its name in sharedpreferences as string...when user again open this activity you should check the sharedpreferences to find that which images seen with user and according that data decide to show next image to user...
Related
I have an application where app has both online & offline mode.
Once the fragment is loaded a network call is made and data is set.
The user makes some changes into fragment UI like adding buttons, Editing TextBoxes etc. I have to maintain that state throughout the application.
I have next & previous buttons, when i press on previous button on the fragment is reloading even though i tried to adding it to back stack while replacing it and calling getActivity().onBackPressed();.
Things I have Tried :
1) saving the values into bundles is too much data & hard ti retrieve due to bulk of data/values.
If you use viewpager, you can keep the last state of the fragments and it will not reload untill the offscreen limit u set as below.
mViewPager.setOffscreenPageLimit(limit); //limit: integer value >=1
If you have large number of data you want to save, use SQLite, otherwise you can use Shared Preferences
For more info visit this link
First get the reference to your shared preferences file
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
after this write to Shared Preferences with
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("key", "value");
//use editor.apply() if you want to save your data imedialtly
editor.commit();
when you want to retrieve the data (onCreateView/onViewCreated)
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
EditText editex = (EditText) view.findViewById(R.id.your_view_id);
editext.setText(sharedPref.getString("key", defaultValue))
}
But keep in mind, Shared Preferences will save and keep your data even if your app is killed/destoyed
i am new in android and stuck in very different situation where
a.> where i have to check that whether the app is installing first time or not if it is first time then save setting from the user
i have built this successfully using shared preferences but
now when i am running it 2nd or 3rd time the app doesn't saved the settings set in first run and comes with fresh default values
for eg .. i have selected and saved the state rajasthan from spinner but on second run it comes with 1st value in spinner(gujrat) i am using normal variables for this and not intializing with any default value
Try this code for save value -
boolean flag=false; //Declare an flag for first time install condition
private SharedPreferences preferences; //Declare sharedprefrence for stor local values for app.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
flag=preferences.getBoolean("KeyFlag", false);
if(flag)
{
// do code here for next time installed
}
else
{
// for first time installed you have to put true value in flag
Editor editor=preferences.edit();
editor.putBoolean("KeyFlag", true);
editor.commit(); //Don't forget to commit the editor
}
}
Hope this code helps you !!!!
if its is not working please let me know i will try to help you more.
From second time u need to compare the selected or saved values with your list(Arraylist or
array) and then take the position of the values and put spinner.setSelection(position). or
when you are save the selected position first time and second time pass the position to the
spinner
Is it possible to set image resource in onCreate method before loading layout.
For example:
On main layout I have images instead of buttons, so there is no button "Play", instead there is an image on which "Play" is drawn.
But, on other hand, user can change languages, so if user choses German there has to be different image on main layout.
I know how to change picture on some event, but is it possible to do it in onCreate method.
Logic would probably be something like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences options= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String language= options.getString("language", "en");
setLocal(language);
setContentView(R.layout.activity_main);
ImageView play= (ImageView)findViewById(R.id.play);
if(language=="en")
play.setImageResource(R.drawable.play_en);
else if(language=="de")
play.setImageResource(R.drawable.play_de);
}
Of course, this doesn't change the picture because image is already loaded, but I hope you get what I want to achieve.
What you wan't to do can be done using localization on your resources, I think this is te correct and easily way to do it.
You can check how, here: http://developer.android.com/guide/topics/resources/localization.html
I have an android application that displays, by default, a series of ImageButtons using xml. The user changes the image based on their input. I am trying to display the changed ImageButtons the next time the user loads the app.
Example:
ImageButton starts as Android.png (loaded from default xml page)
User enters text
ImageButton is changed to Correct.png
The next time that the app loads I want Correct.png to display instead of Android.png. Is there a way to iterate through the ImageButtons before the app starts (the buttons are NOT created programatically) to set the source value for each one before the application loads?
You can use findViewById(R.id.button_1) for each button and set it as needed. You'll then need to store which values it should have so that you can load these values and set the ImageButton source for each one. You can store it with either SharedPreferences or a SQLite database, depending on how much you have to change.
So, for example:
ImageButton b = (ImageButton)findViewById(R.id.button_1);
b.setImageDrawable(drawable);
You can load a drawable up for the checkbox so that you do it only once, which is more efficient, or you could setImageResource to R.drawable.correct, which would be a bit slower since I assume that the checkbox would probably be set for multiple images.
A better way to perform this by using SharedPreferences.
SharedPreferences sharedPref = getSharedPreferences("userpref", Activity.MODE_PRIVATE);
String image = pref.getString("userchoice","no-image");
if(image.equeals("no-image")
{
imageView.setBackgroundResource(R.drawable.android);
}
else
{
//compare here your image
imageView.setBackgroundResource(R.drawable.Correct);
}
And whenever user selects any other image, just edit your preference file by using following code.
SharedPreferences.Editor editor = pref.edit();
editor.putString("userchoice","Correct.png");
I’m a new developer, so my question maybe too much basic.
I look for example of defining preferences of sound. User can choose what kind of sound will start an application, for example. There can be such a RingtonPreference widjet, so user can choose a sound.
As I know, preferences support the primitive types: Boolean, string, float, long and integer. What way is the best to design preferences: store in entryValues the names if sounds (strings), the address of files from Resourse class (integer), or other way.
Please provide a short example of code.
Thanks in advance!
First of all thank you for the quick and detailed answer!
I want to arrange list of sounds: there must be one “None”, list of sounds that contains folder “raw”, option to add a new sound from different locations and two buttons: “set” and “cancel”. When user touches one item from the list – sound starts to play.
There is a little problem with standard widget that provide android library. “ListPreference” isn’t appropriate because on touch on one of the items – item is chosen and list closes, “there are not buttons set and cancel”.
“RingtonPreference” isn’t appropriate as well – I didn’t succeed to add something to list.
How is possible to build a custom preference layout and that is options that were chosen will be saved as well as on standard widgets. Please provide a short code example. Thanks in advance!
I think the best way to store the Resource are by integers. or you could do names.
I think integer is more reliable.
So to use SharedPreference with this you will need to get access to the apps SharedPreference
public class PreferencesDemo extends Activity {
SharedPreferences app_preferences;
private int resourceNumber;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the app's shared preferences
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
resourceNumber = app_preferences.getInt("resourceNumber", 0);
if(resourceNumber == 0){
//This means the user hasnt selected a song and you must act accordingly. Or put a resource number where the 0 is do set it to a default song
}
You will probably want to create a method to put the songs in the SharedPreference such as;
private void createSongResouces(){
SharedPreferences.Editor editor = app_preferences.edit();
//Here you can put as many songs as you want just make sure you call editor.commit(); as i do.
editor.putInt("resourceNumber", resourceNumber);
editor.commit(); // Very important
}