Furkan KELES
Furkan KELES

Reputation: 139

Why i dont show positiveButton

I made an AlertDialog with a "positive button" but in emulator it doesn't show up. What's the problem?

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnDersEkle = (Button) findViewById(R.id.btnDersEkle);
        etDers = new EditText(MainActivity.this);
        LayoutDers = (LinearLayout) findViewById(R.id.layoutDers);
        AlertDialog.Builder alertDers = new AlertDialog.Builder(MainActivity.this);
        alertDers.setTitle("Ders Adi Giriniz");
        alertDers.setView(etDers);
        final AlertDialog alert = alertDers.create();

        alertDers.setPositiveButton("TAMAM", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                tvDers = new TextView(MainActivity.this);
                tvDers.setText(etDers.getText().toString());
                LayoutDers.addView(tvDers);

            }
        });


        btnDersEkle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.show();
            }
        });


    }
}

Upvotes: 1

Views: 51

Answers (3)

Alejandro Lora
Alejandro Lora

Reputation: 7802

Because you have to set the negative button too.

alertDers.setNegativeButton("Button2", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
    //DO TASK
}

and if you don't want that this button is shown, you can do this:

alertDers.getButton(yourButton).setEnabled(false);

Upvotes: 0

shkschneider
shkschneider

Reputation: 18243

You cannot modify such things once the AlertDialog got created.

Use setPositiveButton() and other methods before create():

final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
.setPositiveButton(/* ... */)
.create();

Upvotes: 1

Mattia Maestrini
Mattia Maestrini

Reputation: 32780

You need to setPositiveButton before create the AlertDialog. Try this:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnDersEkle = (Button) findViewById(R.id.btnDersEkle);
        etDers = new EditText(MainActivity.this);
        LayoutDers = (LinearLayout) findViewById(R.id.layoutDers);
        AlertDialog.Builder alertDers = new AlertDialog.Builder(MainActivity.this);
        alertDers.setTitle("Ders Adi Giriniz");
        alertDers.setView(etDers);
        alertDers.setPositiveButton("TAMAM", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                tvDers = new TextView(MainActivity.this);
                tvDers.setText(etDers.getText().toString());
                LayoutDers.addView(tvDers);

            }
        });
        final AlertDialog alert = alertDers.create();

        btnDersEkle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.show();
            }
        });
    }
}

Upvotes: 1

Related Questions