How to findViewById form another XML file?

SWE picture SWE · May 14, 2012 · Viewed 11.1k times · Source

I connent an addevent.java with addevent.xml is content a listview, the listview take its element from another XML file "item.xml"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

        <Button
            android:id="@+id/d"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:drawablePadding="0dip"
            android:drawableRight="@drawable/icon_name" />

        <EditText
            android:id="@+id/en"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/d" >
        </EditText>

</LinearLayout>

now I need to findviewbyId not from basic xml file but form another "item.xml"

I tried LayoutInflater as this, code but it isn't work:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addevent);
        myList = (ListView) findViewById(R.id.MyList);
        myList.setItemsCanFocus(true);
        myAdapter = new MyAdapter();
        myList.setAdapter(myAdapter);


        final LayoutInflater factory = getLayoutInflater();

        final View textEntryView = factory.inflate(R.layout.item, null);

        tvDisplayDate = (EditText)textEntryView.findViewById(R.id.editd);
        btnChangeDate=(Button)textEntryView.findViewById(R.id.d);

any help please

Answer

Simon Dorociak picture Simon Dorociak · May 14, 2012

Try this View view = LayoutInflater.from(getApplication()).inflate(R.layout.item, null);

How you can see, static from() method takes 1 argument(Context) and you obtains LayoutInflater from given Context, in your case it will be your Activity where you want to inflate View.

Hope it helps!