-1

I'm having a problem to change the button color after a certain amount of time. I know how to change using handle after a fixed time, but I need to change the color after a specific time that is chosen by the user.

public class MainActivity extends Activity {

EditText tempo;
Button bt;
int estado = 1;


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

    tempo = (EditText) findViewById(R.id.tempo);
    long delay = Long.parseLong(tempo.getText().toString());


     bt = (Button) findViewById(R.id.btvibrar);

     bt.setOnClickListener(new View.OnClickListener() {


        public void onClick(View arg0) {


        if(!tempo.getText().toString().equals("")){

            if(estado==1){

                  Vibrar();
                  estado*=-1;

                 bt.setText("Parar !");
                 bt.setBackgroundColor(Color.RED);

                    //Handler handler = new Handler();
                    //handler.postDelayed(new Runnable() {

                        //@Override
                        //public void run() {
                            //estado*=-1;
                            //bt.setText("Vibrar !");
                             //bt.setBackgroundColor(Color.GREEN);
                        //}
                //  },  );
                            } else {
                                Parar();
                                estado*=-1;
                                bt.setText("Vibrar !");
                                 bt.setBackgroundColor(Color.GREEN);

                                    }
                                        } else  {
                                            AlertDialog.Builder dialogo = new AlertDialog.Builder(MainActivity.this);
                                            dialogo.setTitle("Erro !");
                                            dialogo.setMessage("Escolha um tempo.");
                                            dialogo.setNeutralButton("OK", null);
                                            dialogo.show();

                                        }
        }



        private void Vibrar(){    // É necessario lançar excessao no ANDROIDMANIFEST.XML
            Vibrator rr = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            long treal = Long.parseLong(tempo.getText().toString());
            long milliseconds = treal*1000;  
            rr.vibrate(milliseconds);
        }

        private void Parar(){
            Vibrator rr = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            rr.cancel();
        }

    });
    }

}

kapex
  • 28,903
  • 6
  • 107
  • 121
  • I posted it now, but i think that i made some mistake and my post was closed. It is a app to vibrate the phone. – Rcgoncalves Apr 19 '13 at 16:55
  • You are still unclear on what is the problem. – madth3 Apr 19 '13 at 17:17
  • Sorry, it is my first time here, this app can vibrate the phone, the user choose the time. When the time ends, i want to make my button green again ( automatically ) , so i need to wait a certain amount of time ( the time choosen by the user, to turn the button green again ) – Rcgoncalves Apr 19 '13 at 17:32

1 Answers1

0

This code switches the color of your button to red:

bt.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        new AsyncTask<Long, Void, Void>() {
            @Override
            protected Void doInBackground(Long... delay) {
                if (delay.length > 0) {
                    try {
                        Thread.sleep(delay[0]);

                    } catch (InterruptedException e) {
                    }
                }

                return null;
            }

            @Override
            protected void onProgressUpdate(Void... none) {
            }

            @Override
            protected void onPostExecute(Void none) {
                if (ContactManager.this != null && !ContactManager.this.isFinishing()) {
                    bt.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
                    bt.invalidate();
                }
            }
        }.execute(new Long(delay));
    }
});

It uses the delay variable for the delay. Hope this helps ...

Cheers!

Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • Didn't work. i think that i might be doing something wrong. In my code, where i have to put yours ? – Rcgoncalves Apr 23 '13 at 13:52
  • Mmmmhhh... I tested it in a sample app and it worked quite well. You've to set the listener in `onCreate()` for the button you want to change (here it's named `bt`). So add a `Button bt = (Button)findViewbyId(...` before the code above. Do you get any exceptions? If yes: which one? – Trinimon Apr 23 '13 at 14:06
  • I think that the only exception is when the user does't fill the EditText.. and maybe when the user closes the app i want to stop the vibrate. how can i talk to you directly and explain ?? Sorry about everything, i just started on this, and realy thank you. can you send me your e-mail ? – Rcgoncalves Apr 24 '13 at 01:59
  • Send me a e-mail if you fell confortable : Rcunhagoncalves@gmail.com – Rcgoncalves Apr 24 '13 at 22:11
  • I just saw, it was on span. – Rcgoncalves May 07 '13 at 21:29