How to get the id of an ImageButton?

I'm developing a solution that passes the one activity for other activity after the user clicks in an ImageButton. I have 6 images and all images are having the same onClick event and I need to distinguish different ImageButton's clicks which is which for passing for the second activity. I tried the solution below, but the line with idImageButton.tag.toString() doesn't work.

file.kt

fun onclickImage(view: View){
    val idImageButton:ImageButton = view as ImageButton
    val pokemonName:String = idImageButton.tag.toString()

    val myIntent = Intent(this, Details::class.java)
    myIntent.putExtra("pokemon", pokemonName)
    startActivity(myIntent)
}

You have to checked the id of the view to distinguish them. For Example if the id of the ImageButton for which you want to move to the second activity is pokemon, then try like this.

fun onclickImage(view: View) {

    if(view.id == R.id.pokemon) {
        val myIntent = Intent(this, Details::class.java)
        myIntent.putExtra("pokemon", "Pokemon")
        startActivity(myIntent)
    }
}

If you also want to do it by tag you have to set tag in either xml or code. Then you can check it with tag

<ImageButton
    android:id="@+id/pokemon"
    android:tag="Pokemon"
    android:onClick="onclickImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Then check tag and take decision.

fun onclickImage(view: View) {
    val pokemonName = view.tag.toString()

    if(pokemonName.equals("Pokemon", true)) {
        val myIntent = Intent(this, Details::class.java)
        myIntent.putExtra("pokemon", pokemonName)
        startActivity(myIntent)
    }
}

How to get ID of imagebutton when it is clicked. Ask Question. Asked 4 years, 3 months ago. Active 4 years, 3 months ago. Viewed 2k times. 1. public void onClick(View v) { int id = v.getId(); switch(id) { case R.id.a : String textans = ans.getText().toString(); ans.setText(textans +id); } }

You can get view id with casting to ImageButton

when(view.id) {
            R.id.btnFirst -> {}
            R.id.btnSecond -> {}
            //so on
        }

You can do it in few ways. If you have separate listener for each button, then you know the button and can get it using v.getId(). If you have one listener for all, then you can use switch statements to identify which one was clicked. See Android OnClickListener - identify a button. It explains for a button but its same for ImageButton as well.

 val button:ImageButton
       val id =  button.id

I want the user to flick through a series of images and when they click on an image a sound will play. I have an imagebutton on my layout that will change it's source when the user swipes. Each image has a corresponding sound. So, for example, when the user clicks an image of the letter B a sound will play that corresponds with that letter.

ID: It is a unique ID for identification purposes. ImageUrl: With the help of the image URL, we can set the image path for the image on the image button control. AlternateText: If the image is not be displayed on the web page, AlternateText will be displayed. PostBackUrl: When we click the image button, it will redirect the other page

In android, Image Button is a user interface control that is used to display a button with an image and to perform an action when a user clicks or taps on it. By default, the ImageButton looks same as normal button and it performs an action when a user clicks or touches it, but the only difference is we will add a custom image to the button instead of text.

Re: Get ID of clicked Image button Jun 23, 2014 08:26 AM | Rion Williams | LINK Could you post an example of what your source currently looks like as your jQuery selector will depend on if you are using an actual ImageButton or an Image.

Comments
  • log idImageButton.tag.toString() and see what it logs.