I would like to create a custom Android button from 9patches, but without using any XML. I know, it sounds dumb, but the reason is that for some reason my IDE (NetBeans) messes up the whole XML beyond belief. The R.java doesn’t get generated half the time (the fixes didn’t work for me), and I get random errors, even if I just copy and paste XML from tutorials.
This is how I plan to create the Button:
- The button has a
setBackgroundDrawable()method - I need to suply this method with a
StateListDrawable - Which I need to build up from
NinePatchDrawables
So first I need to create the 9patches, then a StateListDrawable from them, and simply pass the StateListDrawable as background for my Button’s setBackgroundDrawable() method.
I can’t start the 9patches, because I can’t find out how the constructors work.
Wouldn’t it make more sense for you to invest your time in either:
Trying to do Android development without XML is akin to trying to fly without wings. It’s possible, but rockets tend to crash and burn in the end.
Regardless, you do not need to directly work with
NinePatchDrawableto use nine-patch PNG files. Android will handle that part for you.Instead, create an instance of
StateListDrawableand calladdState()for each one of your states. To get theDrawablefor the state, usegetResources().getDrawable(R.drawable.this_is_one_of_your_nine_patch_images)from yourActivity. Android will detect that this is a nine-patch and will do the right thing.