How to let alertDialog pop-out every time I run my app? - java

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();
}
}

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();
}

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!

Related

Create an alertdialog which include a list

How can I make a list on AlertDialog which include some radiobuttons in that , and when I clicked on items of the AlertDialog , do something like toast .
Try this:
//Setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Choose an animal");// add a radio button list
String[] animals = {"horse", "cow", "camel", "sheep", "goat"};
int checkedItem = 1; // cow
builder.setSingleChoiceItems(animals, checkedItem, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Here you can implement Toast Message
}
});
//Add OK and Cancel buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Handle when user clicks OK
}
});
builder.setNegativeButton("Cancel", null); //Create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
Let me know if that helped :)

Why crash my android app without any exception on showing an alert dialog?

I'm currently developing an android app. In this app should an alert dialog ask for an username and a password. The problem is, that the show-method causes a crash of the app. So I have tried to show a simple alert dialog; but there was the same problem. The wired this is, that the app closes without any exception.
Here is the code of the showLoginWindow:
public class MainScreen extends AppCompatActivity {
// other code
private void showLoginWindow(boolean falseLogin){
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.login, null);
TextView tv = (TextView) promptView.findViewById(R.id.Message3);
if(falseLogin) tv.setText("Falscher Benutzername oder falsches Passwort. Bitte wiederholen.");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptView);
final EditText uName = (EditText) promptView.findViewById(R.id.username);
final EditText password = (EditText) promptView.findViewById(R.id.password);
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
login(uName.getText().toString(), password.getText().toString());
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
}
And here is the code of the simple dialog (also in MainScreen):
private void testIt(){
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
builder.setTitle("test")
.setMessage("test dialog")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("no", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
I hope you can help me and sorry for my bad language.

submit EditText and display result in AlertDialog TextView

I am very new to Java and I'm trying to keep things simple. Why doesn't this work? I have an XML layout with a EditText field and a Submit Button. I want to press it, an AlertDialog to pop up with the TextView of what I inputted into the EditText. What I tried keeps crashing:
//CustomAlertDialogPopUp
public void submitButton(View v) {
LayoutInflater inflater = getLayoutInflater();
View alertLayout = inflater.inflate(R.layout.alertXML, null);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Alert");
alert.setView(alertLayout);
AlertDialog dialog = alert.create();
dialog.show();
EditText text1 = (EditText)findViewById(R.id.inputText);
TextView text2 = (TextView)findViewById(R.id.alertText);
String result = text1.getText().toString();
text2.setText(result);
}
Sorry if I sound stupid! Like I say, super new.
If you want to keep it simple, this will work:
For custom AlertDialog view you can refer to this question
public void submitButton(View v){
new AlertDialog.Builder(this)
.setTitle("Message")
.setMessage(text1.getText().toString())
.setPositiveButton("Dismiss", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.show();
}

Checkbox dialog not working

When I write this code, it only shows dialog with one button, but not showing check boxes.
I don't know what's the problem.
private void showDialog(){
final ArrayList selectedItems = new ArrayList();
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("asdasd");
dialogBuilder.setMessage("asdasd");
final String[] options = {"asd", "dsa","asd","aa"};
dialogBuilder.setMultiChoiceItems(options, null, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
selectedItems.add(which);
}else if(selectedItems.contains(which)){
selectedItems.remove(Integer.valueOf(which));
}
}
});
dialogBuilder.setNegativeButton("CANCEL",null);
AlertDialog dialog = dialogBuilder.create();
dialog.show();
};
Just see this link, and here is some code:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color);
.setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}
Hope you can solve your problem.

Hyperlinking the Phone number in AlertDialog of Android

I have an AlertDialog having the text "For more information, Please Call 1-800-200-1000".
Here is my Code for the Alert Dialog Display while clicking on the ListView Item :
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final SpannableString s = new SpannableString("1-800-200-1000");
Linkify.addLinks(s, Linkify.ALL);
ListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
builder.setMessage("For more information, Please Call "+s)
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}});
Here i wish to hyperlink the "1-800-200-1000" and while clicking on this, another dialog for call function should be implemented.
Here is my AlertDialog for Call Function:
final Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to Call?");
builder.setCancelable(false);
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");dial.setData(Uri.parse("tel:"+ Phone));
startActivity(dial);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(getApplicationContext(),"Activity will continue",Toast.LENGTH_LONG).show();
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
Please help me in two problems:
1.How to hyperlink the Phone number in the First Dialog?
2.how to embed the Call AlertDialog while clicking the Hyperlinked Phone Number?
Thanks in advance.
try this one:
Spannable spans = (Spannable) text;
ClickableSpan clickSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"+ url));
}
public void updateDrawState(TextPaint ds) {
//override link-centric text appearance
}
};
int index = (text.toString()).indexOf(url);
spans.setSpan(clickSpan, index, url.length() + index,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Categories

Resources