KoboldMan
KoboldMan

Reputation: 13

Change Button back to its original state after Time Delay

I think mine should be a fairly simple one:

Basically I want a Button I created to turn back to its original state after a short amount of time.

You press it, it changes though if you don´t press it again it returns back to its original state automatically.

How do I go about that?

I have looked alot extensively though I am New to this.

Here so far is what I got:

MainActivity.java:

    package button.tutorials.suvendu.com.mybuttontutorial;

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

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;


public class MainActivity extends AppCompatActivity {

    Timer timer;
    MyTimerTask myTimerTask;

    Button CoolButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        CoolButton=(Button)findViewById(R.id.btnchangedisp);
        CoolButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View vw) {
                CoolButton.setText(getString(R.string.FirstClick));

                timer = new Timer();
                myTimerTask = new MyTimerTask();

                timer.schedule(myTimerTask, 1000);


            }
        });
    }

    class MyTimerTask extends TimerTask {

        @Override
        public void run(){

So stuck at run method. I´m trying to use Timer and Timertask to achieve my goal. But I don´t know how to continue...

also here is my xml to which the code refers to:

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="button.tutorials.suvendu.com.mybuttontutorial.MainActivity"
    tools:showIn="@layout/activity_main">



    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:id="@+id/btnchangedisp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="128dp" />
</RelativeLayout>

Upvotes: 0

Views: 101

Answers (1)

George Mulligan
George Mulligan

Reputation: 11923

Using a timer in this case is not the best choice as there are simpler ways. Here is one alternative:

CoolButton.postDelayed(new Runnable() {
    @Override
    public void run() {
        // change CoolButton in some way.
    }
}, 1000);

Upvotes: 1

Related Questions