Shape does not apply correctly

4

I want to put this shape on my button but it is not being applied correctly, since in the middle there is a blank rectangle that does not show the edges of the shape.

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff"/>

<stroke android:color="#aaaaa"
   android:width="10dp" />

<padding android:left="50dp"
    android:top="1dp"
    android:right="50dp"
    android:bottom="1dp"/>

<corners android:radius="50dp"/>
   </shape>


   <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="@string/search_group"
    android:background="@drawable/rounded_button"
    android:layout_below="@+id/message_part3"
      />

    
asked by Jorge Gonzalez 12.10.2016 в 16:44
source

3 answers

2

I explain to you what is the problem you are using the wrong color "#aaaaa" or you use a 3-digit notation or a 6-digit notation as you did with the white color, this causes the error you are seeing. if you use android studio make sure the color is visible in the guide column on the left. use the following code:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#ffffff"/>

    <stroke android:color="#aaaaaa"
        android:width="10dp" />

    <padding
        android:left="50dp"
        android:top="1dp"
        android:right="50dp"
        android:bottom="1dp"/>

    <corners android:radius="50dp"/>
</shape>

After you change it refreshes the view many times it takes time to refresh, I recommend you close it and open it again or use the refresh button in the android studio.

    
answered by 12.10.2016 / 19:28
source
2

Jorge, the problem with the shape is that you're defining in stroke an invalid color :

<stroke android:color="#aaaaa"

which causes this:

should be:

<stroke android:color="#aaaaaa"

This is the shape corrected:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ffffff"/>

    <stroke android:color="#aaaaaa"
        android:width="10dp" />

    <padding android:left="50dp"
        android:top="1dp"
        android:right="50dp"
        android:bottom="1dp"/>

    <corners android:radius="50dp"/>
</shape>

to get what you want:

Another option is to do this with a 9-patch image, which you would simply define as background :

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="@string/search_group"
    android:background="@drawable/ninepatchimage"
    android:layout_below="@+id/message_part3"
      />
    
answered by 12.10.2016 в 19:29
1

Are you sure there is nothing more code that affects that button? I have used your shape on the same button and it is shown correctly. This is the code that I used for the button:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.usuario.usuario.pruebashape.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="BUSCAR"
        android:background="@drawable/rounded_button"
        android:layout_below="@+id/message_part3"
        />
</RelativeLayout>

And this is the shape I used:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ffffff"/>

    <stroke android:color="#aaaaaa"
        android:width="10dp" />

    <padding android:left="50dp"
        android:top="1dp"
        android:right="50dp"
        android:bottom="1dp"/>

    <corners android:radius="50dp"/>
</shape>

The result has been this:

As you can see it is the same code as yours, instead of using a string for the name "SEARCH", I put it directly. So, I can think of two things that can be making your button look like a white rectangle:

  • That in your @string/search_group you are giving some additional character to the message of "SEARCH" such as a padding.

  • That you are modifying that button with code.

  • Please check that you are not modifying the button from any other site.

        
    answered by 12.10.2016 в 19:08