Reputation: 9
I have a project which a activity have more than 1000 buttons. i am updating the background color of these buttons after taking the color information from the database. This whole process is a very big process and took 8 seconds to load my activity. i have implement Asynctask for that but the problem is with this exception because "Only the original thread that created a view hierarchy can touch its views". because my long operation is on that part where i am updating my UI elements
After that i have implemented a thread that runs on UIthread for updating ui elements, this works fine according to its function, this thread update my ui but it stucks my application for 5-6 seconds. and then i thought of implementing a progress dialog for that 5-6 seconds. but if i implement progress dialog on runOnUiThread(new Runnable(){} it doesn't work. because updating ui and progress dialog runs on the same time. so progress dialog flashes for few milliseconds. and the activity still remains the same as it was before. I don't know that to use for updating ui if they took long time to update.
This is my code where i update by ui elements from database.
for (int i = 0; i < list.size(); i++) {
Button btn = list.get(i);
Platform p = db.setplatformncolor(btn.getTag().toString());
String color = p.getColor();
if (color.equals("red")) {
btn.setBackgroundColor(Color.RED);
}
if (color.equals("green")) {
btn.setBackgroundColor(Color.rgb(0, 176, 80));
}
Any help is appreciated. Thanks.!!
Upvotes: 0
Views: 426
Reputation: 3438
If your app api level >=11 and if you only need two colors, then you can create a drawable xml for your buttons' background like:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/red" android:state_activated="false" />
<item android:drawable="@color/green" android:state_activated="true"/>
and then just change btn.setActivated(true/false);
Upvotes: 0
Reputation: 990
this might be a bit tricky and complex solution for starters , but is efficient and applicable to your requirement.
Android has launched DataBinding Library, which will update your XML dynamically , try it
https://developer.android.com/topic/libraries/data-binding/index.html
Upvotes: 0
Reputation: 1086
AsyncTask can fix your problem. the most time consuming operation in this code is
db.setplatformncolor(btn.getTag().toString());
so you have to move this line into doInBackground()
method and after adding to DB call publishProgress()
also you should implement onProgressUpdate()
and change your button backgrounds in this method
Upvotes: 0
Reputation: 1223
You can declare a green button theme and a red button theme like this
<style name="RedButton" parent="@android:style/Widget.Button">
<item name="android:background">@android:color/holo_red_light</item>
</style>
<style name="GreenButton" parent="@android:style/Widget.Button">
<item name="android:background">@android:color/holo_green_light</item>
</style>
Then just apply the theme. The system will automatically apply your background color for all buttons. See here if want more details about themes switching.
Upvotes: 0
Reputation: 554
the for loop take two long time, try to just put the loop in thread and setBackgroundColor in ui thread.
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < list.size(); i++) {
final Button btn = list.get(i);
Platform p = db.setplatformncolor(btn.getTag().toString());
String color = p.getColor();
if (color.equals("red")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
btn.setBackgroundColor(Color.RED);
}
});
}
if (color.equals("green")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
btn.setBackgroundColor(Color.rgb(0, 176, 80));
}
});
}
}
}
}).start();
Upvotes: 1