Button to change Background using Tab's

Put your problem here if it does not fit any of the other categories.

Button to change Background using Tab's

Postby aliasbody » Thu Jul 01, 2010 8:56 pm

Hi everyone !
To start sorry for my very bad english. I am developping an android software, and i want to implement a serie of buttons that allow the user to change the background of the app. The problem is, since I start using the Tab's (TabHost etc..), i have problems using the simple "getWindow().setBackgroundDrawableResource(R.drawable.wallpaper);", because he doesn't work anymore.

I have just one button in one class file (Options), and just clicking on the picture showing the wallpaper, the wallpaper appears on every tab content.

Is there anyway to resolve this problem ?

Thanks in advance for your help !
aliasbody
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jul 01, 2010 8:46 pm

Top

Re: Button to change Background using Tab's

Postby blundell » Thu Jul 01, 2010 10:58 pm

Hmm post some nice neat code and I can tell you were your going wrong.

Why call getWindow? Why not get the current View that your tabhost is using i.e. your tab host's setContent and call the native method.

If you think you've got a dodgy tab setup check out:
http://www.gregbugaj.com/?p=6
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Button to change Background using Tab's

Postby aliasbody » Thu Jul 01, 2010 11:25 pm

Thanks for you reply !
I found a solution, but it only works on the used class like this :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.aliasbody.app.bodyimc;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8.  
  9. public class Body_IMC_Sobre extends Activity
  10. {
  11.     public Button BtnSair;
  12.     @Override
  13.     public void onCreate(Bundle bodyimc_sobre)
  14.     {
  15.         super.onCreate(bodyimc_sobre);
  16.         setContentView(R.layout.main_sobre);
  17.         BtnSair = (Button)findViewById(R.id.BtnSair);
  18.         BtnSair.setOnClickListener(new OnClickListener()
  19.         {
  20.             public void onClick(View v)
  21.             {
  22.                 Body_IMC_Sobre.this.getWindow().setBackgroundDrawableResource(R.drawable.fundo_azul);
  23.                 //System.exit(0);
  24.             }
  25.         });
  26.     }
  27. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


On this example i have changed the exit button, just to change the color, and it works, but i want to do this for each button, and each class.

I don't find what i need in your site :S either that or it was me who didn't understand...

Thanks in advance
aliasbody
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jul 01, 2010 8:46 pm

Re: Button to change Background using Tab's

Postby blundell » Fri Jul 02, 2010 8:37 am

You need to be looking in your layout xml file.

Just add a drawable to each of your tab child tag's.
android:background="blah"

This is pseudo android:
Code: Select all
<tabHost>
   <tabWidget>
       <FrameLayout>
          <LinearLayout android:background="blah1"> <!-- TAB 1 -->
            ... stuff ..
          </LinearLayout>
          <LinearLayout android:background="blah2"> <!-- TAB 2 -->
          </LinearLayout>
          <LinearLayout android:background="blah3"> <!-- TAB 3 -->
          </LinearLayout>
       </FrameLayout>
</tabHost>

Better looking at:
how_to_create_tab_in_android-t1102.html

But above is the jist
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Button to change Background using Tab's

Postby aliasbody » Fri Jul 02, 2010 5:21 pm

Hi
Thanks for your help !
But if i do that i will have a especifique wallpaper on each tab, and what i want it's to let the user choose just by clicking in a button :S

How can i do that :S ?

Thanks in advance,
Aliasbody
aliasbody
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jul 01, 2010 8:46 pm

Re: Button to change Background using Tab's

Postby blundell » Fri Jul 02, 2010 5:24 pm

So once you have done what I said, you can then get the layout (that you have set the background on) and change it to whatever you want. Something like:
Code: Select all
LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.LinearLayout1);
myLinearLaout.setBackgroundResource(R.drawable.image1);


http://developer.android.com/reference/android/view/View.html#setBackgroundResource(int)
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Re: Button to change Background using Tab's

Postby aliasbody » Fri Jul 02, 2010 6:01 pm

Thanks ! it works like a charm ^^
PS : i want to create a button to save the wallpaper, so that the user when restarting the application will have is wallpaper again, i have to do this with the code forcing the wallpaper or the xml?

