Search code examples
androidbackgroundandroid-edittextandroid-linearlayoutandroid-relativelayout

Adding a background image to RelativeLayout hide all others elements


I have a simple XML:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity"
    android:background="@drawable/login_background">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="320dp"
        android:layout_height="160dp"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="218dp"
        android:layout_marginBottom="25dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/editText"
            android:textColor="@color/surtimax_gris_texto"
            android:layout_gravity="center_horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:textStyle="bold"
            android:hint="@string/login_edittext_user_text"
            android:textColorHint="@color/surtimax_gris_texto" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/editText2"
            android:background="@drawable/login_edittext_background"
            android:textColor="@color/surtimax_gris_texto"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="15dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:textStyle="bold"
            android:hint="@string/login_edittext_pass_text"
            android:textColorHint="@color/surtimax_gris_texto" />
    </LinearLayout>
</RelativeLayout>

The problem is: neither in emulator or real device is showing LinearLayout and EditText when RelativeLayout has a background.
If I just remove the background everything works fine... it's weird.

Not error logs about app.

minSdkVersion 17
targetSdkVersion 22

UPDATED Its working with normal background image (non 9patch). So the problem is about background 9patch image.

UPDATED 2 There is a lot of questions/answers about using 9patch in background in SO, but because the problem I faced was applying the background I started to looking in google for "background image problems". Of course none of the result in google throw me any useful answer. When Kai gave me and advice about the problem I easily found the problem. So I'm not going to close the question because it can help to someone to go through.


Solution

  • The problem is that your background is set with a 9patch image that doesn't define both the stretch area (left and top corners) and the drawable area (right and bottom corners).

    The solution is either modify 9patch image to include the necessary information, or set padding to 0 (android:padding="0dip" in XML or setPadding(0) in code).