1

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();           
    }
}

3 Answers3

0

My suggestion is to use onClickListener in your code instead of declaring

     android:onClick="refresh"

If you declare onClickListener you'll be able to log when the onClick event is intercepted;)

axl coder
  • 739
  • 3
  • 19
  • 1
    why you can Log by using onClickListener and you cant using onClick xml tag !!? – JafarKhQ Jul 17 '14 at 13:00
  • Inserting log in onClick method will give him clear evidence of when onClick event is intercepted. I thought at this because it seems that his methods are in a queue, so it's not clear when they are started. – axl coder Jul 17 '14 at 13:06
  • You will get the same result by Logging the method 'refresh' – JafarKhQ Jul 17 '14 at 13:39
0

Probably in your application long task in main thread block the UI and click event come after UI unlocked.

After click button you should:

  1. Set button disable
  2. Run operation in other thread - using Thread class or AsyncTask
  3. After operation complete set button as enable
Matt Twig
  • 444
  • 3
  • 8
0

Define an AtomicBoolean variable somewhere in your class.

private AtomicBoolean tokenTaken = new AtomicBoolean();

In your button click listener set the atomic variable to true.

public void refresh(View view){
    if(!tokenTaken.compareAndSet(false,true))
        return;
    ...//Rest of your code.
    //set the token false when you want to enable the button.
    tokenTaken.set(false);
}

Reference: https://developer.android.com/reference/java/util/concurrent/atomic/AtomicBoolean

ABN
  • 1,024
  • 13
  • 26