Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find view by name

Is it possible to find a view by its name rather than its id.

findViewById(R.id.someView); 

but I would like to do something like this:

findViewByName("someView"); 
like image 211
Drejc Avatar asked Jul 26 '11 14:07

Drejc


People also ask

What is findViewById () method used for?

FindViewById<T>(Int32)Finds a view that was identified by the id attribute from the XML layout resource.

What does focusable mean Android?

Focusable means that it can gain the focus from an input device like a keyboard. Input devices like keyboards cannot decide which view to send its input events to based on the inputs itself, so they send them to the view that has focus.

What does findViewById return?

findViewById returns an instance of View , which is then cast to the target class. All good so far. To setup the view, findViewById constructs an AttributeSet from the parameters in the associated XML declaration which it passes to the constructor of View . We then cast the View instance to Button .

What is SetContentView?

SetContentView is used to fill the window with the UI provided from layout file incase of setContentView(R. layout. somae_file). Here layoutfile is inflated to view and added to the Activity context(Window).


1 Answers

you have to find views by identifier when dealing with xml, but you can look up the identifier by using getIdentifier(String name, ...) which is useful if you have your layouts numbered for example. Just be aware that such a lookup is relatively expensive.

to complete the answer

int id = getResources().getIdentifier(name, "id", context.getPackageName()); View view = findViewById(id); 
like image 87
MrJre Avatar answered Oct 02 '22 04:10

MrJre