1

I am trying to show a customizable dialog. This dialog contains 3 edittext and one timepicker. I want to show this dialog when I press a button in the screen. I looked at google tutorial and try to write the code. However, when I used the root layout in the inflater as the layout which is being pressed button in it, it adds the dialog under the button. When I used the root layout in the dialog_xml the button does not work. The part of that code is under below. Do you have any idea How can i do this in right way?

    Button ekleButton;
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.haphatirlatici);

       // After creating the activity setting other things for running
       ekleButton = (Button) findViewById(R.id.EkleButton);
       ekleButton.setOnClickListener(new View.OnClickListener() {   
       public void onClick(View v) {
            // TODO Auto-generated method stub
            // Dialog icin yerlesimler

        AlertDialog.Builder builder;
        AlertDialog alertDialog;

        Context mContext = getApplicationContext();
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.ekle_dialog,
                                       (ViewGroup) findViewById(R.id.Ekle_Layout));

        builder = new AlertDialog.Builder(mContext);
        builder.setView(layout);
        alertDialog = builder.create();

        }
    });

After this one I edit the code and add two buttons. I want to get the result of the alert dialog to the activity that is seen. The code that I write is on below.

public class HapHatirlatici extends Activity{

Button ekleButton;
boolean eklendiMi;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.haphatirlatici);

    // After creating the activity setting other things for running
    ekleButton = (Button) findViewById(R.id.EkleButton);
    eklendiMi = false;
    ekleButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub

        View layout = getLayoutInflater().inflate(R.layout.ekle_dialog, null);                               
        AlertDialog.Builder builder = new AlertDialog.Builder(HapHatirlatici.this);
        builder.setPositiveButton(R.string.ekle,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                eklendiMi = true;
           }
        });
        builder.setNegativeButton(R.string.vazgec, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                eklendiMi = false;
           }
        });
        builder.setView(layout);
        AlertDialog alertDialog = builder.create();
        alertDialog.setTitle("Ilac Ekleme");
        alertDialog.show();


        }
    });
}
public boolean databaseEkle()
{
    boolean sonuc = false;
    return sonuc;
}

 }
Tekin alp
  • 97
  • 1
  • 4
  • 9

1 Answers1

1

You forgot the alertDialog.show(); to actually display the dialog you just built.

Also, your code can be simplified:

Button ekleButton;
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.haphatirlatici);

   // After creating the activity setting other things for running
   ekleButton = (Button) findViewById(R.id.EkleButton);
   ekleButton.setOnClickListener(new View.OnClickListener() {   
       public void onClick(View v) { 
           View layout = getLayoutInflater().inflate(R.layout.ekle_dialog, null);                               
           AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityClass.this);
           builder.setView(layout);
           AlertDialog alertDialog = builder.create();

           // this is what you forgot:
           alertDialog.show();
       }
    });
}

Of course, replace YourActivityClass with your actual activity name.

Guillaume
  • 22,694
  • 14
  • 56
  • 70