Change button visibility from outside thred that created it

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

Change button visibility from outside thred that created it

Postby ameetnsharma » Tue Dec 22, 2009 8:41 am

I have a game running in a loop in a thread...

There are some buttons I created before starting the game thread...

is it possible to change the visibility of the buttons from inside the game thread? I'm getting an exception that says you can't change the view from outside the thread that created it...

Is there a way around this?

Thanks.
ameetnsharma
Developer
Developer
 
Posts: 30
Joined: Tue Dec 08, 2009 10:55 pm

Top

Postby achie1266 » Tue Dec 22, 2009 9:18 am

yes you can make them as static variables and then set the visibility as you want them.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby ameetnsharma » Tue Dec 22, 2009 9:45 am

Thanks achie1266. For some reason that didn't work. I went ahead and set up a handler as described here:

http://developer.android.com/guide/appe ... tasks.html

using

final Handler mHandler = new Handler();

// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
updateResultsInUi();
}
};


etc...


Seems to be working so far.
ameetnsharma
Developer
Developer
 
Posts: 30
Joined: Tue Dec 08, 2009 10:55 pm

Postby achie1266 » Tue Dec 22, 2009 5:36 pm

or try to acces the button itself like this and see if it works. This is working for me
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Button myBtn = (Button)findViewById(R.id.my_btn);
  2.  
  3. myBtn.setVisibility(View.VISIBLE);
Parsed in 0.029 seconds, using GeSHi 1.0.8.4
Can you also post the code snippent for how you have set the visibility in your updateResultsInUi method. I would like to use it if that is a better approach than what I am doing.
Thank you.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby ameetnsharma » Tue Dec 22, 2009 7:35 pm

Hi achie. This is my exact code at the beginning right after the view is created:

final Handler mHandler = new Handler();

final Runnable mUpdateResults = new Runnable() {
public void run() {

menuButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.INVISIBLE);

}
};


and this is the code I use from inside the external thread:

mHandler.post(mUpdateResults);



So I don't have an actual updateResultsInUi method... I just put in the code in he run() method directly.
ameetnsharma
Developer
Developer
 
Posts: 30
Joined: Tue Dec 08, 2009 10:55 pm

Postby achie1266 » Tue Dec 22, 2009 7:56 pm

hmm cool,
Thanks for posting the code.

btw it will be nice if you can use the code formatting in your posts to distinguish java and xml from the discussion.
Thanks again.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Top

Postby ameetnsharma » Tue Dec 22, 2009 9:54 pm

achie1266 wrote:hmm cool,
Thanks for posting the code.

btw it will be nice if you can use the code formatting in your posts to distinguish java and xml from the discussion.
Thanks again.


You're welcome. :)

Thanks, yeah I will use it in the future.
ameetnsharma
Developer
Developer
 
Posts: 30
Joined: Tue Dec 08, 2009 10:55 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google [Bot] and 7 guests