0

I am new in java and I am trying to find a simple way to put a dialog after if statement but I am stack. I tried this code but i don't want to create a new class for one dialog.

So I am trying to get a Dialog when I get close to a point, I have a toast as well and I am getting the toast correct.

I didn't include the full code but I can do so if is necessary.

Thx

if(i==3){//this is home
        TextView tv4=(TextView)parentActivity.findViewById(R.id.textView4);
        tv4.setText("Distance From home is: "+ String.valueOf(distance));
        if (distance < 1000) {
              
       
AlertDialog.Builder builder1 = new  AlertDialog.Builder(parentActivity.getBaseContext());                  
                   
                     builder1.setTitle("Alert Dialog");
                        builder1.setMessage("Write your message here.");
                     
                          
            Toast.makeText(parentActivity.getBaseContext(), 
                       "Welcome Home", Toast.LENGTH_LONG).show();
            
        }
       }
Community
  • 1
  • 1
George Christou
  • 107
  • 2
  • 11
  • You only need to call `builder1.show();` after setting the title and message to display the dialog. – AndroidEx Apr 16 '15 at 02:52
  • Just add this two lines...to show the dialog... AlertDialog alertDialog = builder1.create(); alertDialog.show(); – Harsha Vardhan Apr 16 '15 at 02:56
  • It seems better now but the app dose not work, before it was running showing the toast without the dialog. Should I change something on the layout? – George Christou Apr 16 '15 at 03:08

2 Answers2

1

You need to show the alert dialog that you built using alert builder.

AlertDialog alert = builder1.create();
alert.show();
kalan nawarathne
  • 1,944
  • 27
  • 26
1

you're missing two methods first of all you didn't call the create method which actually creates the dialog and then you have to call the show method to make the dialog visible

add this

// create alert dialog
    AlertDialog alertDialog = builder1.create();

// show the dialog
    alertDialog.show();
danidee
  • 9,298
  • 2
  • 35
  • 55
  • It seems better now but the app dose not work, before it was running showing the toast without the dialog. Should I change something on the layout? – George Christou Apr 16 '15 at 03:08
  • Try only passing the 'parentActivity' not context in AlertDialog.Builder builder1 = new AlertDialog.Builder(parentActivity.getBaseContext()); – kalan nawarathne Apr 16 '15 at 03:11
  • what exactly do you mean by it does not work? is it crashing or it dosen't show the toast again – danidee Apr 16 '15 at 03:11