I made a button, it calls refresh method:
... android:onClick="refresh"
In the begining of the method I set these:
btn.setEnabled(false);
btn.setClickable(false);
And in the end I set enable and clickable true. My problem is that, after the first click while program is working and button is disabled I click on the button for example 3 times. When transaction is over it automatically starts again 3 times. So setEnabled(false) is not working. Can anybody tells me why? Thx.
So there is only one button in my layout:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center_horizontal"
android:onClick="refresh"
android:text="Refresh"
android:textStyle="bold"
android:background="@drawable/button"
/>
Here is the method:
public void refresh(View view){
btn.setEnabled(false);
btn.setClickable(false);
final double currentLat = lat;
final double currentLng = lng;
if(isOnline == true) {
String link = "http://maps.googleapis.com/maps/api/geocode/json?latlng="+currentLat+","+currentLng+"&sensor=true&language=hu";
final GetLocation si = (GetLocation) new GetLocation(this, link).execute();
Runnable runnable = new Runnable() {
public void run() {
handler.post(new Runnable() {
public void run() {
Thread.currentThread();
try {
Thread.sleep(3000);
if(si.getAddress() != null) {
address.setText(si.getAddress());
}
String date = DateFormat.getDateTimeInstance().format(new Date());
String data = date+": ["+currentLat+", "+currentLng+"] - "+si.getAddress()+"\n";
FileManagement f = new FileManagement(filename);
f.writeToFile(data);
btn.setEnabled(true);
btn.setClickable(true);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
});
}
};
new Thread(runnable).start();
}
}