• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps StateListDrawable not working

Mtihc

Lurker
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:
	private Button createButton(int buttinIndex, int buttonWidth, int buttonHeight, int regular, int pressed, int focused)
	{
		StateListDrawable drawables = new StateListDrawable();
		
		
		BitmapDrawable bitmapPressed = createBitmap(
				pressed,
				buttinIndex,
				buttonWidth, buttonHeight);
		BitmapDrawable bitmapFocused = createBitmap(
				focused,
				buttinIndex,
				buttonWidth, buttonHeight);
		BitmapDrawable bitmapRegular = createBitmap(
    			regular, 
    			buttinIndex, 
    			buttonWidth, buttonHeight);
		
		drawables.addState(new int[]{android.R.attr.state_pressed}, bitmapPressed);
		drawables.addState(new int[]{android.R.attr.state_focused}, bitmapFocused);
		drawables.addState(new int[]{android.R.attr.state_enabled}, bitmapRegular);
		drawables.addState(new int[]{-android.R.attr.state_enabled}, bitmapPressed);
		
		
		Button result = new Button(this);
		result.setBackgroundDrawable(drawables);
		//result.setBackgroundResource(R.drawable.examplebutton);
		return result;
	}

This is where I call the above method:
Code:
        	i++;
        	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?
 
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.
 
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 :)
 
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 :)
 
Back
Top Bottom