How can I dynamically assign a view using android?
<p>I'm trying to dynamically choose the view based on the device screen size. For most devices i want a list view, but for xlarge screen sizes I want a grid view. </p>

<p>I have created the layouts in two separate library_list.xml files, one in the layout folder and one in the layout-xlarge folder. </p>

<p>The list view looks like:</p>

<p><code>
ListView  xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/library_list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            android:textColor="@android:color/black"
            android:background="@android:color/transparent"
            android:cacheColorHint="#00000000"
</code></p>

<p>and the grid view looks like:</p>

<p><code>
GridView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/library_list"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:columnWidth="@dimen/library_column_width"
            android:numColumns="auto_fit"
            android:verticalSpacing="10dp"
            android:horizontalSpacing="@dimen/library_item_horizontal_spacing"
            android:stretchMode="columnWidth"
            android:textColor="@android:color/black"
            android:background="@android:color/transparent"
            android:cacheColorHint="#00000000" 
        </code></p>

<p>My problem is, in my java code i need to declare a public variable to hold the view and then assign it dynamically. </p>

<p>I tried declaring an AbsListView (I can't declare GridView or ListView because I don't know which one it is until runtime) like this:</p>

<p><code>
public static AbsListView libraryList;</p>

<p>...</p>

<p>libraryList = findViewById(R.id.library_list);
</code></p>

<p>which works on an Asus eee transformer tablet, but not samsung galaxy or htc desire (it just crashes, log cat doesn't tell me anything). Is declaring an AbsListView the right way to do it or am I missing something? </p>

<p>I have read through  <a href="http://developer.android.com/guide/practices/screens_support.html" rel="nofollow">http://developer.android.com/guide/practices/screens_support.html</a> but this doesn't seem to help for my problem. This seems like it should be a fairly common scenario but I can't find an answer anywhere. </p>

<p>Thanks!</p>
