View.setVisible from a separate thread?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

View.setVisible from a separate thread?

Postby mortefer » Tue Dec 23, 2008 2:45 pm

Hi every1,
I've got a little problem here. I have an OpenGL thread that renders some of my stuff. On one point i need to display another view that has some buttons in it. The question is - how this can be done? because calling View.setVisible(View.VISIBLE) directly from OpenGL thread raises an exception (CalledFromWrongThreadException) that states that "Only the original thread that created a view hierarchy can touch it's views". Maybe i can set up some kind of listener for an activity that will periodically check for a flag and show other view if needed?

Thanx in advance
mortefer
Experienced Developer
Experienced Developer
 
Posts: 54
Joined: Sat Dec 20, 2008 11:24 am

Top

Postby mortefer » Tue Dec 23, 2008 8:37 pm

Okay looks like i'm gonna answer my own question :)
this is a synchronization issue that can be fixed using Handler object.
Main thread (Activity) creates a handler object and passes it to child processes. When needed, child processes post messages to the handler that are caught using handleMessage method of handler object (you have to subclass from Handler in order to implement that method) and there you can do what ever you want.
mortefer
Experienced Developer
Experienced Developer
 
Posts: 54
Joined: Sat Dec 20, 2008 11:24 am

Postby MrSnowflake » Wed Dec 24, 2008 11:03 am

Actually you don't need a handler. To do some stuff on the Activities thread you should use Activity.runOnUiThread(Runnable). It is the same in use as your Handler solution (wich is the way to go for non Activity classes!).
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google Feedfetcher, Majestic-12 [Bot] and 9 guests