Class from XML layout that can be instantiated in the main activity

NuMan

New Member
So I have this xml file \[code\]<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:eek:rientation="vertical" android:padding="10sp" android:id="@+id/lengthLayout" android:background="@color/gray2" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/inputLength" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="0.3" android:hint="@string/temperatureHint" android:inputType="number" android:textSize="30sp" tools:ignore="NestedWeights" /> <Spinner android:id="@+id/lengthUnits" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="0.7" /> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="15dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:eek:rientation="horizontal" > <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:verticalSpacing="10sp" android:horizontalSpacing="10dp" android:gravity="center" android:id="@+id/results_length" android:divider="@android:color/transparent" android:dividerHeight="5sp" > </ListView> </LinearLayout></LinearLayout>\[/code\]and I want to create a class that will use the resources from this XML file. So that when the class is instantiated in the main activity it will add the view to the app . \[code\]public class LengthFragment extends View implements OnClickListener, OnItemSelectedListener{ Context rContext;public LengthFragment(Context context){ super(context); rContext = context; inflate(context, R.layout.view_length, null);}\[/code\]and this is what I am doing in the main activity \[code\]LengthFragment lF = new LengthFragment(testapp.this);\[/code\]I have even tried passing in the inflator of the main activity to lengthfragment and inflating the view using that inflator but it doesnt workall it does is gives me a blank view.Thanks.also it works when I do this in the main activity LinearLayout l1 = (LinearLayout) getLayoutInflater().inflate(R.layout.view_length, null);but I want to create this view outside of main acttivity but just instantiaing a class in main activity.
 
Top