Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set a limit of clicking a button when user already click it for 10 times-android
    primarykey
    data
    text
    <p>I have a button called "Save". When I click it, it will call below coding :</p> <pre><code>public void SaveText(View view){ try { OutputStreamWriter out = new OutputStreamWriter(openFileOutput ("myfilename.txt",MODE_APPEND)); double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()); String latt = Double.toString(latitude); String lonn = Double.toString(longitude); String text =(latt+" "+lonn+" "+mydate); out.write(text); out.write('\n'); out.close(); Toast.makeText(this,"Text Saved !",Toast.LENGTH_LONG).show(); } catch (java.io.IOException e) { //do something if an IOException occurs. Toast.makeText(this,"Sorry Text could't be added",Toast.LENGTH_LONG).show (); } </code></pre> <p>I want to set the "Save" button unclickable when user already click it for 10 times so that it will not save more than 10 texts to text file.</p> <p><strong>EDIT</strong></p> <p>What I have tried so far :</p> <p>do{</p> <pre><code> try { OutputStreamWriter out = new OutputStreamWriter(openFileOutput ("myfilename.txt",MODE_APPEND)); double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()); String latt = Double.toString(latitude); String lonn = Double.toString(longitude); String text =(latt+" "+lonn+" "+mydate); out.write(text); out.write('\n'); out.close(); Toast.makeText(this,"Text Saved !",Toast.LENGTH_LONG).show(); count++; } catch (java.io.IOException e) { Toast.makeText(this,"Sorry Text could't be added",Toast.LENGTH_LONG).show (); }} while(count&lt;9); if (count&gt;9){Button btn=(Button)findViewById(R.id.Save); btn.setEnabled(false);} } </code></pre> <p>The result is when click "Save" button once, it disable my "Save" button without waiting it to be clicked until 10 times.</p> <p><strong>NEW CODE</strong></p> <pre><code> public void SaveText(View view){ if (this.counter &lt;= 10) { try { OutputStreamWriter out = new OutputStreamWriter(openFileOutput ("xy.txt",MODE_APPEND)); double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()); String latt = Double.toString(latitude); String lonn = Double.toString(longitude); String text =(latt+" "+lonn+" "+mydate); out.write(text); out.write('\n'); out.close(); Toast.makeText(this,"Text Saved !",Toast.LENGTH_LONG).show(); } catch (java.io.IOException e) { Toast.makeText(this,"Sorry Text could't be added",Toast.LENGTH_LONG).show (); } this.counter++; } }} </code></pre> <p>All method I have tried but when I click "Save" button for the second time, it will do nothing.</p> <p><strong>SOLUTION :</strong></p> <p>Thank you very much. Everyone really help me to solve this.</p> <pre><code> public void SaveText(View view){ if (this.counter &lt;= 10) { //to-do coding } this.counter++; } else{Button btn=(Button)findViewById(R.id.Save); btn.setEnabled(false);} }} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload