java - Dynamic Button with time Itervals -


as follow-up first question , has been kindly answered george mulligan,

i´m trying create button needs pressed 3 times consecutively avtivated. between each hit countdown on button shows remaining number of hits become active [hence after 1st hit, button show 2, after 2nd show 1 , after 3rd lights green , says active]

though there should timer runnning in background or sort of delay initiates after first hit , reinitiates after each except third when button has become active.

when maximum delay reached before next hit, button shall turn original state , 1 have start over, i.e perform 3 consecutive hits within time button become active.

how go that?

so far, i´ve made button change 1 hit , setup .postdelayed() method.

though should overridden , reset following hit , disactivated on 3rd hit.

who can tell me how achieve that?

is there unless statement allow me that?

here´s code:

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));                  coolbutton.postdelayed(new runnable() {                     @override                     public void run() {                         coolbutton.settext(getstring(r.string.button));                     }                 }, 1000);   //this how far got. rest standard code.                   timer = new timer();                 mytimertask = new mytimertask();                }         });     }      class mytimertask extends timertask {          @override         public void run(){            }         }     @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     }     } 

and xml file 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> 

you should use counter count hit , check condition of hit , use postdelayed method.

     int count = 0; coolbutton.setonclicklistener(new view.onclicklistener() {    @override             public void onclick(view v) {                   count = count + 1;// initialise int variable global int count = 0;                  if (count == 3) {                     count = 0;// reset counter when got activate.                     toast.maketext(mainactivity.this, "your button activated", toast.length_short).show();                  // write code here of activate button color change or methods , whatever want .                    } else if (count == 2) {                     toast.maketext(mainactivity.this, "your click 1 more time activate button", toast.length_short).show();                 }else if (count == 1) {                     toast.maketext(mainactivity.this, "your click 2 more time activate button", toast.length_short).show();                 }                   new handler().postdelayed(new runnable() {                      @override                     public void run() {                         count = 0;                     }                 }, 5000);// delay time , in 5 second have click 3 time otherwise again reset counter zero.                 // can set time interval restart counter.             }          }); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -