1. Are you ready for the Galaxy S20? Here is everything we know so far!

How can i set a padding for a Button element that uses android:background+app quits instantly

Discussion in 'Android Development' started by workbench, Mar 16, 2020.

?

How can i set a padding for a Button element that uses android:background+app quits instantly

  1. Solution to my problem.

    0 vote(s)
    0.0%
  2. Hints for the solution.

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. workbench

    workbench Lurker
    Thread Starter

    Hello to everyone in this forum,
    i'm new to android-development and i have a question:

    i'm trying to create a rounded button,i found a tutorial that descibles the android:background attribute for the Button element for the layout files. So i created the buttonshape.xml in the drawables folder and have the following layout file:
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:layout_width="match_parent"
    5.     android:layout_height="match_parent"
    6.     android:orientation="vertical">
    7.  
    8.     <ScrollView
    9.         android:layout_width="match_parent"
    10.         android:layout_height="match_parent">
    11.  
    12.     <LinearLayout
    13.         android:orientation="horizontal"
    14.         android:layout_width="match_parent"
    15.         android:layout_height="wrap_content"
    16.         android:padding="5dp">
    17.  
    18.         <Button
    19.             android:id="@+id/date_from_button"
    20.             android:layout_width="wrap_content"
    21.             android:layout_height="wrap_content"
    22.             android:layout_weight="1"
    23.             android:onClick="onButtonDateFrom"
    24.             android:background="@drawable/buttonshape"
    25.             android:text="@string/datum_from_button" />
    26.  
    27.         <Button
    28.             android:id="@+id/date_to_button"
    29.             android:layout_width="wrap_content"
    30.             android:layout_height="wrap_content"
    31.             android:layout_weight="1"
    32.             android:onClick="onButtonDateTo"
    33.             android:text="@string/datum_to_button" />
    34.  
    35.  
    36.     </LinearLayout>
    37.  
    38.     <DatePicker
    39.         android:layout_gravity="center"
    40.         android:layout_width="wrap_content"
    41.         android:layout_height="wrap_content"
    42.         android:visibility="gone"/>
    43.  
    44.     <LinearLayout
    45.         android:orientation="horizontal"
    46.         android:layout_width="match_parent"
    47.         android:layout_height="wrap_content">
    48.  
    49.         <Button
    50.             android:id="@+id/time_from_button"
    51.             android:layout_width="wrap_content"
    52.             android:layout_height="wrap_content"
    53.             android:layout_weight="1"
    54.             android:onClick="onButtonTimeFrom"
    55.             android:text="@string/time_from_button" />
    56.  
    57.         <Button
    58.             android:id="@+id/time_to_button"
    59.             android:layout_width="wrap_content"
    60.             android:layout_height="wrap_content"
    61.             android:layout_weight="1"
    62.             android:onClick="onButtonTimeTo"
    63.             android:text="@string/time_to_button" />
    64.  
    65.  
    66.     </LinearLayout>
    67.  
    68.     <DatePicker
    69.         android:layout_gravity="center"
    70.         android:layout_width="wrap_content"
    71.         android:layout_height="wrap_content"
    72.         android:visibility="gone"/>
    73.  
    74.     <FrameLayout
    75.         android:layout_width="match_parent"
    76.         android:layout_height="match_parent">
    77.  
    78.         <Button
    79.             android:layout_width="wrap_content"
    80.             android:layout_height="wrap_content"
    81.             android:text="@string/search_start"
    82.             android:layout_gravity="center_horizontal"/>
    83.  
    84.         <Button
    85.             android:layout_width="match_parent"
    86.             android:layout_height="wrap_content"
    87.             android:text="@string/search_start"/>
    88.  
    89.     </FrameLayout>
    90. </ScrollView>
    91. </LinearLayout>
    92.  
    Now my problem is that i'm not able to set a padding for the Button, no matter which attribute i set i get no padding and the app stops directly after starting it. Here is the code of the drawable/buttonshape.xml file:

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    4.     <corners
    5.         android:radius="23dp"
    6.         />
    7.     <solid
    8.         android:color="#FF0000"
    9.         />
    10.     <stroke
    11.         android:width="2dp"
    12.         android:color="#878787"
    13.         />
    14.  
    15.     <padding
    16.         android:left="5dp"
    17.         android:top="5dp"
    18.         android:right="5dp"
    19.         android:bottom="5dp"
    20.         />
    21. </shape>
    22.  
    I don't understand what i'm doing wrong so the app isn't even starting... but i get no errors when building it... please help a lost soul.

    best regards,
    workbench
     


Loading...
Similar Threads - set padding Button
  1. greg8334
    Replies:
    7
    Views:
    86
  2. AndroidGUIIsComplicated
    Replies:
    1
    Views:
    335
  3. MoodyBlues
    Replies:
    17
    Views:
    1,051
  4. Petecc
    Replies:
    0
    Views:
    120
  5. Charles Lee
    Replies:
    5
    Views:
    401
  6. ETtrailrunner
    Replies:
    7
    Views:
    465
  7. dvhttn
    Replies:
    2
    Views:
    286
  8. Steve Erlich
    Replies:
    3
    Views:
    233
  9. UndefinedN00B
    Replies:
    7
    Views:
    530
  10. masplin
    Replies:
    10
    Views:
    442

Share This Page

Loading...