buttonkotlinbackgroundfragmentsetbackground

Change Button Background in a Fragment with Kotlin


I'm totally new in kotlin so I don't know how many things I'm doing wrong. I'm trying to set the button background of a fragment from code, but when I start the application in my phone, there is just the background and an empty button

class start : Fragment() {


    override fun onCreateView(Layoutinflater: LayoutInflater,
                              container: ViewGroup?, savedInstantState: Bundle?):View? {
        return Layoutinflater.inflate(R.layout.fragment_start, container, false)

        val button_start = view!!.findViewById<Button>(R.id.button_start_1_id)

        button_start.setBackgroundResource(R.drawable.button_start1)


    }


}

And

<ImageButton

    android:id="@+id/button_start_1_id"
    android:layout_width="124dp"
    android:layout_height="69dp"
    android:onClick="button_start_pushed">

</ImageButton>

I'm also new in this forum so if a doing something wrong please let me know


Solution

  • Welcome to stackoverflow.

    Answer to your question is a simple one. First of all you do not explicitly need to get reference to your view using the following code, and it is redundant.

            val button_start = view!!.findViewById<Button>(R.id.button_start_1_id)
    

    You can easily get the reference to your view / imageview using view's id. In your case, and you can just call background property in your view class and set the background using resource.getDrawable(R.drawable.button_start1)

    If your API level 16 or above, you can use the following code.

    class ImageFragment : Fragment(){
    
    override fun onCreateView(Layoutinflater: LayoutInflater,
                              container: ViewGroup?, savedInstantState: Bundle?): View? {
        return Layoutinflater.inflate(R.layout.fragment_start, container, false)
    
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        button_start.background = resources.getDrawable(R.drawable.button_start1)
        }
    }
    

    resources.getDrawable gives deprecation warning in Kotlin because it requires Resource.Theme as second parameter. If you have Resource.Theme, give it as a second parameter. And your layout would be the following:

    <ImageButton
    
        android:id="@+id/button_start"
        android:layout_width="124dp"
        android:layout_height="69dp"
        android:onClick="button_start_pushed">
    
    </ImageButton>
    

    If you are using API level below 16, then you could use the following code:

    class ImageFragment : Fragment(){
    
    override fun onCreateView(Layoutinflater: LayoutInflater,
                              container: ViewGroup?, savedInstantState: Bundle?): View? {
        return Layoutinflater.inflate(R.layout.fragment_start, container, false)
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        button_start.setImageResource(R.drawable.button_start1)
    }
    }
    

    Just make sure you have button_start1 icon or img in your drawable, and your layout name is correct.

    Another things to note: it would be a lot easier for you to have your own consistent naming convention, or at least follow the recommended one by Kotlin or Java.

    I hope it helps you!