• 0

[Java / Android] OnClick event, get the sender ID / tag


Question

First of all, my Java is fairly rusty (just started using again after a 6 year hiatus). I am working on this for Android 2.1

Goal:

I have multiple buttons, they all perform the same task, but with different data pulled from an array. For example, button1, button2, button3. I want to use the same onClick for all of them so they share the same code base. if button1 is clicked, text1 is updated, etc

The problem:

I can't figure out how to determine which of the buttons triggered the onClick event. I have seen references to getTag, and getId, but when I check what those contain while debugging, its always empty.

Is there a way to either get the button name in onClick, or maybe a way to pass it another parameter so I can just tell it to pass the ID when I set up the onClick listener?

Thanks!

8 answers to this question

Recommended Posts

  • 0
  On 28/04/2010 at 03:13, dontocsata said:

I believe that View passed to the onClick is the View which generated the click.

I've poked through the view that was passed, but couldn't find the button name in it, unless it's buried in some odd place?

  On 28/04/2010 at 12:28, El Marto said:

iv only ever written one game for android and it was a while agoo. dont you declare the id for each button in the xml file? may be wrong here

Yes you do, and that is what I am trying to find when onClick is triggered. Like dontocsata mentioned, I would think it would be somewhere in the view that was passed in, but if it is, I can't find it

  • 0

No, the View passed in is the Button, I believe. Try this.

so you have... (it changed the case of the objects/methods, but you get the idea)

Button button = //get via R.id, or whatever
Button button2 = //get via R.id, or whatever

OnClickListener myListener = new OnClickListener(){
	public void onClick(View v){
		if(v==button){
			//do something
		}else if(v==button2){
			//do something different
		}
	}
}

button.setOnClickListener(myListener);
button2.setOnClickListener(myListener);

  • 0

I always use this technique:

firstly give your buttons an id. Then do something like this:

Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);

btn1.setOnClickListener(myListener);
btn2.setOnClickListener(myListener);

//myListener:
public void onClick(View v) {

     switch (v.getId()) {
            case R.id.button1:
                     btn1.setText("1 clicked!");
                     break;


            case R.id.button2:
                     btn1.setText("2 clicked!");
                     break;
      }
}

  • 0

That worked! I ended up using the switch that al1uk posted as the basis of it. I now see I was completely misunderstanding what the view object was when it was getting passed through and what the getid was doing.

Thanks again everyone! Extremely helpful :D

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • ISTM from the article that the user was just out to get the developer for one reason or another. It may have had nothing to do with the software but the user just wanted to harm the developer in one way or another for some personal reason. They used the the project's Codeberg page to accomplish their goal.
    • ""Settings vs. Control Panel" - this has absolutely been fixed." - This is absolutely not fixed. Your personal experience may vary but still there is a clear division between two. "Error 0x80070643" - this issue never existed for Windows 11: https://learn.microsoft.com/en...(install-error-0x80070643). You can find similar cases for Windows 11; this is just example. Though with Windows 11, the issue is not so common like Windows 10. "UX inconsistencies" - This is terrible. And not just UX but UI as well. Windows 11 may work but it's not a good product. Hundreds thousands of engineering hours, billions of USD... This might be the only choice by force due to the jettisoning of Windows 10, however the end product for all those resources spent is simply not good. The real problem is lack of competition. Linux derivatives and MacOS unfortunately are not competitive.
    • Grab Motorola's Razr Ultra 2025 at 15% off and ditch your slab phone by Paul Hill Motorola is currently selling the Razr Ultra 2025 on Amazon at a discount of 15%, meaning you can pick it up now for $1,099.99 instead of the usual $1,299.99. This is a notable saving on a premium foldable smartphone, but it still remains a high-end purchase. Any Prime members out there can get free overnight delivery and if you have an old device to trade in, then you can get up to $725 in Amazon.com Gift Card credit that you can use towards your upgrade. One of the main perks of this device is that it’s universally unlocked, meaning you can use it on all major US carriers. It’s powered by the Snapdragon 8 Elite and comes with 16GB RAM and 512GB storage. With these specs, this device should be able to run any apps you throw at it. The Pantone Scarab color, a soft, velvety suede-like texture, is the result of a collaboration with the Italian brand Alcantara, known for its premium materials, giving the phone a very unique look. As a flip phone, you also get an intelligent and interactive external display that lets you access apps without opening your phone. Even with this external display, Motorola says you can expect over 36 hours of battery life and when you do need to recharge, you have fast charging. The main display on this device is 7 inches and features a Super HD resolution which is 2992 x 1224, the refresh rate is 165Hz. The external display has a resolution of 1272 x 1080. This phone could be a good pick for fashion-conscious users with its unique materials. It’s also a great device for anyone after a compact form factor or who is a fan of Motorola devices. The style, compact design, external display, fast performance, and improved camera system (50MP) are all positives on this device. There’s also IP48 rating for dust and water protection, adding durability. Motorola Razr Ultra 2025: $1,099.99 (Amazon US) / MSRP $1,299.99 This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
  • Recent Achievements

    • One Month Later
      BA the Curmudgeon earned a badge
      One Month Later
    • First Post
      Doreen768 earned a badge
      First Post
    • One Month Later
      James_kobe earned a badge
      One Month Later
    • Week One Done
      James_kobe earned a badge
      Week One Done
    • Week One Done
      macomen earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      653
    2. 2
      ATLien_0
      253
    3. 3
      Xenon
      168
    4. 4
      neufuse
      147
    5. 5
      +FloatingFatMan
      125
  • Tell a friend

    Love Neowin? Tell a friend!