Question

I want to change the background of a layout when it's pressed. The layout isn't always the same shape, so I created a 9-patch image that will keep the image centered. My image looks like this:

enter image description here

Then my selector xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
 <item 
     android:state_pressed="true" 
     android:drawable="@drawable/my_selector" 
     />
 <item>
     <shape android:shape="rectangle">
         <solid android:color="#00ffffff" />
     </shape>
 </item>
</selector>

When I had this code and my image was not a 9-patch it stretched to fill. Even though I changed the image to a 9-patch, it still treats the image as a normal .png file. Even has the black dots in the corner and everything.

enter image description here

I am wondering why the 9-patch-ness of the image isn't being respected and it's just being treated as a regular .png file.

Was it helpful?

Solution

I believe the issue is that you are using two dots on the bottom and right portions of the image. I'm not sure that they system knows how to handle disjoint areas on those two dimensions. You are, in essence, saying the 'content' portion of this image is made up of four one pixel regions which is the intersection of the four dots on the right and bottom borders.

If you take that out, I think it should stretch a lot more predictably for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top