Reputation: 2655
When I'm trying to use 9patch background programmatically,
it only works when: (scenario 1) the 'Stretch Regions' (top/left) is bigger then the 'Content Padding Regions' (bottom/right)
but if: (scenario 2) the padding is bigger then the stretch - I'm getting this error:
02-08 10:32:04.203: E/AndroidRuntime(397): FATAL EXCEPTION: main
02-08 10:32:04.203: E/AndroidRuntime(397): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.game.test}: java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.os.Looper.loop(Looper.java:123)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-08 10:32:04.203: E/AndroidRuntime(397): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 10:32:04.203: E/AndroidRuntime(397): at java.lang.reflect.Method.invoke(Method.java:507)
02-08 10:32:04.203: E/AndroidRuntime(397): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-08 10:32:04.203: E/AndroidRuntime(397): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-08 10:32:04.203: E/AndroidRuntime(397): at dalvik.system.NativeStart.main(Native Method)
02-08 10:32:04.203: E/AndroidRuntime(397): Caused by: java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-08 10:32:04.203: E/AndroidRuntime(397): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
anyone knows how to solve scenario 2?
Code:
linLayout = new LinearLayout(this);
NinePatchDrawable ninep = (NinePatchDrawable)getResources().getDrawable(R.drawable.ninepatch_background) ;
linLayout.setBackground(ninep);
Upvotes: 2
Views: 4989
Reputation: 2655
What I end up doing is to use scenario 1 NinePatch (Stretch is bigger then Padding - which I didn't want)
and set up the padding zone manually, with this code:
NinePatchDrawable ninepatch;
Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.ninepatch_background);
if (image.getNinePatchChunk()!=null){
byte[] chunk = image.getNinePatchChunk();
Rect paddingRectangle = new Rect(30, 0, 30, 50);
ninepatch = new NinePatchDrawable(getResources(), image, chunk, paddingRectangle, null);
}
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
linLayout.setBackgroundDrawable(ninepatch);
} else {
linLayout.setBackground(ninepatch);
}
Upvotes: 1
Reputation: 4733
There is no need to create a NinePatchDrawable
object. You can simply use setBackgroundResource(R.drawable.ninepatch_background)
. I don't know if that's the problem but I hope it helps.
Upvotes: 3