little view problem

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

little view problem

Postby swordi » Tue May 19, 2009 9:51 am

hello

im new at developing with android and i have a little problem with my app.

first i have an activity with some elements on it and one button

on button click it changes to a new activity with 5 imagebuttons on it.

i selected an image for one of these buttons.

for another button i wanted to select the image on runtime via source code.

Button bt11 = (Button)findViewById(R.id.bt11);
bt11.setBackgroundResource(R.drawable.a);

as result i see nothing - the new image is not on the button

i think i havent seen something important, but i dont check this

thanks for your help

if you dont understand something, please let me know it
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Top

Reply

Postby satishkumar_lskin » Tue May 19, 2009 11:21 am

Hi,
U should use ImageButton class if u need to place images in buttons. Button class is used to create push button type widgets ( similar to a HTML Form Submit button ) .

Regards
Satish
satishkumar_lskin
Developer
Developer
 
Posts: 32
Joined: Mon Oct 20, 2008 8:15 am
Location: Coimbatore , TamilNadu, India

Postby swordi » Tue May 19, 2009 11:45 am

thanks for your reply

its clear - i have to use the imagebutton. thats right

now i changed it from button to imagebutton in the source now but nothing happens

its like the view will not be updated after changing something.

is there maybe a statement to update the whole view or something?
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Reply

Postby satishkumar_lskin » Tue May 19, 2009 12:12 pm

Hi,
Can u post the code snippet that u say is not working for u ? To update a View , invalidate() has to be called. invalidate() inturn invokes onDraw() on the View. But this method is rarely needed.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. bt11.setBackgroundResource(newResourceId);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
should work correctly.
satishkumar_lskin
Developer
Developer
 
Posts: 32
Joined: Mon Oct 20, 2008 8:15 am
Location: Coimbatore , TamilNadu, India

Postby swordi » Tue May 19, 2009 12:14 pm

here is the code of my second activity, with the buttons


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class GameActivity extends Activity{
  2.  
  3.        
  4.  
  5.         public void onCreate(Bundle savedInstanceState) {
  6.  
  7.     super.onCreate(savedInstanceState);
  8.  
  9.    
  10.  
  11.     ImageButton bt11 = (ImageButton)findViewById(R.id.bt11);
  12.  
  13.    
  14.  
  15.     bt11.setBackgroundResource(R.drawable.a);    
  16.  
  17.    
  18.  
  19. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


invalidate?

from witch class is this method?
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Reply

Postby satishkumar_lskin » Tue May 19, 2009 12:19 pm

Hi,
Have u used
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  setContentView(yourLayout) ;
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


in ur GameActivity ? . Other than that, ur code looks good.
invalidate() is available for any Class that extends View such as ImageButton. For ur need, u don't have to use it.
satishkumar_lskin
Developer
Developer
 
Posts: 32
Joined: Mon Oct 20, 2008 8:15 am
Location: Coimbatore , TamilNadu, India

Top

Postby swordi » Tue May 19, 2009 12:27 pm

omg - here is a big problem - nothing changes ;)

no i have

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class GameActivity extends Activity{
  2.  
  3.        
  4.  
  5.         public void onCreate(Bundle savedInstanceState) {
  6.  
  7.     super.onCreate(savedInstanceState);
  8.  
  9.    
  10.  
  11.     ImageButton bt11 = (ImageButton)findViewById(R.id.bt11);
  12.  
  13.     bt11.setBackgroundResource(R.drawable.a);  
  14.  
  15.    
  16.  
  17.     setContentView(R.layout.game);
  18.  
  19.    
  20.  
  21.         }
  22.  
  23.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


but its also without changes

infuriatingly
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Reply

Postby satishkumar_lskin » Tue May 19, 2009 12:33 pm

Hi,
Do u use
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. context.startActivity(intent);
  2.  
  3.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

Is the onCreate() method of ur new activity (GameActivity ) getting invoked ?
I have tried launching new activities from an existing activity on a UI event such as a button click. It works for me.
satishkumar_lskin
Developer
Developer
 
Posts: 32
Joined: Mon Oct 20, 2008 8:15 am
Location: Coimbatore , TamilNadu, India

Postby swordi » Tue May 19, 2009 3:16 pm

ok now i got it

it was this context.startActivity()

i started it false.

thank you man

but now i get an exception at

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ImageButton bt11 = (ImageButton)findViewById(R.id.bt11);
  2.  
  3.     bt11.setBackgroundResource(R.drawable.a);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


any hints?
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Postby nmc » Tue May 19, 2009 6:58 pm

findViewById() will always be null when used before setContentView()
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby swordi » Tue May 19, 2009 7:11 pm

oooooohhhh yes

now it works !!

thanks to all here
swordi
Developer
Developer
 
Posts: 46
Joined: Tue May 19, 2009 9:44 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests