Hi i want to pass a value “username” which i saved through SharedPreferences in my activity to a EditText in a CustomDialog class.
Meaning i want to use EditText.settext (username) in my CustomDialog class.
Can somebody help me to achieve this?
Thank you.
SharedPreference code in Main activity:
public class AndroidLogin extends Activity implements OnClickListener {
public SharedPreferences mPrefs;
...
@Override
protected void onPause() {
super.onPause();
Editor e = mPrefs.edit();
e.putString(USERNM, username);
e.commit();
}
}
Now i want to set the value “USERNM” to the EditText in my CustomDialog class:
public class MyCustomForm extends Dialog {
public SharedPreferences mPrefs;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.sencide.R.layout.inlogdialog);
EditText userTest = (EditText)findViewById(R.id.txtUserName);
mPrefs = getSharedPreferences("PREFS_NAME", this.MODE_PRIVATE); // does not work
String text = mPrefs.getString("USERNM", "");
userTest.setText(text);
}
}
CustomDialog:
import android.app.Dialog;
...
public class MyCustomForm extends Dialog {
String mTextChange;
public SharedPreferences mPrefs;
public interface ReadyListener {
public void ready(String user, String pass, boolean save);
}
private String usernm;
private String passwd;
private ReadyListener readyListener;
public MyCustomForm(Context context, String user, String pass, ReadyListener readyListener) {
super(context);
this.usernm = user;
this.passwd = pass;
this.readyListener = readyListener;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.sencide.R.layout.inlogdialog);
setTitle("Login:");
EditText userTest = (EditText)findViewById(R.id.txtUserName);
//mPrefs = getSharedPreferences("PREFS_NAME", this.MODE_PRIVATE);
//String text = mPrefs.getString("USERNM", "");
userTest.setText();
Button buttonConfrim = (Button) findViewById(R.id.btnConfirm);
buttonConfrim.setOnClickListener(new OKListener());
Button buttonCancel = (Button) findViewById(R.id.btnCancel);
buttonCancel.setOnClickListener(new CancelListener());
}
private class OKListener implements android.view.View.OnClickListener {
public void onClick(View v) {
EditText user = (EditText) findViewById(R.id.txtUserName);
EditText pass = (EditText) findViewById(R.id.txtpass);
CheckBox saveuser = (CheckBox) findViewById(R.id.saveuser);
readyListener.ready(user.getText().toString(),pass.getText().toString(), saveuser.isChecked());
MyCustomForm.this.dismiss();
}
}
private class CancelListener implements android.view.View.OnClickListener {
public void onClick(View v) {
MyCustomForm.this.dismiss();
}
}
}
Edit, in my main activity:
public void createDialog(Context context) {
SharedPreferences prefs = context.getSharedPreferences("mPrefs", Context.MODE_PRIVATE);
String user = prefs.getString("USERNM", "");
new MyCustomForm(context, user, user, null); // where the class that is calling this is aa OnTextChangeListener
}
This is how i show the Dialog:
public void getLoginData() throws Exception {
if (checkInternetConnection() == true){
MyCustomForm dialog = new MyCustomForm (this, "", "", new OnReadyListener());
dialog.setContentView(R.layout.inlogdialog);
dialog.show();
}
It’s not recommended to pass context around (and by extension views). I would suggest creating a listener for your dialog, maybe something like an
OnTextChangedListener#onChange(String).Create the listener in your activity and pass it to your dialog. Then when the dialog is done, call the listeners
onChange(String)method which will be set to fire an event to a UI thread handler to update the edit text. Conversely, you could just pass the handler.Here is how you pass it: