Reputation: 389
I made a refresh item in action bar. When u click it a dialog box will show asking to check the internet connection. The problem is there's no dialog box showing up at all. I wanna know if there's something wrong with my code.
private void Refresh() {
if(IsparkLib.isInternetConnected(InquiryMainActivity.this)){
nameOfMethod();
AlertDialog.Builder dialog = new AlertDialog.Builder(InquiryMainActivity.this);
dialog.setTitle("Are you sure you want to update?");
dialog.setCancelable(true);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
// TODO Auto-generated method stub
Intent intent = new Intent(InquiryMainActivity.this,InquiryMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
});
}
else{
AlertDialog.Builder dialog = new AlertDialog.Builder(InquiryMainActivity.this);
dialog.setTitle("Warning");
dialog.setPositiveButton("OK",null);
final TextView mes = new TextView(InquiryMainActivity.this);
mes.setTextColor(Color.BLACK);
mes.setText("Please check your internet connection");
mes.setTextSize(20);
}
Upvotes: 0
Views: 1005
Reputation: 4522
edit your code as
private void Refresh() {
if(IsparkLib.isInternetConnected(InquiryMainActivity.this)){
nameOfMethod();
AlertDialog.Builder dialog = new AlertDialog.Builder(InquiryMainActivity.this);
dialog.setTitle("Are you sure you want to update?");
dialog.setCancelable(true);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
// TODO Auto-generated method stub
Intent intent = new Intent(InquiryMainActivity.this,InquiryMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
});
dialog.show();
}
else{
AlertDialog.Builder dialog = new AlertDialog.Builder(InquiryMainActivity.this);
dialog.setTitle("Warning");
dialog.setPositiveButton("OK",null);
/*final TextView mes = new TextView(InquiryMainActivity.this);
mes.setTextColor(Color.BLACK);
mes.setText("Please check your internet connection");
mes.setTextSize(20);*/ It's not necessary
dialog.setMessage("Please check your internet connection");//
dialog.show();
}
Upvotes: 1
Reputation: 9700
You forgot to create the AlertDialog
instance from AlertDialog.Builder
object and to show that dialog.
Another thing, you have tried to create the AlertDialog.Builder
object in both if-else
condition in Refresh()
method but you should create outside the if-else
condition as below...
private void Refresh() {
AlertDialog.Builder dialog = new AlertDialog.Builder(InquiryMainActivity.this);
if(IsparkLib.isInternetConnected(InquiryMainActivity.this)){
nameOfMethod();
dialog.setTitle("Are you sure you want to update?");
dialog.setCancelable(true);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
// TODO Auto-generated method stub
Intent intent = new Intent(InquiryMainActivity.this,InquiryMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
});
} else {
dialog.setTitle("Warning");
dialog.setPositiveButton("OK",null);
final TextView mes = new TextView(InquiryMainActivity.this);
mes.setTextColor(Color.BLACK);
mes.setText("Please check your internet connection");
mes.setTextSize(20);
}
AlertDialog alertDialog = dialog.createDialog();
alertDialog.show();
}
Update:
For Cancel option, you have to add another button to the dialog as below...
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//add your code
dialog.dismiss();
}
});
Upvotes: 2
Reputation: 2097
Add this below two line after ifelse
condition..
AlertDialog alertDialog = dialog.createDialog();
alertDialog.show();
And you complete with your code....
Upvotes: 2
Reputation: 132992
You will need to call AlertDialog.show()
to show alert on screen do it as:
AlertDialog alertDialog = dialog.create();
// show it
alertDialog.show();
Upvotes: 3
Reputation: 1958
I think you are not calling dialog.show();
. Check once.
In else block try like this...
else{
AlertDialog.Builder dialog = new AlertDialog.Builder(InquiryMainActivity.this);
dialog.setTitle("Warning");
dialog.setPositiveButton("OK",null);
final TextView mes = new TextView(InquiryMainActivity.this);
mes.setTextColor(Color.BLACK);
mes.setText("Please check your internet connection");
mes.setTextSize(20);
dialog.show();//=> here is the change
}
Upvotes: 2