Custom XML Attributes

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

Custom XML Attributes

Postby mdownie » Sat Dec 20, 2008 12:21 am

I am having a little trouble getting custom attributes to work. Here is my attrs.xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <resources>
  4.  
  5.      <declare-styleable name="PokerButton">
  6.  
  7.         <attr name="spade" format="integer" />
  8.  
  9.         <attr name="club" format="integer" />
  10.  
  11.         <attr name="heart" format="integer" />
  12.  
  13.         <attr name="diamond" format="integer" />
  14.  
  15.     </declare-styleable>
  16.  
  17.      <declare-styleable name="PokerButtonSuit">
  18.  
  19.         <attr name="suit" format="integer" />
  20.  
  21.     </declare-styleable>
  22.  
  23. </resources>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4

The top 4 work fine in this code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public PokerButton(Context context, AttributeSet attrs){
  2.  
  3.                 super(context, attrs);
  4.  
  5.                 this.ctx = context;
  6.  
  7.                 this.bg = this.getBackground();
  8.  
  9.                 setOnClickListener(this);
  10.  
  11.                 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PokerButton);
  12.  
  13.                 this.suit = new Suit[] { new Suit(a.getDrawable(R.styleable.PokerButton_spade)), new Suit(a.getDrawable(R.styleable.PokerButton_club)), new Suit(a.getDrawable(R.styleable.PokerButton_heart)), new Suit(a.getDrawable(R.styleable.PokerButton_diamond)) };
  14.  
  15.                 a.recycle();
  16.  
  17.         }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

But, this code does not seem to work:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class PokerButtonSuit extends Button implements View.OnClickListener {
  2.  
  3.         private Context ctx = null;
  4.  
  5.         private int suit;
  6.  
  7.         public PokerButtonSuit(Context context, AttributeSet attrs, int defStyle){
  8.  
  9.                 super(context, attrs, defStyle);
  10.  
  11.         }
  12.  
  13.         public PokerButtonSuit(Context context, AttributeSet attrs){
  14.  
  15.                 super(context, attrs);
  16.  
  17.                 ctx = context;
  18.  
  19.                 setOnClickListener(this);
  20.  
  21.                 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PokerButtonSuit);
  22.  
  23.                 this.suit = a.getInteger(R.styleable.PokerButtonSuit_suit, 0);
  24.  
  25.                 a.recycle();
  26.  
  27.         }
  28.  
  29.         public PokerButtonSuit(Context context){
  30.  
  31.                 super(context);
  32.  
  33.         }
  34.  
  35.         public void onClick(View v){
  36.  
  37.                 this.setText(this.suit);
  38.  
  39.         }
  40.  
  41. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

I did notice in my research that there is a TypedArray.getInteger() and a TypedArray.getInt(). No idea what the difference is. All I want to do is pass an integer through XML so I can differentiate which object I am dealing with inside the class I made. Any advice would be greatly appreciated. Thank you.
mdownie
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Nov 25, 2008 6:54 pm
Location: Las Vegas

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests