Click Items By using Channel With AnnotationClass At Kotlin

Oguzhan Karakaya
IBTech
Published in
2 min readFeb 24, 2023

Hi Everyone , In this article, I’m gonna mention about the click structure that I often use at my personal project . When clicking in many places in Android like text, button ,images etc. may be want to call different functions and take different actions. Gathering these click operations will reduce the complexity of new click operations that can be add in the future and provide ease of management in clicks.

Let’s observe this with a simple instance :

→ At First, create Annotation Class . I will use two variable to click at this project . One of them for text click , the other one is for button click.

import androidx.annotation.IntDef

@Retention(AnnotationRetention.SOURCE)
@IntDef(ClickAnnotationClass.TEXT_CLICK, ClickAnnotationClass.BUTTON_CLICK)
annotation class ClickAnnotationClass {

companion object {
const val TEXT_CLICK = 0
const val BUTTON_CLICK = 1
}
}

→ After that , Channel created as flow . itemClicked function created for take id and update the clickedItem .

class ViewModel: ViewModel() {

private var _clickedItem = Channel<Int>()
val clickedItem = _clickedItem.receiveAsFlow()

fun itemClicked(id: Int) {
viewModelScope.launch {
_clickedItem.send(id)
}
}
}

→ Create a simple xml which include clickable text and button. itemClicked function is called from onClick .

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>

<import type="com.example.clickitemsbychannelwithannotationclass.ClickAnnotationClass" />

<variable
name="viewModel"
type="com.example.clickitemsbychannelwithannotationclass.viewModel.ViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/mainConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{() -> viewModel.itemClicked(ClickAnnotationClass.TEXT_CLICK)}"
android:text="@string/click_test"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/firstButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{() -> viewModel.itemClicked(ClickAnnotationClass.BUTTON_CLICK)}"
android:text="@string/click"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>

</layout>

→ clickedItem is collected at mainActivity.( clickedButton() function can call from OnCreate() ). In this way, when the button is clicked, buttonClick() function is called. textClick() function is also called when clicking on the text.

 private fun clickedButton() {
lifecycleScope.launchWhenStarted {
viewModel.clickedItem.collect { id ->
when(id) {
ClickAnnotationClass.BUTTON_CLICK -> buttonClick()
ClickAnnotationClass.TEXT_CLICK -> textClick()
}

}
}
}

private fun buttonClick() {
Toast.makeText(this, "Button Clicked ",Toast.LENGTH_SHORT).show()
}

private fun textClick() {
Toast.makeText(this, "Text Clicked ",Toast.LENGTH_SHORT).show()
}
Text Click and Button Click

Hope to see you next post .

--

--