Jones Jones - 5 months ago 30
Android Question

Android image view and list view

I want to do something like a file manager. I have a list of files and when I click a file if it is an image I have to show it on a preview. So, I made a table layout where I have two rows: one is the list view containing all the files and the second one is the image view where I want to show the image. Problem is when I add the image to the image view it doesn't appear at all. Here is the layout:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>

<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom" >

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ImageView>
</TableRow>
</TableLayout>


and here is where I add image to ImageView (I have to do it like this because my files are on sdcard)

ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageURI(Uri.fromFile(file));


Can someone tell me what I am doing wrong? Thanks

Answer

You would better use a LinearLayout. That will be more efficient.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

But if you have to use a TableLayout(I don't see any reason for your sample), you can use like this:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ListView
            android:id="@+id/listView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" >
        </ImageView>
    </TableRow>

</TableLayout>