0

I'm building an android app which requires me to prompt the user for his name the first time the user launches the app. During the second time onwards, the application won't prompt the user for his name instead it will greet the user instead(and straight away go to the main menu page. Does anybody know how I might do this?

user
  • 86,916
  • 18
  • 197
  • 190
honeybee
  • 9
  • 6

2 Answers2

3

Store the user's name in a preference field. When the app starts check and see if this preference field is set, if it isn't then prompt the user for a username and save it, if it is set then retrieve that username and use it in the activities you have:

@Override
protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    final SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(this);
    String userName = prefs.getString("user_name", null);
    if (userName == null) {
        EditText input = new EditText(this);
        input.setId(1000);          
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setView(input).setTitle("Enter your username!")
                .setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                EditText theInput = (EditText) ((AlertDialog) dialog)
                                        .findViewById(1000);
                                String enteredText = theInput.getText()
                                        .toString();
                                if (!enteredText.equals("")) {
                                    SharedPreferences.Editor editor = prefs
                                            .edit();
                                    editor.putString("user_name",
                                            enteredText);
                                    editor.commit();
                                }
                            }
                        }).create();
        dialog.show();
    } 
    // you are ready to use the user's username
user
  • 86,916
  • 18
  • 197
  • 190
  • Then how do I navigate this activity to another page? must I use a new intent? – honeybee Jul 21 '12 at 08:28
  • @honeybee You don't have to navigate from this activity. In each activity you have(or the first one the user will see when you start the app) you will place this code. If the user doesn't have a username the dialog will pop and the username will be saved, otherwise the user has stored a username and you use it to greet him. In other activities you'll simple get the string from the preferences and use it. – user Jul 21 '12 at 08:31
  • After the user key in his name, how do I direct to another page? say, the main menu page? sorry for asking too much questions because Im very new to android programming.. – honeybee Jul 21 '12 at 08:46
  • @honeybee If you want to direct the user to another page then you would use an `Intent`. – user Jul 21 '12 at 08:56
  • if lets I dont want to use the diaglog box but just the normal edit text with the text view "Enter user name ", then display the name to the next activity, how do i go about it? I know the codes reading the user input, directing to other activity, but im not sure how to store the username using shared pref and display it on the next activity... – honeybee Jul 23 '12 at 05:29
  • @honeybee Well, my code actually stores the entered text in the preferences(see the part about `SharedPreferences.Editor`). In the next activity you'll simply retrieve this text from the preferences (like in my code `prefs.getString...`). If you don't want the dialog box you could make your own dialog that looks the way you want or you could use a `PopupWindow` ( http://developer.android.com/reference/android/widget/PopupWindow.html ) – user Jul 23 '12 at 07:00
  • thank you so much for explaining the codes further! I understand it better now! – honeybee Jul 24 '12 at 13:05
1

Use this code:

        File file = new File("username.txt");

        // if file doesnt exists, then prompt username alert dialog
        if (!file.exists()) {
          //prompt for username
          file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        //suppose  EditText_UserName is your edittext where he can enter his name
        bw.write(EditText_UserName.getText().toString());
        bw.close();

Everytime you start the application, check for this file using file.exists and continue.

gkris
  • 1,209
  • 3
  • 17
  • 44
  • does this mean diff user can use the app? – honeybee Jul 21 '12 at 08:32
  • how do you direct this to another page? say, the menu page? – honeybee Jul 21 '12 at 08:42
  • Yes. This code will consider all the users as the same. If you want to switch user, you should have a logout kind of option, wherein you will change the contents of the file. But if you want to make sure that different users enter using their own name, then you should modify your requirement…it should be like the application suggests for a name everytime it is opened (not directly reading from file). What do you mean by direct to another page? If you mean a new activity, you can look (here)[http://stackoverflow.com/a/4186097/1322460] – gkris Jul 21 '12 at 08:49