1

I want to inflate some views but have a button at bottom of the view. I already know how to dynamically inflate views and then inflate the button below the other inflated views. However, when there are only a few inflated views (causing the combined view to take less space than an entire screen) the button does not rest at the bottom of the page as i would like. My idea is that i need to have the button aligned at the bottom of the relative layout by setting the button to: android:layout_alignParentBottom (when i try to do this though the app crashes)

here is a screenshot of what i have, followed by a screenshot of what i want, all the items you see are inflated onto a scrollview that is inside a relative layout. I think the problem is that the button is inflated onto the scrollview which is only as large at the items force it to be (unfortunately I'm too noob to know how to inflate onto a different part of the view)

enter image description here

the code for the image on the left is as follows (i dont have and errors for the view on the left): parentView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/blurybg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">    
<ScrollView     
  android:layout_height="wrap_content" 
  android:id="@+id/scrollView1" 
  android:layout_width="fill_parent" >
<LinearLayout     
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id= "@+id/parent_of_inflated_view_id">
</LinearLayout>   
</ScrollView>
</RelativeLayout>

childButton (other views are similar to this)

<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:height="30dp"
android:layout_width="fill_parent"
android:background="@drawable/done"
android:layout_alignBottom ="@layout/availablerestaurants"
/>

Java for creating the inflator and inflating the button. other inflated views are similar to this (i use the same inflator of course). I am adding the inflated button last to get it at the bottom of the view.

LayoutInflater theInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout linLayout = (LinearLayout)findViewById(R.id.parent_of_inflated_view_id); 

    Button doneButtonCurrentRest=  (Button) theInflater.inflate(R.layout.done_button_availrest, null);
    linLayout.addView(doneButtonCurrentRest);

Thanks for the help, Adam

Goku
  • 1,565
  • 1
  • 29
  • 62
  • Piece of code would be useful to point your errors. – olshevski Aug 30 '12 at 15:39
  • Also, it is quite unclear why "android:layout_alignParentBottom" failed. Please, provide some log of exception. – olshevski Aug 30 '12 at 15:45
  • just fyi: when i tried doing the align parent bottom, i did NOT inflate the button, i simply put the button in the parent view xml and set it to align parent bottom of the relative layout. i guess i cant inflate my other views in between the button and the top of the view? – Goku Aug 30 '12 at 16:11

1 Answers1

1

Please check my code. It should work as intended:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/parent_of_inflated_view_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

    <Button
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

What about your code. I can see several mistakes:

1) You should specify android:layout_height for your button. Without this parameter exception will be thrown. You use android:height but it is just not the same as android:layout_height.

2) android:layout_alignBottom should point to the id, but not to the layout. Moreover, i don't see the purpose of this attribute in the separate layout for button.

Update

Here is the solution for the question in the comment below:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:id="@+id/layout_for_positioning"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/parent_of_inflated_view_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_weight="0" />
    </LinearLayout>

</ScrollView>

Note that i used android:fillViewport="true" for the ScrollView so it can fill the entire screen. Also i used android:layout_weight to stretch views properly (you can find the explanation of the attribute here).

Community
  • 1
  • 1
olshevski
  • 4,971
  • 2
  • 35
  • 34
  • Alright, I used your code (I actually initially used something similar to this by having a lin layout between the scrollview and the relLayout that i inflated the button onto). The problem i have with both of these solutions is that the "done button" is always on the page after i add alot of inflated views. Is there a way for me to have it so that i wont see the "done button" until i scroll past all of those inflated views? I am very thankful for your comment, its very close to the right answer i feel – Goku Aug 31 '12 at 15:01
  • But at the same time you want the button to be at the end of the screen as on the 2nd screenshot? Let me think. – olshevski Aug 31 '12 at 15:08
  • why do you have a linear layout inside of another linear layout like that? – Goku Aug 31 '12 at 19:39
  • First LinearLayout is for proper button placing: button will be at the bottom of the screen, and when there are too much inflated elements it will be below them (you should scroll for it). I think it is quite hard to achieve the same with one layout. Also this structure looks more logical: separate layout for your inflated items that have nothing common with control element (Button) – olshevski Aug 31 '12 at 19:52
  • Maybe i should be more clear in my explanations: 1) first layout is needed for proper positioning of the button; 2) second layout (@+id/parent_of_inflated_view_id) is for storing inflated views. That's it. – olshevski Aug 31 '12 at 19:55
  • Thank you! it worked! Can you explain what you did to fix this? does the fill viewpoint make this work? – Goku Aug 31 '12 at 21:59
  • 1
    First of all I used android:fillViewport="true" to make ScrollView match his parent. I'm not sure why, but android:layout_height="match_parent" doesn't work as expected for ScrollView. Probably because of the varing actual length of this view. Secondary, i used android:layout_weight to set proportions for the child views of id/layout_for_positioning. Try to change some values and see what happens. – olshevski Sep 04 '12 at 09:51