Add Button to XML Layout programatically
02:25 09 May 2013

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:




   

    


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.

android xml button layout