Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6817209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:01:19+00:00 2026-05-26T21:01:19+00:00

Hi i want to pass a value username which i saved through SharedPreferences in

  • 0

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();       
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:01:21+00:00Added an answer on May 26, 2026 at 9:01 pm

    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.

    public class MyDialog exteds Dialog implements Dialog.OnClickListener {
        OnTextChangeListener mListener;
        String mTextChange;
    
        public MyDialog(OnTextChangeListener listener) {
            mListener = listener;
            // set up your stuff
            setOnClickListener(this);
        }
    
        ...
    
        public void onClick(DialogInterface dialog) {
            // Do what ever you need to do to get set mTextChange
            mListener.onChange(mTextChange);
        }
    
        public static interface OnTextChangeListener {
            void onChange(String textChange);
    }
    

    Here is how you pass it:

     public void createDialog(Context context) {
         SharedPreferences prefs = context.getSharedPreferences("MyPreferencesName", Context.MODE_PRIVATE);
         String user = prefs.getString("what_ever_string_you_want", "fall_back_user_name");
            new MyDialog(user, this); // where the class that is calling this is aa OnTextChangeListener
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pass an integer value to a form in .Net so that
I want to pass an enum value as command parameter in WPF, using something
I want to pass a datetime value into my python script on the command
I want to pass a number value to a Timer. How do I do
I want to .append to a div and pass the value on as html
I want to pass in the tType of a class to a function, and
I want to call a Stored Procedure which takes two parameters(username & password) and
Hi i have a inlogscreen (inlogdialog.xml), which includes 2 EditText (username, passwd) and i
I have problem to pass value via url parameter to other page. I want
Hi I want to pass my userName and Password to WCF Webservice from iPhone.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.