Search code examples
androidnine-patch

Issue with nine-patch background on specific device


I set the 9-patch image test.9.png shown below as the background of a view, and reset the padding to 0 afterwards.

The result is symetric, as expected, on my Nexus 4 with Android 4.4.4, but on my Huawei G6-L11 with 4.3 I get the result shown below, i.e. the image does not extends horizontally in a symetric way.

Am I doing it wrong or is it a bug?

test.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

TestFragment.java

public class TestFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                 Bundle savedInstanceState) {

        return inflater.inflate(R.layout.test, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.setBackgroundResource(R.drawable.test);
        view.setPadding(0,0,0,0);
    }
}

test.9.png ( 6x4 pixels )

enter image description here

Expected result ( Nexus 4, Android 4.4.4 )

enter image description here

Wrong result ( Huawei G6-L11, Android 4.3 )

enter image description here


Solution

  • add your test.9.png to res/drawable-nodpi folder