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

Populating TreeView in less time JavaFX
Violation of single responsibility principle in Iterator from Java core
how to know where HttpServletResponse is redirecting?
While Loops - Timed
Switching on android GPS location
java- Why am I getting “class is not abstract and does not override abstract method” when creating my ATM with gui? [duplicate]
Accessing a Class which extends an AbstractClass which itself extends another Abstract Class
primefaces RequestContext inside a thread
Struggling with Reusable Functions to Handle Frames
Angle Turned Into Radians And Then Into dx. Not Working As I Thought
LinearLayout addView adds only one view
How to get java path in CentOS?
Openshift MysQL Tomcat Connection is null
Client in Client-Server Chat CPU load 30-50%
UDACITY Sunshine Android App Error - java.io.FileNotFoundException
Run-time error when handling arrays and a bubble sort (Java)

Categories

HOME
gitlab
vbscript
urbancode
angular-material
q
paw-app
cakephp-2.5
webpack-2
google-project-tango
installshield
azure-media-services
in-app-purchase
user-input
collectd
msp430
lombok
uitypeeditor
facebook-page
pugjs
riot.js
tar
core-text
language-agnostic
lcd
blazemeter
centos6.5
url-scheme
dbclient
compatibility
newline
gsmcomm
flink-streaming
media-queries
frame
galsim
data-manipulation
karaf
google-sites-2016
avcapturesession
sharefile
uft-api
leading-zero
grid.mvc
texmaker
executenonquery
scrollable
chain-builder
jxcore
django-scheduler
background-service
time-and-attendance
vmware-tools
mikroc
etsy
impresspages
actionbardrawertoggle
essence
sdf
yaws
ford-fulkerson
pervasive-sql
srand
spim
tmuxinator
citrus-pay
browser-bugs
p2
device-orientation
truevault
c++03
qpainter
phalanger
issuu
wp-query
.aspxauth
coverflow
quartz-graphics
bulkloader
ora-00911
shellexecute
eventual-consistency
gil
armcc
tridion-worldserver
ticoredatasync
wse3.0
amazon-appstore
psi
f#-powerpack
locationlistener
sql-server-profiler
imac
nerddinner
post-build
msdev
commodore

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