1

I used this function to disable my button initially but how to enable after a certain time period

button.setClickable(false);

Actually trying to create a button for resending otp so that initially it should be disabled but enable after the certain time period.

this is my otp.java class

package com.avinashjain.hp.cc;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;



import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;


public class Otp extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otp);


        final ProgressBar simpleProgressBar = (ProgressBar) 
        findViewById(R.id.simpleProgressBar);
        Button startButton = (Button) findViewById(R.id.button);
        // perform click event on button
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // visible the progress bar
                simpleProgressBar.setVisibility(View.VISIBLE);
            }
        });

    }

}

This is the activity_otp.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Otp"
    android:orientation="vertical"
    >


    <com.goodiebag.pinview.Pinview
        android:id="@+id/pinview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        app:cursorVisible="false"
        app:forceKeyboard="true"
        app:hint="0"
        app:inputType="text"
        app:layout_constraintEnd_toEndOf="parent"
        app:password="false"
        app:pinBackground="@drawable/example_drawable"
        app:pinHeight="35dp"
        app:pinLength="7"
        app:pinWidth="32dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteY="169dp" />

    <ProgressBar
        android:id="@+id/simpleProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="160dp"
        android:visibility="invisible" />

    <Button
        android:id="@+id/button"
        android:layout_width="135dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="@string/verify_otp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="148dp"
        tools:layout_editor_absoluteY="258dp" />

    <Button
        android:id="@+id/but_resend"
        android:layout_width="135dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="125dp"
        android:layout_marginTop="20dp"
        android:text="@string/resend_otp"

        />


</LinearLayout>

stacktrace is

mBtn1.setEnabled(false); // The button is initially disabled //
                    Timer buttonTimer = new Timer();
                    buttonTimer.schedule(new TimerTask() {

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

                                @Override
                                public void run() {
                                    mBtn1.setEnabled(true); // The button is enabled by the timer afte 5 seconds //
                                }
                            });
                        }
                    }, 5000); // Set your time period here //

I want that button to do so repetitively up to a certain number of times

Thanx in advance

Avinash jain
  • 486
  • 1
  • 7
  • 15
  • Possible duplicate of [Enabling button for a limited time in android app](https://stackoverflow.com/questions/43045979/enabling-button-for-a-limited-time-in-android-app) –  Jun 05 '18 at 17:40

1 Answers1

1

You can disable a button for a defined time period using the timer function. Here is an example:

public class Otp extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_otp);

    final ProgressBar simpleProgressBar = (ProgressBar) 
    findViewById(R.id.simpleProgressBar);
    Button startButton = (Button) findViewById(R.id.button);
    // perform click event on button
    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // visible the progress bar
            simpleProgressBar.setVisibility(View.VISIBLE);
        }
    });

// Your Resend OTP Button Code is below //

Button ResendButton = (Button) findViewById(R.id.but_resend);
ResendButton.setEnabled(false); // The button is initially disabled //
                        Timer buttonTimer = new Timer();
                        buttonTimer.schedule(new TimerTask() {

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

                                    @Override
                                    public void run() {
                                        ResendButton.setEnabled(true); // The button is enabled by the timer after 5 seconds //
                                    }
                                });
                            }
                        }, 5000); // Set your time period here //
}}
Abhi
  • 3,431
  • 1
  • 17
  • 35
  • Thank you, Abhi for helping out please tell me that how can I display that time remaining before the button is enabled? – Avinash jain Jun 05 '18 at 17:39
  • To display a countdown time, you can use the [CountDownTimer](http://developer.android.com/reference/android/os/CountDownTimer.html) function. – Abhi Jun 05 '18 at 17:42
  • Thank you Abhi for your help ;) – Avinash jain Jun 05 '18 at 17:47
  • Add the complete Activity as an edit to your question. Also add the stacktrace. – Abhi Jun 06 '18 at 09:04
  • Check the edited answer. Just a hint, stacktrace is the error log you receive when an app crashes. You can analyze the stacktrace to find the error source. – Abhi Jun 06 '18 at 10:59