2

After installing the app, an alertDialog will pop-out. I wanted it to have a edit text field asking for the name of the user. After answering, it will never show again. Then afterwards, every time I open the app, another alertDialog will pop-out like a greeting to the user.

public class MainActivity extends Activity {
    final Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle("Welcome");
        alert.setMessage("Enter Your Name Here"); 
        final EditText input = new EditText(context);
        alert.setView(input);
        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            } 
        });

        AlertDialog alertDialog = alert.create();
        alertDialog.show();

        AlertDialog.Builder a_builder = new AlertDialog.Builder(MainActivity.this);
        a_builder.setMessage("Mabuhay!")
                .setCancelable(true)
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = a_builder.create();
        alert.show();
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
kcNeko
  • 587
  • 1
  • 7
  • 23
  • I have edited this post to clean it up a bit. You should avoid using the code snippet for code that is not web-based (HTML/CSS/Javascript), as it is only useful if you can run the code in-browser. For Java, simply paste your code, select it, and use the `{}` button in the editor to set it as a code block. See [here](https://stackoverflow.com/help/formatting) for more about formatting posts on Stack Overflow. – Dan Lowe Jan 04 '16 at 00:40

2 Answers2

1

After the user has entered his name on the first open of your app, you can write the given username into the SharedPreferences of your Application.

When a user now opens the app at any other time, you can simply check if you have an entry in the sharedpreferences of your application, and greet the user with the appropriate user name.

//instance variable in the Mainactivity.class
private boolean showMessage = true;

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String username = sharedPref.getString("username", null);
if(username == null){
    //first time user - ask for username
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("username", enteredName);
    editor.commit();
    showMessage = false;
} else if(showMessage) {
    showMessage = false;
    //greet
    AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
    alert.setMessage("Hello " + username + "!")
            .setCancelable(true)
            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
    alert.create().show();
}
DZDomi
  • 1,685
  • 15
  • 13
  • OMG this actually work! :D Thanks a lot! You're my savior! I actually wasted hours on this one. Just one more question, how can I get the string username that I just input transferred to the greetings part? Like "Hello! *insert name here*" – kcNeko Jan 02 '16 at 00:55
  • And also, every time I return to the MainActivity, the greetings pops-out again. How can I manipulate that the greetings can be only done whenever I first run the app? – kcNeko Jan 02 '16 at 01:07
  • for first question see edited post. To check that the greetings message only shows at the start of the application simply add a instance variable boolean flag to your MainActivity. If the flag is true simple print the message and set it to false after printing. Now also edited in the answer. – DZDomi Jan 02 '16 at 01:12
  • do you want to print the hello message also if the user hast just entered the user name? Because then small changes need to be made in the code – DZDomi Jan 02 '16 at 01:18
  • i guess the time that the user entered their name has the exception, then it will show at the second time onward.. sorry if I'm making this a little too hard for you. :) – kcNeko Jan 02 '16 at 01:36
  • oh sry i missed one important line. the code should now work. – DZDomi Jan 02 '16 at 01:39
  • Again... Thanks a lot, it works like a charm :D You just saved my grades there! – kcNeko Jan 02 '16 at 02:09
1

You can use two methods for do this. The first is by using the alert dialog like your example, you can find here how use dialogs.

Dialogs by Android Developer.

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_message)
       .setTitle(R.string.dialog_title);
AlertDialog dialog = builder.create();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User clicked OK button
           }
       });
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User cancelled the dialog
           }
       });
// Set other dialog properties
...

// Create the AlertDialog
AlertDialog dialog = builder.create();

Or you can create an anctivity dialog by setting in the manifest the dialog theme, and you can find it here.

for further explanations let me know!

Community
  • 1
  • 1
Dario
  • 732
  • 7
  • 30