1. Download our Official Android App: Forums for Android!

Apps StateListDrawable not working

Discussion in 'Android Development' started by Mtihc, Jun 27, 2011.

  1. Mtihc

    Mtihc Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    8
    Joined:
    Jun 27, 2011

    Jun 27, 2011
    8
    0
    5
    Hi,

    I have a whole bunch of buttons that have different backgrounds.
    I learnt about the Drawable Resources and the <selector> tag.

    So I guess I need to make an XML file for every button?

    I thought of making 1 image-file (per state) that has 32x32px reserved for every button's background. I would cut up the image and assign every piece to a button.

    Of course I cannot do that in XML so I tried to use the StateListDrawable class.
    But somehow it doesn't work.

    Here's the code (createBitmap() returns the cutout piece as BitmapDrawable):
    Code (Text):
    1.     private Button createButton(int buttinIndex, int buttonWidth, int buttonHeight, int regular, int pressed, int focused)
    2.     {
    3.         StateListDrawable drawables = new StateListDrawable();
    4.        
    5.        
    6.         BitmapDrawable bitmapPressed = createBitmap(
    7.                 pressed,
    8.                 buttinIndex,
    9.                 buttonWidth, buttonHeight);
    10.         BitmapDrawable bitmapFocused = createBitmap(
    11.                 focused,
    12.                 buttinIndex,
    13.                 buttonWidth, buttonHeight);
    14.         BitmapDrawable bitmapRegular = createBitmap(
    15.                 regular,
    16.                 buttinIndex,
    17.                 buttonWidth, buttonHeight);
    18.        
    19.         drawables.addState(new int[]{android.R.attr.state_pressed}, bitmapPressed);
    20.         drawables.addState(new int[]{android.R.attr.state_focused}, bitmapFocused);
    21.         drawables.addState(new int[]{android.R.attr.state_enabled}, bitmapRegular);
    22.         drawables.addState(new int[]{-android.R.attr.state_enabled}, bitmapPressed);
    23.        
    24.        
    25.         Button result = new Button(this);
    26.         result.setBackgroundDrawable(drawables);
    27.         //result.setBackgroundResource(R.drawable.examplebutton);
    28.         return result;
    29.     }
    This is where I call the above method:
    Code (Text):
    1.             i++;
    2.             Button imgButton = createButton(i-1, 32, 32, R.drawable.hangman_letters_regular, R.drawable.hangman_letters_pressed, R.drawable.hangman_letters_focused);


    When I run the app it shows all buttons in the default state, and clicking it does not change a thing.
    So the images are all cut up nicely, and assigned to buttons, but the states are not working.

    Anybody know why?
     

    Advertisement

  2. alostpacket

    alostpacket Over Macho Grande?
    Rank:
    None
    Points:
    513
    Posts:
    7,972
    Joined:
    Nov 29, 2009

    Nov 29, 2009
    7,972
    3,603
    513
    Android App Developer
    NY
    I'm not entirely sure what's going wrong but have two suggesstions:

    Have you looked at 9-patch drawables? They do something similar to what you describe, but maybe not exactly....

    Drawable Resources | Android Developers

    You might want to use 9-patch anyways as it handles the scaling better..

    The other is to check and make sure you have all states covered. As in one state might be focused AND enabled... Which I think is the reason the addState() method takes an int array as its first argument.

    Otherwise I'm not sure, hope that helps.
     
  3. Mtihc

    Mtihc Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    8
    Joined:
    Jun 27, 2011

    Jun 27, 2011
    8
    0
    5
    Thanks for the reply.

    I know what the 9patch drawable does. But the images I'm using are not scaleable that way.

    The other thing might be worth a try. Although I have seen examples on the internet that define the states in exactly the same way. But I will try out some different combo's of states. And see what happens.

    If anybody has another idea, plz mention it :)
     
  4. Mtihc

    Mtihc Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    8
    Joined:
    Jun 27, 2011

    Jun 27, 2011
    8
    0
    5
    I fixed it. It was a really stupid mistake in the createBitmap() method. It didn't update the source bitmap for every state. So every state got the same image...

    I should go learn how to work the debugger hihi

    Thanks anyway :)
     
  5. alostpacket

    alostpacket Over Macho Grande?
    Rank:
    None
    Points:
    513
    Posts:
    7,972
    Joined:
    Nov 29, 2009

    Nov 29, 2009
    7,972
    3,603
    513
    Android App Developer
    NY
    You're welcome, glad you got it fixed :)
     

Share This Page

Loading...