2

i'll try to be short.

This is a Pokemon App. I have a Linear Layout in which I add Pokemon Container elements dynamically and if the user press the Eye it will appear an image below the container. I want the eye to be a "toggle": If the user press it, the image will appear below the respective Pokemon Container, another click will cause the image to dissapear. So I need to associate a kind of ID for the Eye and the Pokemon Container in order to know what was clicked to remove the proper image. I can't use setId() because It will repeat for the eye icon and the container. I can't also set a String ID because is not possible in android..

Application Image HERE

In the XML I can set an id to that Eye like "eye_1", "eye_2", etc, and the Pokemon Container to "container_1", "container_2", etc. So inside the onClick() I could retrieve the number of the eye icon being pressed and show/remove the image.

I have no idea how to do this on android. I could do it on html getting elements by ID since Id can be any text but how in earth do I achieve this?

This will be much appreciated.

The XML in which I add dynamic data is this:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/data_section"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="vertical">

        <!-- Data will be filled dynamically from the Database-->

    </LinearLayout>
</ScrollView>

I've just saw the answer below and I want to edit with a solution I've found myself. I don't know if this is the right place but here I go. The appDetailsIcon is what I i've called Eye and layoutPokemonContainer is the Pokemon Container. What I basically do is set the image if is not created and then I remove using the index.. I know there are 3 elements before the image.. it's kinda dirty but I've read that tag shouldn't be used to find views on android =/.

ImageView appDetailsIcon = new ImageView(this);
                appDetailsIcon.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.icon_details_small));
appDetailsIcon.setOnClickListener(new View.OnClickListener(){
  public void onClick(View v){
    ImageView iv = (ImageView) findViewById(pokemon.getId());
    if(iv == null){//If the image doesn't exist I create it
    ImageView matchupDetail = new ImageView(v.getContext());

matchupDetail.setImageDrawable(ContextCompat.getDrawable(v.getContext(),R.drawable.matchup_bug));
matchupDetail.setId(pokemon.getId());
View v1 = (View) v.getParent();
    //I get the partent of the button (Pokemon layoutStatsContainer row)
    LinearLayout v2 = (LinearLayout) v1.getParent();
    //I get the parent of the statsContainer row (layoutPokemonContainer)
    v2.addView(matchupDetail);
    }else{//I remove the image from the view
    View v1 = (View) v.getParent();//I get the partent of the button (Pokemon layoutStatsContainer row)
    LinearLayout v2 = (LinearLayout) v1.getParent();//I get the parent of the statsContainer row (layoutPokemonContainer)
    v2.removeViewAt(3);
    }
  }
});

I really don't know how to use the editor is driving me nuts.. I can't get it to work properly.

Fragsman
  • 79
  • 6

1 Answers1

0

You can set a tag on view objects:

view.setTag("eye_1");

E.g. you can get it back in OnClickListener:

public void onClick(View v) {
    ...
    String myTag = (String) v.getTag() // getTag returns "Object"
    doYourLogic(myTag);
    ...
}

From java doc (View.java):

A tag can be used to mark a view in its hierarchy and does not have to be unique within the hierarchy.

What is the main purpose of setTag() getTag() methods of View?

BenRoob
  • 1,662
  • 5
  • 22
  • 24
  • I've found a solution, but if yours is better I'll use it and mark as valid but v.getTag() doens't accept string, only int. And I cannot create an XML with tags since the idea would be having dynamic elements associated (or tagged) this way "eye_"+String.valueOf(pokemon.getId()) – Fragsman Jun 21 '17 at 16:16
  • Oh man, of course View.getTag() is without String parameter!! And it is not an xml attribute. I'll edit my answer. FYI, e.g. tags are usually used in the ViewHolder-pattern (with ListViews), and are also the recommended way in developer's guide: https://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder I think tags are what you need ;) – BenRoob Jun 22 '17 at 06:46
  • You can even the your views with your pokemon objects :) – BenRoob Jun 22 '17 at 13:10