0

This problem has been asked many times here, but none of the solutions work for me, because my XML layout is a bit different.

I have an XML with LinearLayout containing an ImageView and a TextView. The XML is populated using Java code. It is a file picker library from here, it lists the files and folders that exists in the android filesystem.

Here is the 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="horizontal" >

   <ImageView
        android:id="@+id/file_picker_image"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:layout_marginBottom="5dip"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="5dip"
        android:contentDescription="@string/app_name"
        android:scaleType="centerCrop"
        android:src="@drawable/file" />

    <TextView
        android:id="@+id/file_picker_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:singleLine="true"
        android:text="@string/file_name"
        android:textSize="28sp" />

</LinearLayout>

Now, I want to add a button just below this list. When I try to add the button in the XML, it iterates though each row, and buttons are shown in every row. When I do it using Java code, neighter the button, nor the textview is displayed, but the image view is displayed.

What I want is similar to the image in this post, but cannot achieve it till now. None of those solutions or any other that I read here works in my case.

I am trying to add the button using this code in the Activity whose source is in the link provided above. I tried to add the following code in FilePickerListAdapter's constructor.

            RelativeLayout relLayout = new RelativeLayout(context);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

            Button filterBtn = new Button(context);
            filterBtn.setText("Filter");

            relLayout.addView(filterBtn, params);

Please guide me what am I doing wrong, and how to make it possible.

Regards.

Community
  • 1
  • 1
openrijal
  • 583
  • 6
  • 22

2 Answers2

1

Mixing the xml way and programmatic way to create views is not a good idea. You might as well create all views in xml first (including your button), only set your button to android:visibility="gone" to hide it. Set it to visible when you want.

Neoh
  • 15,906
  • 14
  • 66
  • 78
0

This might not be something you asked for, but it's an alternative that you can try it out. I implemented layout_weight into your xml and included the button at the end of the screen. The outcome of this will be something similar with the link in your question.

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight=".9" >

       <ImageView
           android:id="@+id/file_picker_image"
           android:layout_width="40dip"
           android:layout_height="40dip"
           android:layout_marginBottom="5dip"
           android:layout_marginLeft="5dip"
           android:layout_marginTop="5dip"
           android:contentDescription="@string/app_name"
           android:scaleType="centerCrop"
           android:src="@drawable/file" />

       <TextView
           android:id="@+id/file_picker_text"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginLeft="10dip"
           android:singleLine="true"
           android:text="@string/file_name"
           android:textSize="28sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="0.1" >

        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />  <!--Your Button-->

    </LinearLayout>

</LinearLayout>
IssacZH.
  • 1,457
  • 3
  • 24
  • 54
  • May be I posted the whole thing differently. Let me try again. I cannot add Button in XML because it iterates itself in the loop. Even @IssacZH 's way also kept the button in the loop. I will edit my question. – openrijal May 09 '13 at 08:52
  • I see, I might have missed that sentence of yours. So it means that you only wanted to add the button programmatically at the end of your screen? – IssacZH. May 09 '13 at 08:57