get id of view Android code example

Example 1: android studio get id name from view

//Don't get the ID name/string from the view ID.

//Instead use the 
android:tag = "String"
//in your xml 

//Then in Java use 
View.getTag()
  
//To still get the ID name/string from view ID.
getResources().getResourceEntryName(View.getId())

Example 2: android get id of view

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.add_04:
        Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
        break;
    case R.id.add_05:
        Toast.makeText(MainActivity.this, "2", Toast.LENGTH_LONG).show();
        break;
    }
}

Tags:

Java Example