i have a app in which brightness is save in profile. profile active when selected wifi is connected . i save the setting of brightness with shareprefrences.when i save and connected to wifi in the seekbar show value same which i set on it but its brightness does not increase or decrease which i set in setting . brightness increase or decrease only when i change it manually with seekbar.please tell me where i do mistake here is the code of activity.
public class Profile1Activity extends Activity {
//TextViews to show details of volume and brightness
private TextView tVBrightness, tVVolume;
//SeekBars to set volume and brightness
private SeekBar sbVolume, sbBrightness = null;
//Variable to store brightness value
private int brightness = 1;
//Content resolver used as a handle to the system's settings
private ContentResolver cResolver;
//Window object, that will store a reference to the current window
private Window window;
EditText e1;
Button b1;
public static final String MyPREFERENCESSS = "MyPrefsss";
public static final String HOMEWIFI = "homewifi";
Context context = this;
SharedPreferences sharedpreferences;
public static final String BRIGHTNESSS = "brightnessProfile1";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile1);
sbBrightness = (SeekBar) findViewById(R.id.sbBrightness1);
tVBrightness = (TextView) findViewById(R.id.tVBrightness);
e1 = (EditText) findViewById(R.id.ed2);
b1 = (Button) findViewById(R.id.but2);
sharedpreferences = getSharedPreferences(MyPREFERENCESSS, Context.MODE_PRIVATE);
final String homewifi = sharedpreferences.getString(HOMEWIFI, "");
e1.setText(homewifi);
sbBrightness.setProgress(sharedpreferences.getInt(BRIGHTNESSS, 0));
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String homewifi = e1.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(HOMEWIFI, homewifi);
//Suhas
editor.putInt(BRIGHTNESSS, sbBrightness.getProgress());
editor.commit();
Toast.makeText(Profile1Activity.this, "Thanks", Toast.LENGTH_SHORT).show();
}
});
////////////////////////////////////////////////////////////////////////////////////
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final WifiInfo conn = wifiManager.getConnectionInfo();
//Toast.makeText(MainActivity.this, con.getSSID()+"",Toast.LENGTH_LONG).show();
if (conn.getSSID().toString().equalsIgnoreCase("\"" + homewifi + "\"")) {
initializeControls1();
sbBrightness.setProgress(sharedpreferences.getInt(BRIGHTNESSS, 0));
}
}
handler.postDelayed(this, 100);
}
}, 100);
}
private void initializeControls1() {
//Get the content resolver
cResolver = getContentResolver();
//Get the current window
window = getWindow();
//Set the seekbar range between 0 and 255
sbBrightness.setMax(255);
//Set the seek bar progress to 1
sbBrightness.setKeyProgressIncrement(1);
try
{
//Get the current system brightness
brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
}
catch (SettingNotFoundException e)
{
//Throw an error case it couldn't be retrieved
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}
//Set the progress of the seek bar based on the system's brightness
sbBrightness.setProgress(brightness);
//Register OnSeekBarChangeListener, so it can actually change values
sbBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar seekBar)
{
//Set the system brightness using the brightness variable value
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
//Get the current window attributes
LayoutParams layoutpars = window.getAttributes();
//Set the brightness of this window
layoutpars.screenBrightness = brightness / (float)255;
//Apply attribute changes to this window
window.setAttributes(layoutpars);
run1();
}
public void onStartTrackingTouch(SeekBar seekBar)
{
//Nothing handled here
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
//Set the minimal brightness level
//if seek bar is 20 or any value below
if(progress<=20)
{
//Set the brightness to 20
brightness=20;
}
else //brightness is greater than 20
{
//Set brightness variable based on the progress bar
brightness = progress;
}
//Calculate the brightness percentage
float perc = (brightness /(float)255)*100;
//Set the brightness percentage
tVBrightness.setText("Brightness: "+(int)perc +" %");
}
});
}
public void run1(){
String homewifi = e1.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(HOMEWIFI, homewifi);
//Suhas
editor.putInt(BRIGHTNESSS, sbBrightness.getProgress());
editor.commit();
// Toast.makeText(Profile1Activity.this, "Thanks", Toast.LENGTH_SHORT).show();
}
}
set brightness also at a time you set value of seekbar in prefrance
Add this code... only this much code is needed to increase and set default value of phone brightness to seekbar when your profile fragment or activity loads:
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
seekBar.setMax(255);
float curBrightnessValue = 0;
try {
curBrightnessValue = android.provider.Settings.System.getInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
int screen_brightness = (int) curBrightnessValue;
seekBar.setProgress(screen_brightness);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progress = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
progress = progresValue;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do something here,
// if you want to do anything at the start of
// touching the seekbar
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
progress);
}
});
}
Related
I'm working in android studios and I have this app that displays an imageView and has seekbars that allow you to edit the RGB values of the photo, but when I rotate from portrait to landscape (or vice versa) the imageView doesnt save and goes back to the original imageView and not the one that I selected. The Seekbars keep their values though. Any ideas on how I can used shared preferences to save the image? Thanks
public class MainActivity extends AppCompatActivity {
////////////////////////////////////////////////////////////////////////////
TextView textTitle;
ImageView image;
SeekBar barR, barG, barB, barAlpha;
int ColorValues;
private static final int SELECT_PHOTO = 100;
private static final int CAMERA_PIC_REQUEST = 101;
private static final String PREFS_NAME = "PrefsFile";
private static final String COLOR_VALUES = "ColorVals";
private static final String PICTURE = "Picture";
///////////////////////////////////////////////////////////////////////////
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTitle = (TextView)findViewById(R.id.title);
image = (ImageView)findViewById(R.id.image);
barR = (SeekBar)findViewById(R.id.red);
barG = (SeekBar)findViewById(R.id.green);
barB = (SeekBar)findViewById(R.id.blue);
barAlpha = (SeekBar)findViewById(R.id.alpha);
barR.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
barG.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
barB.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
barAlpha.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
//default color
ColorValues = barAlpha.getProgress() * 0x1000000
+ barR.getProgress() * 0x10000
+ barG.getProgress() * 0x100
+ barB.getProgress();
image.setColorFilter(ColorValues, Mode.MULTIPLY);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
ColorValues = settings.getInt(COLOR_VALUES, ColorValues);
}
//menu/////////////////
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.savePicture:
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
MediaStore.Images.Media.insertImage
(getContentResolver(), bitmap, "yourTitle", "yourDescription");
}
return true;
}
//on click function////////////////
public void selectPicture(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
public void takePicture(View view){
try{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
} catch(Exception e){
e.printStackTrace();
}
}
//activity///////////////
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
image.setImageURI(selectedImage);// To display selected image in image view
break;
}
case CAMERA_PIC_REQUEST:
try {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
thumbnail = Bitmap.createScaledBitmap(thumbnail, 700, 500, true);
ImageView imageView = (ImageView) findViewById(R.id.image);
image.setImageBitmap(thumbnail);
} catch (Exception ee) {
ee.printStackTrace();
}
break;
}
}
//warning before exit
#Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Closing Activity")
.setMessage("Are you sure you want to close\nthis activity? \n\nProgress will NOT be saved ")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null);
AlertDialog dialog = builder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
dialog.show();
}
//edit picture
OnSeekBarChangeListener myOnSeekBarChangeListener = new OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
//Colorize ImageView
int newColor = barAlpha.getProgress() * 0x1000000
+ barR.getProgress() * 0x10000
+ barG.getProgress() * 0x100
+ barB.getProgress();
image.setColorFilter(newColor, Mode.MULTIPLY);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
ColorValues = newColor;
editor.putInt(COLOR_VALUES, ColorValues);
// Commit the edits
editor.commit();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {}
};
#Override
protected void onSaveInstanceState(Bundle icicle) {
super.onSaveInstanceState(icicle);
icicle.putInt(COLOR_VALUES, ColorValues);
}
this is what it would look like if I saved the colorvalues(which are somehow already being saved without this code)
if (savedInstanceState != null) {
colorvalues = savedInstanceState.getInt("COLOR_VALUES");
image = savedInstanceState.get??????????????????????
}
image is not an Int so what values should be put there?
also to get the exact RGB values like RBar = savedInstanceState.getInt doesnt works becuase Rbar is a seekbar not an int???
I want to use the progress variable of onProgressChanged method outside of the inner class. I tried to use a method defined in outer class and called from inner class to get that value. But every-time I get 0.
No updated value of progress variable. It is basically an android app to get the updated value of SeekBar.
Have a look at the code for better understanding. Currently I am only getting previous/last value of progress. Not getting updated value immediately as I change it.
Button gene;
ImageView image;
SeekBar ring_bar;
String collect;
private AudioManager am;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prof);
gene = (Button) findViewById(R.id.gene);
image = (ImageView) findViewById(R.id.image);
ring_bar = (SeekBar) findViewById(R.id.ring_bar);
am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxV = am.getStreamMaxVolume(AudioManager.STREAM_RING);
int curV = am.getStreamVolume(AudioManager.STREAM_RING);
ring_bar.setMax(maxV);
ring_bar.setProgress(curV);
ring_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
am.setStreamVolume(AudioManager.STREAM_RING, progress, 0);
}
});
int ring = ring_bar.getProgress();
String ringgg = Integer.toString(ring);
collect = ringgg;
gene.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try{
BitMatrix bitMatrix = multiFormatWriter.encode(collect, BarcodeFormat.QR_CODE,200,200);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
image.setImageBitmap(bitmap);
}
catch (WriterException e){
e.printStackTrace();
}
}
});
}
}
Your problem is, that you save the progress in onCreate to your variable but you never update the value after that.
#Override
protected void onCreate(Bundle savedInstanceState) {
//....
int ring = ring_bar.getProgress();//Progress at the time of onCreate
gene.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try{
//collect is still the same value since onCreate
BitMatrix bitMatrix = multiFormatWriter.encode(collect, BarcodeFormat.QR_CODE,200,200);
//....
}
catch (WriterException e){
e.printStackTrace();
}
}
});
In order to create a QR for the current value you have to call getProgress() in the onClickListener. This will return you the current value of the progress bar.
gene.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try{
int ringtoneVolumeValue = ring_bar.getProgress();
String ringtoneVolume = Integer.toString(ringtoneVolumeValue);
BitMatrix bitMatrix = multiFormatWriter.encode(ringtoneVolume, BarcodeFormat.QR_CODE,200,200);
//....
}
catch (WriterException e){
e.printStackTrace();
}
}
});
private void onTextSizeSeekBarChange() {
final TextView tutorialText = (TextView) findViewById(R.id.tutorialText);
final SeekBar sb = (SeekBar) findViewById(R.id.seekBar);
sb.setMax(20);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int p = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
p = progress;
tutorialText.setTextSize(p);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (p < 20) {
sb.setProgress(p);
}
}
});
}
So the problem is that the code I have above changes the text size of a text view. However when going back and forth the state isn't saved so the text size is then reset. I am really new to android and would appreciate any guidance or help on how I can achieve this. I basically have a tutorial app and the text needs to change from the settings menu which has a seekbar allowing the user to select what text size they want. Any Ideas?
You can do either in this way :
1)you can save the value of p in shared pref and when you come next time you will check the value of p and set it on textview
2)you can save the value in onSavedInstance,but the problem is that when user kill the app from background you are going to loose the value
if you have set sb.setMax(20); then you dose not need to
if (p < 20) {
sb.setProgress(p);
}
code is useless user cant seek more then 20
you have code like that
private void onTextSizeSeekBarChange() {
final TextView tutorialText = (TextView) findViewById(R.id.tutorialText);
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(CONTEXT);
int p = SP.getInt("key", p);
tutorialText.setTextSize(p);
final SeekBar sb = (SeekBar) findViewById(R.id.seekBar);
sb.setMax(20);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
p = progress;
tutorialText.setTextSize(p);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
sb.setProgress(p);
Editor edit = SP.edit();
edit.putInt("key", p);
edit.commit();
}
});
}
I am using togglebutton to switch between images obtained using Intent (selected from sdcard). However i have an error after i select image. Basically my idea is to browse an image and display on (originalimage) Imageview based on the state of togglebutton. Moreover when i change the state of togglebutton i should see different images.
public class LoadImage extends AppCompatActivity {
public static final int REQUEST_CODE = 10;
ToggleButton togglebtn;
Bitmap imgb;
Button browseimagebtn;
Bitmap operation;
ImageView originalimage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_load);
originalimage = (ImageView) findViewById(R.id.originalimage);
browseimagebtn = (Button) findViewById(R.id.browseimagebtn);
//browse image button clicked
browseimagebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent rawIntent = new Intent(Intent.ACTION_PICK);
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
String pictureDirectoryPath = pictureDirectory.getPath();
Uri data=Uri.parse(pictureDirectoryPath);
rawIntent.setDataAndType(data, "image/*");
startActivityForResult(rawIntent, REQUEST_CODE);
}
});
//browse image button long pressed
browseimagebtn.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(getApplicationContext(), "Loads image from Picture Gallery", Toast.LENGTH_SHORT).show();
return true;
}
});
//toggle switch to decide which image to be displayed on screen
togglebtn = (ToggleButton) findViewById(R.id.togglebtn);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK){
if(requestCode==REQUEST_CODE){
final float red = (float) 0.299;
final float green = (float) 0.587;
final float blue = (float) 0.114;
final Uri imagef = data.getData();
InputStream streamI;
try {
streamI = getContentResolver().openInputStream(imagef);
//Create bitmap from selected image
Bitmap imgb = BitmapFactory.decodeStream(streamI);
//Define rows and columns of selected image
int rows = imgb.getHeight();int cols = imgb.getWidth();
operation = Bitmap.createBitmap(cols, rows, imgb.getConfig());
//Convert original image to Gray Image
for (int i=0;i<cols;i++){
for(int j=0;j<rows;j++){
int p = imgb.getPixel(i,j);
int r = Color.red(p);
int g = Color.green(p);
int b = Color.blue(p);
r = (int) (red*r);
g = (int) (green*g);
b = (int) (blue*b);
int gray = (int) (r*0.299+g*0.587+b*0.114);
operation.setPixel(i, j, Color.argb(Color.alpha(p), gray, gray, gray));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();}
}
togglebtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "if is checked", Toast.LENGTH_SHORT).show();
originalimage.setImageBitmap(operation);
} else {
Toast.makeText(getApplicationContext(), "else is checked", Toast.LENGTH_SHORT).show();
originalimage.setImageBitmap(imgb);
}
}
});
}
}
}
Not sure if this fixes everything but:
Bitmap imgb -> will always be null.
Fix:
In your onActivityResult:
try {
streamI = getContentResolver().openInputStream(imagef);
//Create bitmap from selected image
imgb = BitmapFactory.decodeStream(streamI);
.....
}
Included Android permission.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
I want to change the background colour of an activity using AlertDialog. Here is what I have done:
private SharedPreferences prefs;
private static final String SELECTED_ITEM = "SelectedItem";
private Editor sharedPrefEditor;
btnchangeColor = (ImageButton) findViewById(R.id.btnchangeColor);
btnchangeColor.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final CharSequence[] items={"Red","Green","Blue", "Yellow", "#EE82EE"};
AlertDialog.Builder builder = new AlertDialog.Builder(
ContentView_2.this);
builder.setTitle("Pick a Color");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {}
});
builder.setSingleChoiceItems(items, getSelectedItem(), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
wvContent = (WebView) findViewById(R.id.wvContent);
//LinearLayout ll=(LinearLayout)findViewById(R.id.wvContent);
if("Red".equals(items[which]))
{
wvContent.setBackgroundColor(Color.RED);
}
else if("Green".equals(items[which]))
{
wvContent.setBackgroundColor(Color.GREEN);
}
else if("Blue".equals(items[which]))
{
wvContent.setBackgroundColor(Color.BLUE);
}
else if("Yellow".equals(items[which]))
{
wvContent.setBackgroundColor(Color.YELLOW);
}
else if("#EE82EE".equals(items[which]))
{
wvContent.setBackgroundColor(Color.rgb(184,184,184));
}
saveSelectedItem(which);
}
});
builder.show();
}});
}
private int getSelectedItem() {
if (prefs == null) {
prefs = PreferenceManager
.getDefaultSharedPreferences(this);
}
return prefs.getInt(SELECTED_ITEM, -1);
}
private void saveSelectedItem(int which) {
if (prefs == null) {
prefs = PreferenceManager
.getDefaultSharedPreferences(this);
}
sharedPrefEditor = prefs.edit();
sharedPrefEditor.putInt(SELECTED_ITEM, which);
sharedPrefEditor.commit();
}
I also add this code to OnCreate:
wvContent = (WebView) findViewById(R.id.wvContent);
wvContent.setBackgroundColor(getSelectedItem());
The colour is selected and the web view (wvContent) changes to chosen colour, BUT this is not saved to and/or loaded from Shared Preferences. It returns to its default colour when the activity is relaunched.
So the question is: How can properly I save the selected colour to SharedPreferences and load it when activity relaunches?
Many thanks for help.
Currently you are saving position of selected item instead of color code in SharedPreferences. do it as:
wvContent = (WebView) findViewById(R.id.wvContent);
int bg_color=Color.TRANSPARENT;
if("Red".equals(items[which]))
{
wvContent.setBackgroundColor(Color.RED);
bg_color=Color.RED;
}
else if("Green".equals(items[which]))
{
wvContent.setBackgroundColor(Color.GREEN);
bg_color=Color.GREEN;
}
........
// save color in SharedPreferences
saveSelectedItem(bg_color);
Now getSelectedItem() method return color code for previously selected value from spinner.