Android:
setTag() and getTag() usage for changing the controls..
When we are in a situation to dynamically change something on the controls(for example), we can use setTag() and getTag() to identify which one needs to be chosen.
For example:
When i was need to change the background image for the buttons dynamically one to another, i might need to use such techniques. Here is a example code below..
Concept: When "touching" on a button, button should change the current background image to next background image. When again clicking on same button, should change the background image to previous one.
That's it, you got go !!!
setTag() and getTag() usage for changing the controls..
When we are in a situation to dynamically change something on the controls(for example), we can use setTag() and getTag() to identify which one needs to be chosen.
For example:
When i was need to change the background image for the buttons dynamically one to another, i might need to use such techniques. Here is a example code below..
Concept: When "touching" on a button, button should change the current background image to next background image. When again clicking on same button, should change the background image to previous one.
ImageButton button1;ImageButton button2;private int firstImgId = 0;private int secondImgId = 0;
// setting up buttons background image..private void setBackButtonImage(){Log.d("mysample", "Set back button images");button1 = (ImageButton) findViewById(R.id.ImageButton01);button2 = (ImageButton) findViewById(R.id.ImageButton02);// set touch listener for buttonsbutton1.setOnTouchListener(this);button2.setOnTouchListener(this);
firstImgId = R.drawable.myFirstImage;secondImgId = R.drawable.mySecondImage;
// setting the proper images in the background.button1.setImageResource(firstImgId);button1.setTag("1");
button2.setImageResource(secondImgId);button2.setTag("2");}public boolean onTouch(View v, MotionEvent event){ImageButton imgBtn = (ImageButton) v;if ( imgBtn==button1 ) // first button touch{if ( button1.getTag()=="1" ){button1.setImageResource(R.drawable.myFirstImage);button1.setTag("2");}else{button1.setImageResource(R.drawable.mySecondImage);button1.setTag("1");}}else if ( imgBtn==button2 ) // second button touch{if ( button2.getTag()=="1" ){button2.setImageResource(R.drawable.myFirstImage);button2.setTag("2");}else{button2.setImageResource(R.drawable.mySecondImage);button2.setTag("1");}}
}
That's it, you got go !!!
Thank you for followers!
Cheers,
M.P.Prabakar
Senior Systems Analyst.
Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..
no offense but when I get to a site online that doesn't have formatted code i'm better off just not reading anything on the site. I'm pretty sure every else feels the same way..
ReplyDeleteSure, i'll correct it. Thank you!
ReplyDeletePrabaker --
ReplyDeleteThanks for the tutorial. It is very clear, so it's the best one I've found for getTag() and setTag() usage. My question:
You initialize the button images as follows:
---------------------------------------------------
// setting the proper images in the background.
button1.setImageResource(firstImgId);
button1.setTag("1");
button2.setImageResource(secondImgId);
button2.setTag("2");
---------------------------------------------------
However, within the onTouch function, if the button has a tag of "1," you set the image as R.drawable.myFirstImage (and then set the tag to "2"). If that's the case, shouldn't the image be set to R.drawable.mySecondImage instead, since that corresponds to a tag of "2"?
Just trying to help out with your tutorial -- like I said, it's the best one on this topic that I've found.
Take care,
-Lou
Time waste
ReplyDeletekeep up the good work...helping out people is the best work in the world...thank you
ReplyDeleteregards
Mr. Singh
How to swap images even if we want to keep tags intact?
ReplyDeletehello
ReplyDelete