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.

2
  • 1
    Post your listview xml Commented May 9, 2013 at 8:01
  • Just an opinion, why don't you done the whole thing in RelativeLayout and use android:layout_below="" Commented May 9, 2013 at 8:15

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I am not sure if that can be done in my case. I will edit my question.
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>

2 Comments

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.
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.