Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delay put on button click android

Tags:

android

public class MainActivity extends Activity implements NetworkMonitorListener {
    double _mylat = 0;
    double _mylong = 0;
    TextView textView1;

    Button clcikbutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1 = (TextView) findViewById(R.id.textView1);

        clcikbutton = (Button) findViewById(R.id.button1);

        clcikbutton.setEnabled(false);

        Timer buttonTimer = new Timer();
        buttonTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        clcikbutton.setEnabled(true);
                    }
                });
            }
        }, 5000));

        clcikbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                        "MM/dd/yyyy hh:mm:ss aa");
                simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
                textView1.setText(DateFormat.getDateTimeInstance().format(
                        new java.util.Date("11/7/2014 5:19:11 AM UTC")));

            }
        });

    }
}

this is my code for delay on button click.I am trying to implement that when i click on the button and after that it should disable for 5 seconds and then it should work.Please help me where i am doing wrong because there is Error coming .

like image 312
user2794306 Avatar asked Nov 19 '14 04:11

user2794306


People also ask

How to Add delay for button click in android?

final Runnable enableButton = new Runnable() { @Override public void run() { clcikbutton. setEnabled(true); } }; new Handler(). postDelayed(enableButton, 5000); } }); Those code should get you what you want: It will make the button clickable at first, then disable it for 5 seconds the time you click it.

How does onClick work on Android?

When the user clicks a button, the Button object receives an on-click event. To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.

How to set delay in Android?

How to set delay in android? How to set delay in android? In some cases, after some time we need to update on UI to solve this problem, In this example demonstrate how to set a delay in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

How do you handle click events on a button in Android?

Handling Click events in Button | Android. When the user clicks a button, the Button object receives an on-click event. To make click event work add android:onClick attribute to the Button element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.

How to update initial text after some delay in Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view, primary it shows "initial text" after some delay it will update with new text.

How to prevent 300ms click delay on mobile devices?

5 Ways to Prevent the 300ms Click Delay on Mobile Devices 1. Don’t Fret About it 2. Disable Zooming (Chrome and Firefox) 3. Set the viewport to the device-width (Chrome 32+) 4. Use PointerEvents (IE10+) 5. Implement touchend Event Handlers


1 Answers

Try to use Handler to disable button for given time :

clcikbutton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
       simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
       textView1.setText(DateFormat.getDateTimeInstance().format(new java.util.Date("11/7/2014 5:19:11 AM UTC")));
       clcikbutton.setEnabled(false);
       new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                clcikbutton.setEnabled(true);        
            }
        },5000);
    }
});
like image 136
Haresh Chhelana Avatar answered Oct 05 '22 14:10

Haresh Chhelana