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..
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.