Question

I am wanting to pass the data added into these fields to a .txt file stored on the device.

Was it helpful?

Solution

Try to do something as I show below:

Button btn11 = (Button) this.findViewById(R.id.buttonformdata);
btn11.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        try{
              FileOutputStream fos = openFileOutput("yourFile", Context.MODE_PRIVATE);

              String string1 = editText1.getText().toString();
              String string2 = editText2.getText().toString();
              String string3 = editText3.getText().toString();

              fos.write(string1.getBytes());
              fos.write(string2.getBytes());
              fos.write(string3.getBytes());
              fos.close();

         }catch(Exception e){
              Log.e("Exception", e.toString());
         }
    }
});

Let me know if it works!

OTHER TIPS

In your xml file you can add android:onClick="bSomething" to the properties of the button you want to click. Then on your activity class (or where you have your code that you posted) you can do something like:

public void bSomething(View view){
    try{

         FileOutputStream fout = openFileOutput(“yourfile.txt”,MODE_PRIVATE);

         OutputStreamWriter osw = new OutputStreamWriter(fOut);

         osw.write(editText1.getText().toString()+" ");
         osw.write(editText2.getText().toString()+" ");
         osw.write(editText3.getText().toString()+" ");

         osw.close();

         fout.close();

         }catch(Exception e){

             //do the exception handling
         }
     }

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top