Search code examples
androidandroid-layoutandroid-imageviewandroid-drawable

How to get the Drawable name from imageview in android and get it's String name for comparing


I have two drawable apple and a how to get the drawable name in my code and compare that they starts with same alphabet

Community edited question

Questioner is actually seeking for 2 problems-

  1. at first he wants to get the drawable id from the imageview id. (thw drawable which is showing in the imageview)

  2. then he wants to get the drawable name from that obtained drawable id in step-1.


Solution

  • Updated answer

    For step-1

    There is no direct way. You need to save the drawable id in the imageview's Tag via setTag(). then you can retrieve by getTag() of the imageview.

    Step-2

    you should get the image name by

    String imageName1 = context.getResources().getResourceEntryName(R.drawable.apple);
    // now imageName1 should contain "apple".
    
    String imageName2 = context.getResources().getResourceEntryName(R.drawable.a);
    // now imageName2 should contain "a".
    
    if(imageName1.toLowerCase().charAt(0) == imageName2.toLowerCase().charAt(0))
    
    {
    
      // starting character 'a' is matching
    
    }