I've currently got a preferences screen, and I've created a custom class that extends DialogPreference and is called from within my Preferences. My preferences data seems store/retrieve from SharedPreferences without an issue, but I'm trying to add 2 more sets of settings from the DialogPreference.
Basically I have two issues that I have not been able to find. Every site I've seen gives me the same standard info to save/restore data and I'm still having problems. Firstly I'm trying to save a username and password to my SharedPreferences (visible in the last block of code) and if possibly I'd like to be able to do it in the onClick().
My preferences XML that calls my DialogPreference:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory>
<com.rone.optusmon.AccDialog
android:key="AccSettings"
android:title="Account Settings"
android:negativeButtonText="Cancel"
android:positiveButtonText="Save" />
</PreferenceCategory>
</PreferenceScreen>
My Preference Activity Class:
package com.rone.optusmon;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.view.KeyEvent;
public class EditPreferences extends PreferenceActivity {
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
My Custom DialogPreference Class file:
package com.rone.optusmon;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class AccDialog extends DialogPreference implements DialogInterface.OnClickListener {
private TextView mUsername, mPassword;
private EditText mUserbox, mPassbox;
CharSequence mPassboxdata, mUserboxdata;
private CheckBox mShowchar;
private Context mContext;
private int mWhichButtonClicked;
public AccDialog(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
@Override
protected View onCreateDialogView() {
@SuppressWarnings("unused")
LinearLayout.LayoutParams params;
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(10, 10, 10, 10);
layout.setBackgroundColor(0xFF000000);
mUsername = new TextView(mContext);
mUsername.setText("Username:");
mUsername.setTextColor(0xFFFFFFFF);
mUsername.setPadding(0, 8, 0, 3);
mUserbox = new EditText(mContext);
mUserbox.setSingleLine(true);
mUserbox.setSelectAllOnFocus(true);
mPassword = new TextView(mContext);
mPassword.setText("Password:");
mPassword.setTextColor(0xFFFFFFFF);
mPassbox = new EditText(mContext);
mPassbox.setSingleLine(true);
mPassbox.setSelectAllOnFocus(true);
mShowchar = new CheckBox(mContext);
mShowchar.setOnCheckedChangeListener(mShowchar_listener);
mShowchar.setText("Show Characters");
mShowchar.setTextColor(0xFFFFFFFF);
mShowchar.setChecked(false);
if(!mShowchar.isChecked()) {
mPassbox.setTransformationMethod(new PasswordTransformationMethod());
}
layout.addView(mUsername);
layout.addView(mUserbox);
layout.addView(mPassword);
layout.addView(mPassbox);
layout.addView(mShowchar);
return layout;
// Access default SharedPreferences
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
}
public void onClick(DialogInterface dialog, int which) {
mWhichButtonClicked = which;
// if statement to set save/cancel button roles
if (mWhichButtonClicked == -1) {
Toast.makeText(mContext, "Save was clicked", Toast.LENGTH_SHORT).show();
mUserboxdata = mUserbox.getText();
mPassboxdata = mPassbox.getText();
// Save user preferences
SharedPreferences settings = getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putString("usernamekey", (String) mUserboxdata);
editor.putString("passwordkey", (String) mPassboxdata);
}
else {
Toast.makeText(mContext, "Cancel was clicked", Toast.LENGTH_SHORT).show();
}
}
}
In my SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); line, Eclipse says "The method getDefaultSharedPreferences(AccDialog) is undefined for the type AccDialog". I've attempted to change the context to my preferences class, use a blank context and I've also tried naming my SharedPrefs and using "getSharedPreferences()" as well. I'm just not sure exactly what I'm doing here.
As I'm quite new to Java/Android/coding in general, could you please be as detailed as possible with any help, eg. which of my files I need to write the code in and whereabouts in that file should I write it (i.e. onCreate(), onClick(), etc)
Edit: I will need to the preferences to be Application-wide accessible, not activity-wide.
Thanks