Search code examples
android-studiokotlinbuttonborder

Android Studio 4.1 bordered Button loses border setting background color in kotlin code


There are numerous threads for creating a bordered button in this forum. It works for example perfect with this xml-file

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
        <solid android:color="#FFEB3B" />`<!--Background Color-->`
        <stroke android:width="2dip" android:color="#000000"/> `<!--Border-->`
    </shape>

where i fail is the following:

i need 2 bordered buttons, one is yellow, the other one blue. Due to program logic I want to switch the colors, the yellow one gets blue, the blue one to yellow.

in the xml-file

<solid android:color="#FFEB3B" />`<!--Background Color-->`

is used.

In Kotlin I only find

button.setBackgroundColor(Color.YELLOW)

But I find nor button.setColor oder button.setdologColor or anything else.

Using setBackgroundColor, the color changes, but the border disappears. Which statement would keep the border?


Solution

  • I solved it with

    punktelinks.setBackgroundResource(R.drawable.shapecolory)
    

    in code and with resource

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#FFFF00" />`<!--Background Color-->`
        <stroke android:width="2dip" android:color="#000000"/> `<!--Border-->`
    </shape>
    

    This works (for me)