Reputation: 609
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();
}
}
Upvotes: 2
Views: 854
Reputation: 1679
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();
}
Upvotes: 1
Reputation: 757
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.
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!
Upvotes: 1