Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findViewById not working for an include?

Tags:

android

I have an include like:

<include     android:id="@+id/placeHolder"     layout="@layout/test" /> 

The include layout looks like (test.xml):

<?xml version="1.0" encoding="utf-8"?>  <FrameLayout      xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/outer"     ... >      <ImageView         android:id="@+id/inner"         ... /> </FrameLayout> 

I can't seem to find the inner ImageView with id="inner" at runtime:

ImageView iv = (ImageView)findViewById(R.id.inner); if (iv == null) {     Log.e(TAG, "Not found!"); } 

Should I be able to find it? It seems like since it's using an "include", the normal findViewById method does not work.

---------- Update ----------------

So I can find the id assigned to the include:

View view = findViewById(R.id.placeHolder); // ok 

but I can't find any of its children by id like:

view.findViewById(R.id.outer); // nope view.findViewById(R.id.inner); // nope 

same as the original if I try searching for them directly like:

findViewById(R.id.outer); // nope findViewById(R.id.inner); // nope 

Do ids just get stripped off of elements at runtime maybe?

Thanks

like image 361
user291701 Avatar asked May 29 '12 17:05

user291701


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.

How does findViewById work on Android?

In the case of an Activity , findViewById starts the search from the content view (set with setContentView ) of the activity which is the view hierarchy inflated from the layout resource. So the View inflated from R. layout.

What is Viewview findViewById?

view. findViewById() is used to find a view inside a specific other view. For example to find a view inside your ListView row layout. Your app crashes without it because the view you're searching for is inside rowView .

What is findViewById R ID?

findViewById is the method that finds the View by the ID it is given. So findViewById(R. id. myName) finds the View with name 'myName'.


2 Answers

Try retrieving the <include /> and then searching within that

Make sure your root has the same ID as the root element in the included XML file.. ex

<include     android:id="@+id/outer"     layout="@layout/test" /> 

Then retrieve your "inner" content using:

FrameLayout outer = (FrameLayout)findViewById(R.id.outer);  ImageView iv = (ImageView)outer.findViewById(R.id.inner); if (iv == null) {     Log.e(TAG, "Not found!"); } 
like image 195
dymmeh Avatar answered Oct 04 '22 19:10

dymmeh


I just wanted to add to this for future Googlers, I had kind of the opposite problem where the root layout of the included xml (in this case outer), was null when calling findViewById(), but the children of outer were not null.

The problem was solved for me by removing the id property of the include 'view' (in this case placeHolder). When that was gone, I could find outer but it's id :)

If you need a an id assigned to the include item, I think it's better to wrap it in another viewgroup.

like image 30
Daniel Wilson Avatar answered Oct 04 '22 17:10

Daniel Wilson