java


Java: Change background color with a delay using hex values in an array


I need to change the color of a blank ImageView using the hex color code values stored in the String array transmitArray, with the delay specified in TransmitFreq. However when I run the code, only the first color (corresponding to the first array value) is shown.
I tried three methods, namely (thread.sleep), countdown timer and post.delayed but with no success. I would appreciate if someone could point out what I'm doing wrong.
public class Main2Activity extends AppCompatActivity {
String [] transmitArray;
long transmitFreq;
public static int i;
public static View colourView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main2);
final String transmitArray [] = getIntent().getStringArrayExtra("COLOUR_DATA");
transmitFreq = getIntent().getLongExtra("FREQ_VALUE", 0);
int arrayLength = transmitArray.length;
colourView = findViewById(R.id.colourBox);
/*
//Method 1: Using Countdown timer
new CountDownTimer(transmitFreq*(transmitArray.length), transmitFreq) {
public void onTick(long millisUntilFinished) {
colourView.setBackgroundColor(Color.parseColor(transmitArray[i]));
i++;
}
public void onFinish() {
i=0;
}
}.start();
//Method 2: Using post.delayed
Handler handler = new Handler();
for (i = 0; i < arrayLength ; i++) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
String transmitColour = transmitArray[i];
colourView.setBackgroundColor(Color.parseColor(transmitColour));
}
}, transmitFreq);
}*/
//Method 3: Using thread.sleep
for (i = 0; i < arrayLength ; i++) {
String transmitColour = transmitArray[i];
colourView.setBackgroundColor(Color.parseColor(transmitColour));
try {
Thread.sleep(transmitFreq);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
You can initially use a default color inside the onCreate Method and then try the 3 methods outside the OnCreate Method. Try this code
public class Main2Activity extends AppCompatActivity {
String [] transmitArray;
long transmitFreq;
public static int i;
public static View colourView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main2);
colourView = findViewById(R.id.colourBox);
}
final String transmitArray [] = getIntent().getStringArrayExtra("COLOUR_DATA");
transmitFreq = getIntent().getLongExtra("FREQ_VALUE", 0);
int arrayLength = transmitArray.length;
/*
//Method 1: Using Countdown timer
new CountDownTimer(transmitFreq*(transmitArray.length), transmitFreq) {
public void onTick(long millisUntilFinished) {
colourView.setBackgroundColor(Color.parseColor(transmitArray[i]));
i++;
}
public void onFinish() {
i=0;
}
}.start();
//Method 2: Using post.delayed
Handler handler = new Handler();
for (i = 0; i < arrayLength ; i++) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
String transmitColour = transmitArray[i];
colourView.setBackgroundColor(Color.parseColor(transmitColour));
}
}, transmitFreq);
}*/
//Method 3: Using thread.sleep
for (i = 0; i < arrayLength ; i++) {
String transmitColour = transmitArray[i];
colourView.setBackgroundColor(Color.parseColor(transmitColour));
try {
Thread.sleep(transmitFreq);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}

Related Links

Spring MVC: Can I point JSTL Import tag URL at an endpoint relative to current controller
Modular Java Application
Get JSON from neo4j cypher query using spring-data-neo4j
Fastest way to clean HTML from String and return results in Array Android / Java
Java: In Eclipse, is it possible to show an instance variable's name while debugging one of it's methods?
Button is not responding after clicking even with clicklistener class enabled
How to stop a thread within a keyhandler
How can I get started on writing a 3D environment? [closed]
How to read mule project version?
Java - array is being changed for no reason
void method recursion error
Persisting in joined tables with JPA and JSF
How to inspect local variables of method compiled with no debugging info?
Concurrent access to a Remote Object Java RMI
getBoolean won't accept string key with getSharedPreferences
Using Java 7 HashMap in Java 8

Categories

HOME
twitter
netsuite
keycloak
openstack
azure-data-factory
ngrx
push-notification
homebrew
rdf
sqlite3
mvvmcross
datastax-java-driver
azure-media-services
multiple-records
add
vault
izpack
windows-phone-7
fortumo
ibm-odm
dosgi
after-effects
facebook-page
propel
http-status-code-504
jqwidget
opencover
tibco-mdm
crosstab
ping
elasticsearch-net
wtx
compatibility
http-get
code-contracts
force-layout
environment-modules
scaffold
retina-display
sharefile
twitch
azure-ml
tasker
preconditions
galen
midl
wso2carbon
rainbowtable
python-c-api
git-diff
svmlight
forever
crypt
arrow-keys
yt-project
nested-sets
passport-google-oauth
merge-conflict-resolution
impresspages
holder.js
persist
essence
fadeout
photobucket
vhd
content-length
quicklisp
microbenchmark
splash
ios9.1
python-green
fscommand
knuth
xcode-6.2
internet-connection
flask-cors
has-many-through
app42
flexmojos
oam
html4
enterprisedb
browser-detection
gil
tridion-worldserver
errai
ocx
wse3.0
transactionscope
django-tagging
winbugs14
castle-monorail
temporal-database
project-hosting
sector
w3wp
caching-application-block

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App