52

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!

Community
  • 1
  • 1
Santiago
  • 1,575
  • 2
  • 12
  • 11
  • Why would you need that? What are you trying to do? – Macarse Feb 16 '10 at 09:52
  • It is a common situation for showing a view which is not present. For example, if you want a view to show when a user does an action such as pressing a button, clicking a menu icon, etc. – hexicle Dec 06 '17 at 20:57

6 Answers6

82

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

knjk04
  • 115
  • 2
  • 11
Santiago
  • 1,575
  • 2
  • 12
  • 11
5

I have changed in my activity but effected. Here is my code:

View layout = getLayoutInflater().inflate(R.layout.list_group,null);
        try {
            LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.ldrawernav);
            linearLayout.setBackgroundColor(Color.parseColor("#ffffff"));
        }
        catch (Exception e) {

        }

    }
Uwe Allner
  • 3,399
  • 9
  • 35
  • 49
user5076776
  • 51
  • 1
  • 1
4

try:

Activity parentActivity = this.getParent();
if (parentActivity != null)
{
    View landmarkEditNameView = (EditText) parentActivity.findViewById(R.id. landmark_name_dialog_edit);
}
richy
  • 2,716
  • 1
  • 33
  • 42
3

Another way to do this is:

// inflate the layout
View myLayout = LayoutInflater.from(this).inflate(R.layout.MY_LAYOUT,null);

// load the text view
TextView myView = (TextView) myLayout.findViewById(R.id.MY_VIEW);
JFreeman
  • 974
  • 1
  • 10
  • 26
0

It's impossible. You can only find and access views that are currently running. If you want to check the value of ex. TextView used in previus activity you must save the value is SharedPreferences, database, file or pass by Intent.

skyman
  • 2,422
  • 17
  • 16
0

I used

View.inflate(getContext(), R.layout.whatever, null)

The using of View.inflate prevents the warning of using null at getLayoutInflater().inflate().

Avital
  • 549
  • 5
  • 15