Reputation: 169
I am writing a simple program which involves color changing of certain elements. For color changing, I have to store the color value (which I get from the color picker) in the SharedMemory. I have the following color picker code:
public class MainActivity extends Activity implements OnColorChangedListener {
private ColorPicker picker;
private SVBar svBar;
private OpacityBar opacityBar;
private Button button;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
picker = (ColorPicker) findViewById(R.id.picker);
svBar = (SVBar) findViewById(R.id.svbar);
opacityBar = (OpacityBar) findViewById(R.id.opacitybar);
button = (Button) findViewById(R.id.button1);
text = (TextView) findViewById(R.id.textView1);
picker.addSVBar(svBar);
picker.addOpacityBar(opacityBar);
picker.setOnColorChangedListener(this);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Color",""+ picker.getColor());
text.setTextColor(picker.getColor());
picker.setOldCenterColor(picker.getColor());
}
});
}
@Override
public void onColorChanged(int color) {
//gives the color when it's changed.
}
}
How can I store the value of picker.getColor() in the SharedMemory?
Upvotes: 0
Views: 969
Reputation: 1194
User SharedPreference to store your color value.
Shared preference is place where you can store data that persist till the time app exists on device and if user does not clear the app memory through settings.
If you want to store array of colors,refer this post: https://stackoverflow.com/a/12350878/2035885
If just a single value needs to be stored, refer below:
public static int getColor(Context context)
{
SharedPreferences sharedPreference = context.getSharedPreferences("your_shared_preference_name",Context.MODE_PRIVATE);
return sharedPreference.getInt("key_to_use_to_set_and_retrieve_value", 0);
}
public static void setColor(Context context,int color)
{
SharedPreferences store = context.getSharedPreferences(MenuActivity.class.getSimpleName(),Context.MODE_PRIVATE);
SharedPreferences.Editor edit = store.edit();
edit.putInt("key_to_use_to_set_and_retrieve_value", color);
edit.commit();
}
Upvotes: 1
Reputation: 5260
java was follows
package com.example.sharedpreferenceexample;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
public class Utils {
public static void savePreferences(Activity activity, String key1,
int value1) {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(activity.getApplicationContext());
Editor editor = sp.edit();
editor.putInt(key1, value1);
editor.commit();
}
public static int readPreferences(Activity activity, String key,
int defaultValue) {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(activity.getApplicationContext());
return sp.getInt(key, defaultValue);
}
}
and use it as follows
to save
Utils.savePreferences(MainActivity.this, "colorKey", colorValue);
to read
Utils.readPreferences(MainActivity.this, "colorKey", 0)
Upvotes: 0
Reputation: 8946
@Override
public void onColorChanged(int color) {
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.mycolor), color);
editor.commit();
}
To get the stored color from Shared Preference use this
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int storedColor = sharedPref.getInt(getString(R.string.mycolor), 0);
Upvotes: 1
Reputation: 24848
// try this way,hope this will help you...
//Set To SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("yourSharePreferenceName", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("pickerColor",picker.getColor() );
editor.commit();
//Get From SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("yourSharePreferenceName", MODE_PRIVATE);
picker.setOldCenterColor(sharedPreferences.getInt("pickerColor",0));
Upvotes: 0
Reputation: 38605
Store it as an int
. That's already what it is.
@Override
public void onColorChanged(int color) {
// omitted: get a SharedPreferences object first
sharedPreferences.edit().putInt("color", color).apply();
}
Upvotes: 0