Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: findviewbyid: finding view by id when view is not on the same layout invoked by setContentView

Tags:

android

layout

I have an activity MyActivity that extends from MapActivity. In the .xml file containing the layout I can only include the MapView

<com.google.android.maps.MapView     xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/trail_map_view"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:clickable="true"     android:apiKey="key" /> 

However I do need to find another view that is located in another .xml file. Unfortunately, findViewById returns null. How can I get the view I am looking for?

Thanks a lot!

like image 603
Santiago Avatar asked Feb 16 '10 08:02

Santiago


1 Answers

Thanks for commenting, I understand what you mean but I didn't want to check old values. I just wanted to get a pointer to that view.

Looking at someone else's code I have just found a workaround, you can access the root of a layout using LayoutInflater.

The code is the following, where this is an Activity:

final LayoutInflater factory = getLayoutInflater();  final View textEntryView = factory.inflate(R.layout.landmark_new_dialog, null);  landmarkEditNameView = (EditText) textEntryView.findViewById(R.id.landmark_name_dialog_edit); 

You need to get the inflater for this context, access the root view through the inflate method and finally call findViewById on the root view of the layout.

Hope this is useful for someone! Bye

like image 79
Santiago Avatar answered Oct 19 '22 17:10

Santiago