임세준
임세준

Reputation: 154

android button and listener. how can i get it more short

I am making some buttons and listener for it. but i think it is too long and complicated. So can i get it more briefly?

here is my code

fun init_cardView() {
    activity_cardview.setOnClickListener(myOnClickListener)
    alertdialog_cardview.setOnClickListener(myOnClickListener)
    animation_cardview.setOnClickListener(myOnClickListener)
    widgets_cardview.setOnClickListener(myOnClickListener)
    container_cardview.setOnClickListener(myOnClickListener)
    date_cardview.setOnClickListener(myOnClickListener)
    explicitintent_cardview.setOnClickListener(myOnClickListener)
    implicitintent_cardview.setOnClickListener(myOnClickListener)
    fragment_cardview.setOnClickListener(myOnClickListener)
    wifimanager_cardview.setOnClickListener(myOnClickListener)
    menu_cardview.setOnClickListener(myOnClickListener)
    image_cardview.setOnClickListener(myOnClickListener)
    time_cardview.setOnClickListener(myOnClickListener)
    toast_cardview.setOnClickListener(myOnClickListener)
}

fun init_MyOnClickListener() {
    myOnClickListener = View.OnClickListener {
        var intent: Intent? = null
        when (it.id) {
            R.id.activity_cardview -> intent = Intent(applicationContext, Activity::class.java)
            R.id.alertdialog_cardview -> intent =
                Intent(applicationContext, AlertDialog::class.java)
            R.id.animation_cardview -> intent =
                Intent(applicationContext, Animation::class.java)
            R.id.widgets_cardview -> intent = Intent(applicationContext, Widgets::class.java)
            R.id.container_cardview -> intent =
                Intent(applicationContext, Container::class.java)
            R.id.date_cardview -> intent = Intent(applicationContext, Date::class.java)
            R.id.explicitintent_cardview -> intent =
                Intent(applicationContext, ExplicitIntent::class.java)
            R.id.implicitintent_cardview -> intent =
                Intent(applicationContext, ImplicitIntent::class.java)
            R.id.fragment_cardview -> intent = Intent(applicationContext, Fragment::class.java)
            R.id.wifimanager_cardview -> intent =
                Intent(applicationContext, WifiManager::class.java)
            R.id.menu_cardview -> intent = Intent(applicationContext, Menu::class.java)
            R.id.image_cardview -> intent = Intent(applicationContext, Image::class.java)
            R.id.time_cardview -> intent = Intent(applicationContext, Time::class.java)
            R.id.toast_cardview -> intent = Intent(applicationContext, Toast::class.java)
            else -> {
                intent = null
            }
        }
        if (intent != null) {
            Log.d("myOnclickListener", "${it.id}");
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
            startActivity(intent)
        }
    }
}

Thank you!

Upvotes: 1

Views: 60

Answers (2)

Reza
Reza

Reputation: 875

So simple. Just define a method for example onCustomClick in your activity which gets a view as parameter and write whatever you want to do in it. then define the android:onClick="onCustomClick" attribute in your xml file for any button you want them to use onCustomClick method
add this to your activity

fun onCustomClick(v: View) {
        when (v.id) {
            R.id.button1 -> Log.i("TAG", "button 1 clicked")
            R.id.button2 -> Log.i("TAG", "button 2 clicked")
            R.id.button3 -> Log.i("TAG", "button 3 clicked")
        }
    }


and add this attribute to your buttons in xml file

android:onClick="onCustomClick"

Upvotes: 3

Ryan M
Ryan M

Reputation: 20137

You could remove init_MyOnClickListener and just initialize it at the definition (wherever that may be). Other than that, I don't think you could make it any shorter without sacrificing readability.

Upvotes: 1

Related Questions