2

I'm making a project that will dynamically add Views (RelativeLayouts) to a GridLayout. The only problem is that I need to get the row and column of that View I have clicked on to dynamically add or remove a View. I know that in Javascript and html I can add a data attribute. Is there something like that for android?

If there are also other ways of doing it, I will appreciate the help

Zain
  • 37,492
  • 7
  • 60
  • 84

1 Answers1

0

You can set a tag to a view with .setTag() method, and retrieve that with .getTag().

You need to cast that tag when you use getTag() to the data type you put onto it with setTag().

This is the a very useful question that might also help you.

Zain
  • 37,492
  • 7
  • 60
  • 84
  • Yeah I have tried using that. I have 2 options ore link an id to an object in the setter or only give an Object. but i don't really know how to make an id. – SmeldorTheEmperor Apr 18 '20 at 21:25
  • @SmeldorTheEmperor you can use [`setId()`](https://stackoverflow.com/questions/1714297/android-view-setidint-id-programmatically-how-to-avoid-id-conflicts) to set an id to a view – Zain Apr 18 '20 at 21:26
  • But doesn't setTag requests a new id? – SmeldorTheEmperor Apr 18 '20 at 21:29
  • @SmeldorTheEmperor id is something different than a tag, id is unique in your layout, but tag can be any data you need to associate to your View – Zain Apr 18 '20 at 21:34
  • So if i get it right. I can add a tag to a View. i can specify and id to the setter. but that id is of a View. but does that id is connected to a specific View or is is like a key to retrieve the value of the tag? – SmeldorTheEmperor Apr 18 '20 at 21:38
  • @SmeldorTheEmperor yes the Id identifies a view in a layout, you cant have 2 views in your xml layout that share the same id; but you can have 2 views that have the same tag; the id must be an integer, but the tag can be anything that you want to attach to a view.. also Id can be used to position your views within the layout, so you can specify how views can position related to each other and to the parent using ids, not tags – Zain Apr 18 '20 at 21:44
  • @SmeldorTheEmperor the id is like a key that you can identify the view, and then you can get the view tag once the view is identified by its id – Zain Apr 18 '20 at 21:47
  • But in de documentation the setTag(int id, Object obj) the parameter id in this tag is of a child or the parent itself. Its not possible to add 2 tags like x and y. so i need to create an object to store it on a view? – SmeldorTheEmperor Apr 18 '20 at 21:59
  • @SmeldorTheEmperor yes exactly, you can save any Object into a View; Object is the the root of the class hierarchy in java; any class in java must inherit directly on indirectly from Object class; that is why you can attach a tag (of any type) to a View; because any type must be a child of Object – Zain Apr 18 '20 at 22:02
  • 1
    Thanks a lot. I just tried it oud and it works like it should :). Thanks a lot ;) – SmeldorTheEmperor Apr 18 '20 at 22:05