Easier way to get view's Id (string) by its Id (int)
The approach is misguided to begin with. If you want to associate a piece of arbitrary data (e. g. a string) with a view, that's what tag
is for. The ID is numeric and it better stay that way. A word of caution though, tags are not unique in Android, watch for accidental tag collisions within the same view tree.
EDIT much later: the OP's issue was a case of an XY problem. That said, the question title alone is a legitimate question in its own right.
How do I get the String representation of a View's id
View v;
String ResourceIdAsString = v.getResources().getResourceName(v.getId());
Use this in the view constructors
How to find View from string instead of R.id
You can use something like this:
Resources res = getResources();
int id = res.getIdentifier("titleText", "id", getContext().getPackageName());
And then use the id.
android studio(java) finding view id with string
Since you're working with a lot of buttons I suggest you put them all into a ViewGroup (like LinearLayout, FrameLayout or something like that), then iterate through each View of that ViewGroup, check its instance then assign the click listener.
Here is some pseudo-code since I forgot how Java works
ViewGroup viewGroup = findViewById(R.id.yourViewGroupName);
for each child in viewGroup.getChildren {
if child is of type Button {
child.setOnClickListener(this);
}
}
Convert int Id of a View to its string resource name?
thanks for @David Medenjak this is Answer
getResources().getResourceEntryName(int resid);
Find view Id that matches a string android
If you want to get the ID of a string resource, you need to use Resources.GetIdentifier()
:
EX:
var applicationNameId = Resources.GetIdentifier("ApplicationName", "string", this.PackageName);
This would get the Resource ID for the ApplicationName
item in the strings.xml
file.
<string name="ApplicationName">AppName</string>
In other words, I should be able to take the identifier this gives me and search through my Resource.Designer.cs
for the same int
.
If you wanted to get the identifier of a view +id/
name, you might do the following instead:
EX:
var buttonNameId = Resources.GetIdentifier("MyButton", "id", this.PackageName);
Which would get the Resource ID for the Button
with an Id of MyButton
:
<Button
android:id="@+id/MyButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Hello"
/>
However based on the documentation, this is discouraged as it's much slower than just getting resources by IDs rather than by Name.
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
https://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String)
Android: View.setID(int id) programmatically - how to avoid ID conflicts?
According to View
documentation
The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.
So you can use any positive integer you like, but in this case there can be some views with equivalent id's. If you want to search for some view in hierarchy calling to setTag
with some key objects may be handy.
Related Topics
Rotating Image. Animation List or Animated Rotate? (Android)
Using Audiotrack in Android to Play a Wav File
How to Access Resource with Dynamic Name in My Case
Change Progressbar Color Through Code Only in Android
When to Use Fragmentmanager::Putfragment and Getfragment
How to Add Radio Button Dynamically as Per The Given Number of Counts
Set Textcursordrawable Programmatically
How to Enable Cookies in Android Webview
Just Upgraded My Os X to Yosemite, Android Device Chooser Not Showing Device - What to Do
Remove Unwanted White Space in Webview Android
Listfragment Does Not Accept My Layout
How to Center The Camera So That Marker Is at The Bottom of Screen? (Google Map API V2 Android)
Inconsistency Detected in Recyclerview, How to Change Contents of Recyclerview While Scrolling
Using Espresso to Unit Test Google Maps
Android Httpsurlconnection Eofexception