Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dialog does not appear

Tags:

android

dialog

I use following code:

public class Settings extends Activity implements OnClickListener {

    private Activity activity;
    private AlertDialog.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.settings);

        Button bAdd = (Button) findViewById(R.id.bAdd);
        bAdd.setOnClickListener(this);

        activity = this;
        builder = new AlertDialog.Builder(activity);
        builder.setMessage("message")
           .setTitle("title");

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.bAdd:
            AlertDialog dialog = builder.create();
            break;
        }

    }


}

But for some reason my popup doesn't appear and does nothing at all.. Any ideas on what is causing this malfunction? Thanks!

like image 820
Matthias Vanb Avatar asked Nov 13 '12 12:11

Matthias Vanb


2 Answers

You have to call show() method instead of create().

Note: create() method only creates instance of Dialog but it won't show its.

One suggestion:

You can create method that returns Dialog like this:

public Dialog createNewDialog(int type) {
   AlertDialog dlg = null;
   switch (type) {
      case SOME_CONSTANT:
         dlg = new AlertDialog.Builder(ActivityName.this / this)
            .setTitle("Title")
            .setMessage("Message")
            .setPositiveButton("Yes", null)
            .create();
      break;
   }
}

Then you can call it as:

createNewDialog(SOME_CONSTANT).show();

and your Dialog will be shown.

Especially in your case you can reach your goal with this snippet of code:

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.bAdd:
            createNewDialog(SOME_CONSTANT).show();
            break;
        }
    }

Hope it helps.

like image 175
Simon Dorociak Avatar answered Oct 25 '22 09:10

Simon Dorociak


I have this issue and maybe this answer can help someone.

I was running the code to show the AlertDialog on a non-ui thread. After using:

runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            ShowAlert();
        }
    });

the AlertDialog worked.

like image 40
RHaguiuda Avatar answered Oct 25 '22 08:10

RHaguiuda