2
votes

I'm trying to create a button with a radial gradient, but everytime I load my app, it crashes.

mylayout.xml:

...

<Button
    android:id="@+id/mybutton"
    android:layout_width="@dimen/btnwidth"
    android:layout_height="@dimen/btnheight"
    android:background="@drawable/mybutton" />

...

mybutton.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">

            <gradient
                android:type="radial"
                android:startColor="@color/my_start_color"
                android:endColor="@color/my_end_color"/>
        </shape>
    </item>
</layer-list>

Below is the error log. I'm targeting Android SDK 22.

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.MyActivity}: android.view.InflateException: Binary XML file line #324: Error inflating class Button at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) at android.app.ActivityThread.access$800(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: android.view.InflateException: Binary XML file line #324: Error inflating class Button at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at com.my.app.ui.MyFragment.onCreateView(MyFragment.java:183) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:924) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1116) at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1218) at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2170) at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300) at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(AppCompatDelegateImplV7.java:816) at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:72) at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:804) at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at android.view.LayoutInflater.inflate(LayoutInflater.java:365) at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106) at com.my.app.MyActivity.onCreate(MyActivity.java:1033) at android.app.Activity.performCreate(Activity.java:6010) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292) ... 10 more Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/mybutton.xml from drawable resource ID #0x7f02008a at android.content.res.Resources.loadDrawableForCookie(Resources.java:2482) at android.content.res.Resources.loadDrawable(Resources.java:2384) at android.content.res.TypedArray.getDrawable(TypedArray.java:749) at android.view.View.(View.java:3742) at android.widget.TextView.(TextView.java:637) at android.widget.Button.(Button.java:111) at android.widget.Button.(Button.java:107) at android.support.v7.widget.AppCompatButton.(AppCompatButton.java:60) at android.support.v7.widget.AppCompatButton.(AppCompatButton.java:56) at android.support.v7.internal.app.AppCompatViewInflater.createView

2

2 Answers

7
votes

You need a gradient radius:

<gradient
    android:type="radial"
    android:gradientRadius="40"
    android:startColor="@color/my_start_color"
    android:endColor="@color/my_end_color"/>
0
votes

You can try to use this:

Define bg_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/mybutton" android:state_pressed="true" />
    <item android:drawable="@drawable/mybutton" />
</selector>

There you can also define a pressed state. That makes sense when you are using a button.

And this would be your mybutton.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <gradient
        android:type="radial"
        android:startColor="@color/my_start_color"
        android:endColor="@color/my_end_color"/>
</shape>

And at the end you have to set the bg_button to the background of your button:

<Button
    android:id="@+id/mybutton"
    android:layout_width="@dimen/btnwidth"
    android:layout_height="@dimen/btnheight"
    android:background="@drawable/bg_button" />