Problem comparing resources...

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

Problem comparing resources...

Postby mdownie » Fri Dec 12, 2008 5:01 pm

I have a small app that I want to change the background image when it is clicked. When it is clicked again, I would like it to switch back to it's "original" image. The problem I am having is I am not sure how to compare 2 resources and get the desired result. My onClick method looks as follows:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void onClick(View v){
  2.  
  3.                 if (this.getBackground() == getResources().getDrawable(R.drawable.dead))
  4.  
  5.                         this.setBackgroundDrawable(bg);
  6.  
  7.                 else
  8.  
  9.                         this.setBackgroundResource(R.drawable.dead);
  10.  
  11.                 TextView tv = (TextView) ((Activity)ctx).findViewById(R.id.d1);
  12.  
  13.                 if (tv != null)
  14.  
  15.                         tv.setBackgroundDrawable(bg);
  16.  
  17.         }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4

I have a custom button class that extends Button and implements View.OnClickListener. What I am basically trying to do is compare what image is being using for the background of the button when it is clicked. I have looked through as much documentation as I could get my hands on and I thought this code would work, but the result from getResources().getDrawable(R.drawable.dead) is different everytime you click on the button. Any advice will be greatly appreciated.
mdownie
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Nov 25, 2008 6:54 pm
Location: Las Vegas

Top

Postby avolovoy » Fri Dec 12, 2008 5:23 pm

I'll can find the code later but i'd create a drawable field and set it up onCreate or onStart. Then if it's button i'd get drawable by getDrawable() and compare . It used to work just fine for me.
Again.. i have to go now - but i'll get the code in couple of hours.
It's seems that it's not working because you have a new object every time. Get an drawable object in create and compare and set that.

Also there is a new concept of statefull drawable which is sounds interesting - find blog of the dude who wrote compare anywhere.
Alexey Volovoy
Bytesharp.NET
User avatar
avolovoy
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Mon Nov 03, 2008 10:32 pm
Location: kansas

Postby mdownie » Fri Dec 12, 2008 5:38 pm

Let me post my whole class here:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.pokereuphoria.odds;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.content.Context;
  8.  
  9. import android.graphics.drawable.Drawable;
  10.  
  11. import android.util.AttributeSet;
  12.  
  13. import android.widget.Button;
  14.  
  15. import android.view.View;
  16.  
  17. import android.widget.TextView;
  18.  
  19.  
  20.  
  21. public class PokerButton extends Button implements View.OnClickListener {
  22.  
  23.         private Drawable bg;
  24.  
  25.         private Context ctx = null;
  26.  
  27.         public PokerButton(Context context, AttributeSet attrs, int defStyle){
  28.  
  29.                 super(context, attrs, defStyle);
  30.  
  31.                 this.ctx = context;
  32.  
  33.                 setOnClickListener(this);
  34.  
  35.         }
  36.  
  37.         public PokerButton(Context context, AttributeSet attrs){
  38.  
  39.                 super(context, attrs);
  40.  
  41.                 this.ctx = context;
  42.  
  43.                 bg = this.getBackground();
  44.  
  45.                 setOnClickListener(this);
  46.  
  47.         }
  48.  
  49.         public PokerButton(Context context){
  50.  
  51.                 super(context);
  52.  
  53.                 this.ctx = context;
  54.  
  55.                 setOnClickListener(this);
  56.  
  57.         }
  58.  
  59.         public void onClick(View v){
  60.  
  61.                 if (this.getBackground() == getResources().getDrawable(R.drawable.dead))
  62.  
  63.                         this.setBackgroundDrawable(bg);
  64.  
  65.                 else
  66.  
  67.                         this.setBackgroundResource(R.drawable.dead);
  68.  
  69.                 TextView tv = (TextView) ((Activity)ctx).findViewById(R.id.d1);
  70.  
  71.                 if (tv != null)
  72.  
  73.                         tv.setBackgroundDrawable(bg);
  74.  
  75.         }
  76.  
  77. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4

I would understand why the this.getBackground() would change because the background is changing, but I save the original background in the constructor as Drawable bg. The weird thing that changes is the getResources().getDrawable(R.drawable.dead) because I am referencing the exact same resource id, but it returns a different value every time. Maybe I shouldn't be calling the getResources() method every time the button is clicked? Thanks again.
mdownie
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Nov 25, 2008 6:54 pm
Location: Las Vegas

Postby avolovoy » Fri Dec 12, 2008 8:16 pm

I didn't have time to put your code into work but this is working

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ButtonTest extends Activity {
  2.  
  3.     private Drawable mBg;
  4.  
  5.     private Drawable mDead;
  6.  
  7.  
  8.  
  9.     /** Called when the activity is first created. */
  10.  
  11.     @Override
  12.  
  13.     public void onCreate(Bundle savedInstanceState) {
  14.  
  15.         super.onCreate(savedInstanceState);
  16.  
  17.         setContentView(R.layout.main);
  18.  
  19.         mBg = this.getResources().getDrawable(R.drawable.o);
  20.  
  21.         mDead = this.getResources().getDrawable(R.drawable.x);
  22.  
  23.  
  24.  
  25.         LinearLayout mainView = (LinearLayout) findViewById(R.id.top);
  26.  
  27.  
  28.  
  29.         Button currentButton = new Button(getBaseContext());    
  30.  
  31.         currentButton.setBackgroundDrawable(mBg);
  32.  
  33.  
  34.  
  35.         mainView.addView(currentButton);
  36.  
  37.         currentButton.setOnClickListener(new View.OnClickListener() {
  38.  
  39.             public void onClick(View v) {
  40.  
  41.  
  42.  
  43.                 if (v.getBackground().equals(mBg))
  44.  
  45.                     v.setBackgroundDrawable(mDead);
  46.  
  47.                 else
  48.  
  49.                     v.setBackgroundDrawable(mBg);
  50.  
  51.  
  52.  
  53.             }
  54.  
  55.         });
  56.  
  57.     }
  58.  
  59. }
  60.  
  61.  
Parsed in 0.049 seconds, using GeSHi 1.0.8.4
Alexey Volovoy
Bytesharp.NET
User avatar
avolovoy
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Mon Nov 03, 2008 10:32 pm
Location: kansas

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google Feedfetcher and 7 guests