Question: I am struck in a part of the code where an alert box is displayed asking the user to enter the name of the dataset. A file of the same name will be created in the internal storage. The alert dialog is working fine but the file created in the sd card is always "`null.txt`". After hours of searching, I have tried all the solutions such as creating a custom layout for the alert box, adding a `TextWatcher` to the `edittext` etc, but of no help. When I manually assign the name of the dataset to the variable 'Set', the file is successfully created. Thus there is a problem with the `alertdialog`. I am using android studio 2.3.1. Kindly help.

I am also posting the code.

```
else if (map2.contains("1") || map2.contains("4")) {
    final EditText edittext = new EditText(Scan.this);
    AlertDialog.Builder alert1 = new AlertDialog.Builder(Scan.this);
    alert1.setMessage("Name of the dataset");
    alert1.setTitle("Enter Your Title");
    alert1.setView(edittext);
    alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String set = edittext.getText().toString();
            Set = set;
        }
    });
    alert1.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int whichButton) {
        }
    });
    alert1.show();
}
myFile = new File(Environment.getExternalStorageDirectory(),Set+".txt");
if (!myFile.exists()) {
    try {
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write(message1);
        osw.flush();
        osw.close();
        fOut.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
   finally {
   }
}

```

Answer: 
you are creating file before set title in dialog.
try this:

```
else if (map2.contains("1") || map2.contains("4")) {
    final EditText edittext = new EditText(Scan.this);
    AlertDialog.Builder alert1 = new AlertDialog.Builder(Scan.this);
    alert1.setMessage("Name of the dataset");
    alert1.setTitle("Enter Your Title");
    alert1.setView(edittext);
    alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String set = edittext.getText().toString();
            createFile(set);
        }
    });
    alert1.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int whichButton) {
        }
    });
    alert1.show();
}
}
private void createFile(String set){
   myFile = new 
 File(Environment.getExternalStorageDirectory(),set+".txt");
   if (!myFile.exists()) {
       try {
           myFile.createNewFile();
           FileOutputStream fOut = new FileOutputStream(myFile);
           OutputStreamWriter osw = new OutputStreamWriter(fOut);
           osw.write(message1);
           osw.flush();
           osw.close();
           fOut.close();
       }
       catch (Exception e) {
           e.printStackTrace();
       }
       finally {
       }
   }

```

}
Try this code:

inflate your custom layout containing the `Edittext` and use it like:

```
 LayoutInflater inflater = LayoutInflater.from(Scan.this);            
final View view = inflater.inflate(R.layout.custom_layout, null);
AlertDialog.Builder alert = new AlertDialog.Builder(Scan.this); 

alert.setTitle("Tilte"); 
alert.setMessage("Name of the dataset"); 
alert.setView(view); 

final EditText et1 = (EditText) view.findViewById(R.id.editText1);

alert.setPositiveButton("ok", new DialogInterface.OnClickListener() { 
   public void onClick(DialogInterface dialog, int whichButton) 
   { 
          String s1=et1.getText().toString();
          //do operations using s1...
          myFile = new File(Environment.getExternalStorageDirectory(),s1+".txt");
            if (!myFile.exists()) {
                try {
                    myFile.createNewFile();
                    FileOutputStream fOut = new FileOutputStream(myFile);
                    OutputStreamWriter osw = new OutputStreamWriter(fOut);
                    osw.write(message1);
                    osw.flush();
                    osw.close();
                    fOut.close();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
               finally {
               }
        } 
}); 

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
   public void onClick(DialogInterface dialog, int whichButton) { 
            dialog.cancel(); 
   } 
}); 

alert.show(); 

```

From @Deepti Maduskar

Finally I could solve the issue. The problem lied in the definition of the global vaiable Set. i just needed to declare it instead of defining it as `final`.