1

I want to add a Yes/No dialog box to my Android app, I tried the solution in this answer but I couldn't manage to make it work with my code, any help please?

This is my code in which I want to write a text from EditText.

    public void buttonSelect( View v ) {
    View view = null;
    String mac1 = "mac1";
    String mac2 = "mac2";

    TextView tv1, tv2;
    tv1 = (TextView) findViewById(R.id.textView1);
    tv2 = (TextView) findViewById(R.id.textView2);

    switch (v.getId()) {
    case (R.id.Write_MAC1):
        writeData(view, mac1); //I need to confirm writing the data
    break;

    case (R.id.Write_MAC2):
        writeData(view, mac2); //I need to confirm writing the data
    break;

    }
}

 //---------------------------- Writing MACs addresses Function --------------------------------------------   

public void writeData(View view, String macNum)
{
    BufferedWriter bufferWriter =null;
    try {
        FileOutputStream fileOutputStream = openFileOutput(macNum, Context.MODE_PRIVATE);
        bufferWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));

        if (macNum.equals("mac1")){         
           bufferWriter.write(((EditText)this.findViewById(R.id.editText1)).getText().toString());}

        if (macNum.equals("mac2")){         
               bufferWriter.write(((EditText)this.findViewById(R.id.editText2)).getText().toString());}                 

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally
    {
        try {
            bufferWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Majid ff
  • 249
  • 7
  • 22
  • where is AlertDialog related code which you have tried ? – ρяσѕρєя K Feb 01 '15 at 13:02
  • don't use alert dialog. use dialog and create custom one from xml and can add anything as per your requirement. between there is no link between your title and the code which you posted. – Shadow Feb 01 '15 at 13:03
  • There is a pretty extensive [guide on Dialogs on d.android.com](http://developer.android.com/guide/topics/ui/dialogs.html), including an example (with full code) that literally answers your question. – MH. Feb 01 '15 at 13:44

1 Answers1

0

Try to use this:

AlertDialog.Builder alertbox = new AlertDialog.Builder(LauncherActivity.this);
alertbox.setTitle("Are you sure?");
alertbox.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(LauncherActivity.this, "You Choose Yes!!", Toast.LENGTH_LONG).show();
}
});
alertbox.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(LauncherActivity.this, "You Choose Nooo!!", Toast.LENGTH_LONG).show();
}
});
alertbox.show();

See this git repo

Also a custom alertdialog example is exist in this repo.

  • Md. Shahadat Sarker, could you please show me where to add your code to my one, I tried to insert your solution after case (R.id.Write_MAC1): but I got "No enclosing instance of the type LauncherActivity is accessible in scope"! – Majid ff Feb 01 '15 at 13:26
  • LauncherActivity is the name of my App's activity in where I implement my code. By the way, you can write this code into your case statement. And please see my github repo. https://github.com/iamsarker/android-apps/tree/master/CustomAlertBox – Md. Shahadat Sarker Feb 01 '15 at 13:32
  • And replace the Toast with your code writeData(view, mac1); And one more thing replace the launcher activity with your activity name. – Md. Shahadat Sarker Feb 01 '15 at 13:33