# Function “onButtonClicked” is never use

**URL:** <https://discuss.kotlinlang.org/t/function-onbuttonclicked-is-never-use/18421>\
**Category:** Android\
**Created:** [July 11, 2020, 1:17pm UTC](https://discuss.kotlinlang.org/t/function-onbuttonclicked-is-never-use/18421 "2020-07-11T13:17:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgamarra](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@jgamarra](https://discuss.kotlinlang.org/u/jgamarra)\
**Post date:** [July 11, 2020, 1:17pm UTC](https://discuss.kotlinlang.org/t/function-onbuttonclicked-is-never-use/18421/1 "2020-07-11T13:17:54Z")

</div>

I have created a function called “onButtonClicked” and I don’t want to use a setOnClickListener because I am practicing with different views

the problem is when I create the onButtonClicked function, I see this warning

`(the “onButtonClicked” function is never used)

My code:

XML

```auto
 <Button
            android:id="@+id/bt301_tw"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:background="?android:attr/colorActivatedHighlight"
            android:onClick="onButtonClicked"
            android:text="@string/rodiogroup_301" />

```

```auto
Mainactivity

fun onButtonClicked(view: View) {
        if (view.id == R.id.bt301_tw) {
            val intent = Intent(this, MainActivity_04_ReadioGroup::class.java)
            startActivity(intent) 
       }
}

```

---

<div class="post-metadata">

**Author:** ![eugene.rubilov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/eugene.rubilov/32/7745_2.png) [@eugene.rubilov](https://discuss.kotlinlang.org/u/eugene.rubilov)\
**Post date:** [July 14, 2020, 9:52am UTC](https://discuss.kotlinlang.org/t/function-onbuttonclicked-is-never-use/18421/2 "2020-07-14T09:52:31Z")

</div>

Hey @jgamarra!  
In case of work with different views you can set custom click listener using setOnClickListener() without using XML onClick method

> ```
> val button1: Button = findViewById(R.id.navigation_home)
> val button2: Button = findViewById(R.id.navigation_dashboard)
>         
> val clickListener: View.OnClickListener = View.OnClickListener { 
> if (it.id == R.id.navigation_home){
> //todo so
> }
> else if (it.id == R.id.navigation_dashboard){
> //todo something new
> }
> }
>         
> button1.setOnClickListener(clickListener)
> button2.setOnClickListener(clickListener)
> 
> ```
