Android: Get the image source name of an ImageView to display on a TextView
Hi I am new into android development and I am having trouble into this one. I wanted to display the src name of my imageView
on a textView
when I click a certain Image.
To understand much further here is my hardcode for the image clickListener
(hope someone can also help me to improve my coding).
int ctr = 0;
@Override
public vo开发者_StackOverflow社区id onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
//DECLARE CLICK LISTENERS ON OBJECTS
ImageView ImageView1 = (ImageView)findViewById(R.id.imageView1);
ImageView1.setOnClickListener(this);
ImageView ImageView2 = (ImageView)findViewById(R.id.imageView2);
ImageView2.setOnClickListener(this);
...
public void onClick(View v) {
//CHANGE TEXT VIEW TEXT BASED ON THE CTR VALUE
TextView TextView1 =(TextView)findViewById(R.id.textView1);
//I WANT TO CHANGE THIS ONE TO DISPLAY SRC INSTEAD OF THE CTR VALUE
TextView1.setText(Integer.toString(ctr));
}
There, instead of displaying the ctr value I wanted to display the src name of whatever image I click between the two images I set. I believe it is possible. Thanks.
in xml you can add tag to set any information with image
Example
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageview1"
android:background="@drawable/bg"
android:tag="bg"/>
Dynamically you can use imageView.setTag("any name")
to set any data along with image and imageView.getTag()
to retrieve image information
String backgroundImageName = (String) imageView.getTag();
You can use below method for get name of resource from it's id:
/**
* Determines the Name of a Resource,
* by passing the <code>R.xyz.class</code> and
* the <code>resourceID</code> of the class to it.
* @param aClass : like <code>R.drawable.class</code>
* @param resourceID : like <code>R.drawable.icon</code>
* @throws IllegalArgumentException if field is not found.
* @throws NullPointerException if <code>aClass</code>-Parameter is null.
* <code>String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.icon);</code><br>
* Then <code>resName</code> would be '<b>icon</b>'.*/
public String getResourceNameFromClassByID(Class<?> aClass, int resourceID)
throws IllegalArgumentException{
/* Get all Fields from the class passed. */
Field[] drawableFields = aClass.getFields();
/* Loop through all Fields. */
for(Field f : drawableFields){
try {
/* All fields within the subclasses of R
* are Integers, so we need no type-check here. */
/* Compare to the resourceID we are searching. */
if (resourceID == f.getInt(null))
return f.getName(); // Return the name.
} catch (Exception e) {
e.printStackTrace();
}
}
/* Throw Exception if nothing was found*/
throw new IllegalArgumentException();
}
For Example of use :
String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.icon);
Result will be icon
try this
String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.imagename);
精彩评论