1

OK, I have looking and searched seemingly everywhere, and cannot find an answer to this. I think it's because I'm trying to do too much at once. I want to use the background image of the rectangle with a bitmap multiple times, with different text from the TextView.

ideal This is an example of the bitmap (.png) file I've been trying to use: dice_one

I want a TextView with a background of a drawable XML file that is a layer-list. See code:

box_ones xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:width="128dp" android:height="34dp">
            <shape android:shape="rectangle">
                <stroke android:width="1dp"/>
                <solid android:color="@color/ivory"/>
                <corners android:topLeftRadius="16dp" android:topRightRadius="16dp"/>
            </shape>
        </item>
        <item android:right="10dp">
            <bitmap android:src="@drawable/dice_one" android:gravity="right|center_vertical"/>
        </item>
    </layer-list>

box_twos xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:width="@string/tBoxWidth" android:height="@string/boxHeight">
            <shape android:shape="rectangle">
                <stroke android:width="1dp"/>
                <solid android:color="@color/ivory"/>
            </shape>
        </item>

<item android:right="10dp">
    <bitmap android:src="@drawable/dice_two" android:gravity="right|center_vertical"/>
</item>

activity_main xml:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ones"
        android:background="@drawable/box_ones"
        android:textAlignment="gravity"
        android:gravity="center_vertical"
        android:textSize="16sp"
        />

Strings xml

<resources>
    <string name="xSize">16dp</string>
    <string name="boxWidth">40dp</string>
    <string name="boxHeight">34dp</string>
    <string name="tBoxWidth">128dp</string>
</resources>

I have tried the above; I've tried doing a custom class that extends TextView; I've tried doing a TextView and and ImageView; I've tried doing android:drawableEnd. But, each method comes up with some error that prevents me from using said method. What am I doing wrong??

2019-01-25 14:24:29.978 4346-4346/com.dyna.ks.yscorepadv E/AndroidRuntime: FATAL EXCEPTION: main Process: com.dyna.ks.yscorepadv, PID: 4346 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dyna.ks.yscorepadv/com.dyna.ks.yscorepadv.FullscreenActivity}: android.view.InflateException: Binary XML file line #47: Binary XML file line #47: Error inflating class TextView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6938) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) Caused by: android.view.InflateException: Binary XML file line #47: Binary XML file line #47: Error inflating class TextView Caused by: android.view.InflateException: Binary XML file line #47: Error inflating class TextView Caused by: android.content.res.Resources$NotFoundException: Drawable com.dyna.ks.yscorepadv:drawable/box_twos with resource ID #0x7f06005b Caused by: android.content.res.Resources$NotFoundException: File res/drawable/box_twos.xml from drawable resource ID #0x7f06005b at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:837) at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:659) at android.content.res.Resources.loadDrawable(Resources.java:900) at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:953) at android.content.res.TypedArray.getDrawable(TypedArray.java:928) at android.widget.TextView.<init>(TextView.java:1391) at android.widget.TextView.<init>(TextView.java:1129) at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:87) at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:83) at androidx.appcompat.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:177) at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102) at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266) at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:772) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at com.dyna.ks.yscorepadv.FullscreenActivity.onCreate(FullscreenActivity.java:13) at android.app.Activity.performCreate(Activity.java:7183) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6938) at java.lang.reflect.Method.invoke(Native Method) 2019-01-25 14:24:29.979 4346-4346/com.dyna.ks.yscorepadv E/AndroidRuntime: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 2 to dimension: type=0x3 at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:730) at android.graphics.drawable.LayerDrawable.updateLayerFromTypedArray(LayerDrawable.java:376) at android.graphics.drawable.LayerDrawable.inflateLayers(LayerDrawable.java:266) at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:194) at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:146) at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1322) at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1281) at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:824) ... 36 more

Kenyx
  • 129
  • 1
  • 1
  • 7
  • The stack trace says that there's a problem with `box_twos`. It looks like you've posted `box_ones`. – Mike M. Jan 25 '19 at 20:26
  • I just did that because it's supposed to be box_ones. I did an edit after I posted the question. Sorry! – Kenyx Jan 25 '19 at 20:27
  • box_twos is exactly the same as box_ones, just with `dice_two` instead of `dice_one` – Kenyx Jan 25 '19 at 20:28
  • Are you sure it's _exactly_ the same? I just threw that into a test project, and it works just fine for me: https://drive.google.com/file/d/1V3Z7vO1Rq-PtcfJU5L8R75FnUmM14g1v/view?usp=drivesdk. The stack trace says one of the dimension measures is wrong in `box_twos`. – Mike M. Jan 25 '19 at 20:32
  • Now, I have a Custom Class that's extending `TextView`, but I haven't used it at all. It's just there for when I need it. – Kenyx Jan 25 '19 at 20:32
  • I get the same error if I use `@drawable/box_twos` as the background. – Kenyx Jan 25 '19 at 20:34
  • Yes, that stack trace is saying there's something wrong in `box_twos`. It doesn't say anything about `box_ones`, which is what you've posted. – Mike M. Jan 25 '19 at 20:37
  • I posted box_twos, and my strings.xml for you two see. – Kenyx Jan 25 '19 at 20:41
  • That is not exactly the same as `box_ones` – `android:width="@string/tBoxWidth" android:height="@string/boxHeight"`. You cannot use strings for dimensions. – Mike M. Jan 25 '19 at 20:42
  • @MikeM. And YES, you can use strings for dimensions. I do it all the time and never have a problem. – Kenyx Jan 25 '19 at 21:00

1 Answers1

-1

Apparently, it an SDK issue. I had my minimum SDK at 19. In order to use android:width and android:height in the drawable, my minimum had to be 23. See image:

enter image description here

Mike M.
  • 38,532
  • 8
  • 99
  • 95
Kenyx
  • 129
  • 1
  • 1
  • 7
  • Yeah, this didn't have anything to do with. That's just a warning saying those attributes won't be applied on older versions. If you read your stack trace, it's saying exactly as I said in my comment above; you cannot use string resources there – `Can't convert value at index 2 to dimension: type=0x3`. Type `0x3` is a string resource. – Mike M. Jan 25 '19 at 22:08