thanks in advance ^^
aliasbody
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jul 01, 2010 8:46 pm

Re: Button to change Background using Tab's

Postby blundell » Fri Jul 02, 2010 7:09 pm

You mean when the app starts you want the saved wallpaper to be set?

You need to look into shared preferences
http://developer.android.com/guide/topi ... orage.html

in your onCreate() you'll have something like

Code: Select all
SharedPreferences settings = getSharedPreferences("myAppsPrefs", 0);
       int savedWallpaperId = settings.getInt("savedWallpaper", R.drawable.defaultWallpaper);
if(savedWallpaperId != R.drawable.defaultWallpaper){
      myLinearLaout.setBackgroundResource(savedWallpaperId);
}


And obviously you have to code your save wallpaper button to the preferences under the same name "savedWallpaper"

*edit* you probably don't even need the If statement, your choice
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Button to change Background using Tab's

Postby aliasbody » Fri Jul 02, 2010 8:47 pm

Hello again.
Thanks for the "savedWallpaper" solution ^^ but i have a problem, i tryed to change the wallpaper with your method and it works, but i didn't try with the others tabs, now when i try the aplications do a "FC", and i don't know what to do... i show you the java code :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.aliasbody.app.bodyimc;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.AbsoluteLayout;
  8. import android.widget.Button;
  9. import android.widget.ImageView;
  10.  
  11. public class Body_IMC_Opcoes extends Activity
  12. {
  13.     public Button BtnSair;
  14.     public ImageView prev1;
  15.     public ImageView prev2;
  16.     public ImageView prev3;
  17.     public ImageView prev4;
  18.     public AbsoluteLayout albodyimc_main;
  19.     public AbsoluteLayout albodyimc_opcoes;
  20.     public AbsoluteLayout albodyimc_historicos;
  21.     public AbsoluteLayout albodyimc_sobre;
  22.    
  23.     @Override
  24.     public void onCreate(Bundle bodyimc_opcoes)
  25.     {
  26.         super.onCreate(bodyimc_opcoes);
  27.         setContentView(R.layout.main_opcoes);
  28.         albodyimc_main = (AbsoluteLayout)findViewById(R.id.BodyIMC_main);
  29.         albodyimc_opcoes = (AbsoluteLayout)findViewById(R.id.BodyIMC_opcoes);
  30.         albodyimc_historicos = (AbsoluteLayout)findViewById(R.id.BodyIMC_historicos);
  31.         albodyimc_sobre = (AbsoluteLayout)findViewById(R.id.BodyIMC_sobre);
  32.         prev1 = (ImageView)findViewById(R.id.prev1);
  33.         prev1.setOnClickListener(new OnClickListener()
  34.         {
  35.             public void onClick(View v)
  36.             {
  37.                 albodyimc_opcoes.setBackgroundResource(R.drawable.fundo_azul);
  38.                 albodyimc_historicos.setBackgroundResource(R.drawable.fundo_azul);
  39.                 //albodyimc_sobre.setBackgroundResource(R.drawable.fundo_azul);
  40.                 //albodyimc_main.setBackgroundResource(R.drawable.fundo_azul);
  41.             }
  42.         });
  43.         prev2 = (ImageView)findViewById(R.id.prev2);
  44.         prev3 = (ImageView)findViewById(R.id.prev3);
  45.         prev4 = (ImageView)findViewById(R.id.prev4);
  46.  
  47.         BtnSair = (Button)findViewById(R.id.BtnSair);
  48.         BtnSair.setOnClickListener(new OnClickListener()
  49.         {
  50.             public void onClick(View v)
  51.             {
  52.                 //albodyimc_opcoes.setBackgroundResource(R.drawable.fundo_azul);
  53.                 albodyimc_historicos.setBackgroundResource(R.drawable.fundo_azul);
  54.                 //System.exit(0);
  55.             }
  56.         });
  57.     }
  58. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


PS : all the xml files used there, uses AbsoluteLayout, but only the selected tab allows to change the background.

Thanks in advance!
Aliasbody,
aliasbody
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jul 01, 2010 8:46 pm

Re: Button to change Background using Tab's

Postby blundell » Fri Jul 02, 2010 10:31 pm

Sorry you can't just throw code at me and expect a fix!

What does your Logcat error console say?
Debuggin Eclipse
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Button to change Background using Tab's

Postby aliasbody » Fri Jul 02, 2010 11:21 pm

euh :S yes you're right i'm sorry :S...
PS : i'm using netbeans instead of eclipse so i will try to find a way to do this on netbeans and put here the Logcat.

Thanks in advance once again ^^
aliasbody
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jul 01, 2010 8:46 pm

Re: Button to change Background using Tab's

Postby aliasbody » Sat Jul 03, 2010 12:07 am

I find a way to see the log and use the debugger with netbeans but not to find de Logcat to put here...
I've tried to use breakpoint line by line, and he didn't found any error with the function to set the background, but only after that.

And the Log Window says :
Syntax: [ Download ] [ Hide ]
Using scala Syntax Highlighting
  1. 23:00:15.516    1687    WARN    dalvikvm        threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
  2. 23:00:15.526    1687    ERROR   AndroidRuntime  Uncaught handler: thread main exiting due to uncaught exception
  3. 23:00:15.706    1687    ERROR   AndroidRuntime  java.lang.NullPointerException
  4. 23:00:15.706    1687    ERROR   AndroidRuntime      at com.aliasbody.app.bodyimc.Body_IMC_Opcoes$2.onClick(Body_IMC_Opcoes.java:56)
  5. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.view.View.performClick(View.java:2344)
  6. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.view.View.onTouchEvent(View.java:4133)
  7. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.widget.TextView.onTouchEvent(TextView.java:6510)
  8. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.view.View.dispatchTouchEvent(View.java:3672)
  9. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
  10. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
  11. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
  12. 23:00:15.706    1687    ERROR   AndroidRuntime      at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
  13. 23:00:15.706    1687    ERROR   AndroidRuntime      at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
  14. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
  15. 23:00:15.706    1687    ERROR   AndroidRuntime      at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
  16. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
  17. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
  18. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
  19. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
  20. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
  21. 23:00:15.706    1687    ERROR   AndroidRuntime      at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
  22. 23:00:15.706    1687    ERROR   AndroidRuntime      at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
  23. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
  24. 23:00:15.706    1687    ERROR   AndroidRuntime      at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
  25. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
  26. 23:00:15.706    1687    ERROR   AndroidRuntime      at android.os.Handler.dispatchMessage(Handler.java:99)
  27.  
Parsed in 0.012 seconds, using GeSHi 1.0.8.4


I don't know if this can help :S

Thanks in advance.
Aliasbody
aliasbody
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jul 01, 2010 8:46 pm

Re: Button to change Background using Tab's

Postby anarche » Sat Jul 03, 2010 1:02 am

Irrelevent, but why are you using netbeans?

You have a null pointer here albodyimc_historicos.setBackgroundResource(R.drawable.fundo_azul) somewhere. I'd bet on the drawable not being compiled. Try cleaning you project and double-checking the names of the files.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Button to change Background using Tab's

Postby aliasbody » Sat Jul 03, 2010 2:13 am

Hi !
I use netbeans because it's more easy (for me) than eclipse (for java only), because he propose "swing" to create easy GUI programs with Java.

for the null pointer :
the problem is, as you can see i am in the Body_IMC_Opcoes class, and if i tried to make the fundo_azul appears with albodyimc_opcoes how calls the Body_IMC_Opcoes xml, it works, but if i tried with another one like albodyimc_historicos how calls the Body_IMC_Historicos xml he doesn't work at all.

I have verified all the names and everything is fine, i think i have to use another way to call the AbsoluteLayout of the others classes...

Ps : once again sorry for my bad english, if you don't understand something please tell me ^^
thanks in advance.
Aliasbody,
aliasbody
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Jul 01, 2010 8:46 pm

Re: Button to change Background using Tab's

Postby anarche » Sat Jul 03, 2010 2:35 am

Fair call. I was just curious about the netbeans. The Android GUI maker for eclipse is pretty good tho'

Ok you are not in the Body_IMC_Opcoes class, you are in the OnClickListener class. Just because its anonymous doesn't mean its not there ;p

Pop up your xml please? So I can have a look at it.

PS. your english hurts my head. Probably as much as my Spanish(??) would hurt yours ;p
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Top
Next

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 18 guests