Created
August 28, 2017 19:07
-
-
Save Binary-Finery/dcbe082e6cceef4f7c2b4dc656554857 to your computer and use it in GitHub Desktop.
Wafi, auto cycle iterate through colors at set interval using handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.view.View; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.Button; | |
import android.widget.LinearLayout; | |
public class MainActivity extends AppCompatActivity implements View.OnClickListener { | |
private final String[] COLORS = { | |
"#F44336", //red | |
"#E91E63", //pink | |
"#9C27B0", //purple | |
"#2196F3", //blue | |
"#4CAF50", //green - black | |
"#FFEB3B", //yellow - black | |
"#FF9800", //orange - black | |
"#795548", //brown | |
"#9E9E9E", //grey - black | |
}; | |
private Handler handler; | |
private Runnable runnable; | |
private LinearLayout rootLayout; | |
private Button btnStart; | |
private int counter = 0; | |
final int DURATION = 1500; | |
boolean inAutoMode = false; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
rootLayout = (LinearLayout)findViewById(R.id.root_layout); | |
btnStart = (Button)findViewById(R.id.button_start); | |
rootLayout.setOnClickListener(this); | |
btnStart.setOnClickListener(this); | |
handler = new Handler(); | |
runnable = new Runnable() { | |
@Override | |
public void run() { | |
rootLayout.setBackgroundColor(Color.parseColor(COLORS[counter])); | |
counter++; | |
if (counter == COLORS.length) counter = 0; | |
handler.postDelayed(this, DURATION); | |
} | |
}; | |
} | |
@Override | |
public void onClick(View v) { | |
if (v == btnStart){ | |
btnStart.setVisibility(View.GONE); | |
inAutoMode = true; | |
handler.post(runnable); | |
} | |
if ((v == rootLayout) && inAutoMode){ | |
handler.removeCallbacks(runnable); | |
counter = 0; | |
rootLayout.setBackgroundColor(Color.WHITE); | |
btnStart.setVisibility(View.VISIBLE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment