I have ListView and I want add footer. I easily did it through standard addFooterView method. But footer added into end of ListView and displayed as part of ListView (i.e. if ListView items more then placed on the screen then footer outside the screen).
Why I need it: I want show button when user interacted with some View in ListView. And "lift up" ListView border at bottom of screen on (current height value - height value of button).
The problem is: onClickListener required View in adapter. And setContentView in Activity. I do not get access to the parameters of a button from the adapter to ListActivity.
My XML Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/watchAddList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:listSelector="#drawable/list_selector"
style="#style/listViewStyle">
</ListView>
<include
layout="#layout/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
And include Layout:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:id="#+id/rl_submit">
<Button
android:id="#+id/submit"
android:layout_height="wrap_content"
android:text="Сохранить"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"></Button>
</RelativeLayout>
UPD: I do It! i used Singleton, and call Activity instance in Adapter class, then got access to the button properties. Cool!
Related
I have the following layout
<?xml version="1.0" encoding="utf-8"?>
<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="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:longClickable="true"/>
<TextView
android:id="#+id/notes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout>
The TextView is populated programmatically and is shown at the bottom of the list as I require however when the list starts to fill the screen the TextView is list behind the list and can no longer be seen. How can I stop the List view expanding into the space occupied by the text view?
I was thinking about changing it to a ScrollView but is was my understanding that you shouldn't use a ListView inside a ScrollView.
Use a RelativeLayout Instead Of LinearLayout
and add this to your ListView
<ListView
...
android:layout_above="#+id/notes"
/>
if you want the textView To be at the bottom of your view add this too:
android:layout_alignParentBottom="true"
I would like to make a listview with dynamic size. When I press the button, it will add a row to the listview. And I want the button is always aligned to bottom of the list view.
The problem is listview will push the button out of screen when it have enough rows. So in this case, how to make the button align to bottom of the screen or set the max height for the listview? But when the listview just have 1 or 2 rows, I want the button is right below the listview.
If you want the button to be displayed even though the user is at the top of the list view see other answers, but if you prefer to display the button when the user scrolls to the bottom of the listView use this approach with a footer.
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.addFooterView(footer);
The footer layout contains the button.
You can have into Linear Layout a ListView and a button with different weight. Something like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/reportCommentsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/reportCommentsListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:paddingTop="5dp" >
</ListView>
<Button
android:id="#+id/reportCommnets_Addbutton"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:text="Button" />
</LinearLayout>
According to your question, you have to dynamically change the position of your button. From my comment, this is the answer.
Attach listener to your ListView : see https://stackoverflow.com/a/4433294/1377145
In the listener callback, check the visibility of your button :
Rect videoBounds = new Rect();
yourButton.getGlobalVisibleRect(videoBounds);
getGlobalVisibleRect will return true if the button is visible > 0%, false either.
To fix button on the bottom : change the LayoutParams of your button and set alignParentButtom to true. Else set alignParentButtomto false.
bis : depending on your layout, you will have to "move" your button in the view tree with removeView/addView. Don't forget to removeview so the view will not have a parent naymore before adding it to another view.
Additional note : to have a % visibility on your button, you can use the videoBounds.height() and a rule of three :
100 * videoBounds.height() / yourButton.getHeight
EDIT : change answer to match accepted one.
You can make a RelativeLayout with the Button using the rule alignParentBottom=true and the ListView using the rule above. Something like this:
<?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" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/button" >
</ListView>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"/>
Something like this should help:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Add"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/add"
/>
</RelativeLayout>
Sorry, to inject my own personal opinions, but I think a "add more" or :load more button" always be the last view within a ListView.
To answer your question, to achieve what you need, the Listview and the "add row" button should be siblings within a ViewGroup.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="48dp"
android:minHeight="48dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
The key point here is that the Button has a fixed height and the ListView takes the rest of the space. The button also aligns to the bottom of the parent view.
Use Relative Layout.
Put the button at the bottom of screen, and set the listView position above him:
<?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" >
<ListView
android:layout_width="match_parent"
android:id="#+id/list"
android:layout_above="#+id/button1"
android:layout_height="wrap_content">
</ListView>
<Button
android:layout_width="match_parent"
android:id="#+id/button1"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" />
</RelativeLayout>
Basically here is a mspaint demonstraiton of what I want to accomplish:
I do not want the outline to be visible to the user, it was just to give you an idea of what I want to do. I want new textviews to take position under each other whenever a new goal is added from the edittext-button combo on the bottom part of the GUI.
I know how to save the data - SQLite. But I have no idea how to make new textviews be on the bottom of the last one - and if there is no more room on the screen I want that textview part to be scrollable.
I don't know what I need to use to accomplish this.
Thanks in advance!
create two xml files.
One consisting of list view
<?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="vertical"
android:background="#drawable/bkgrnd_320x285">
<ListView
android:id="#+id/List1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#000000"
android:dividerHeight="1dp"
android:fastScrollEnabled="true"/>
</LinearLayout>
and another xml for just the textView eg. row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Name"
android:textSize="20dp"
android:layout_marginTop="20dp" />
</LinearLayout>
Then in the main activity u need to refer to both the xml files as follows
ListView ll = (ListView) findViewById(R.id.List1);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.row,R.id.textView2, arraylist_you_want_to_display);
ll.setAdapter(arrayAdapter);
where R.layout.row is the row.xml consisting of textView
And R.id.textView2 is the id of the textView present in the row.xml
You may create a LinearLayout, setting the orientation to vertical.
Adding a ScrollView or ListView to show the upper part, with android:weight=1
Add the EditText to the LinearLayout, and this is done.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/List1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weight="1"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
I'm developing and android application in which I have created a ListActivity which contains an ImageView and a TextView. There is also the ability to drag and drop those objects so that they can be moved. What I want to do now is to have a button at the bottom of the screen,not the list.
As the user scrolls the button will always stay there. I found this link but when i did what this link suggested no button did appear, and instead i got a huge blank space that takes over a quarter of the screen. Can anyone point me to the right direction?
ok so here is the .xml file that is set to the ListActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<DragNDrop.DragNDropListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</DragNDrop.DragNDropListView>
</LinearLayout>
hopefully it can help
Your view should be like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button"
android:layout_centerHorizontal="true"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/btn"/>
</RelativeLayout>
Populate the listview with the data and see the button will be on its place though list being scrolled.
I think that ListActivity is a bad practice.
What you need to do is to is to change the Activity from ListActivity to Activity. In the layout file, do the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_contant"
android:orientation="horizontal">
<DragNDrop.DragNDropListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_contant">
</DragNDrop.DragNDropListView>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
In on create you can get the list reference by doing:
ListView lv = (ListView)findViewById(R.id.list);
In order to make the button clickable, user OnClickListener
I have listview that contain checkbox and an image when the checkbox is clikced I show a button at bottom of the screen that perform deletion, but when listview height more ,then the listview some portion move under the button ,so I need an alternative option for delete can anyone help me, I except something like menu?
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_centerHorizontal="true"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="#+id/DelPhto"
android:text="Delete" android:layout_alignParentBottom="true" />
<ListView
android:layout_width="280dp"
android:layout_height="fill_parent"
android:id="#+id/list_pictures"
android:layout_alignParentTop="true"
android:layout_above="#+id/DelPhto" />
</RelativeLayout>
Do you mean you wish for the delete button to be always visible even when the list contents are larger than the list control?
If that's the case try setting the layout_weight of your ListView to 1 and see if that solves your problem.
Put ListView in ScrollView (so one will be able to scroll entire list), like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainView"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listView"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:headerDividersEnabled="true">
</ListView>
</ScrollView>
</LinearLayout>
I have had a similar issue in the past. I found that using a relative layout and defining the button before the list solved my issues. Lets consider the following.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/someButtonId"
android:background="#drawable/gray_button"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_alignParentBottom="true"
android:text="#string/some_button_value"
android:textColor="#color/button_text"
/>
<ListView
android:id="#+id/someList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/someButtonId"
/>
</RelativeLayout>
Here I have defined a Relative layout, the layout will occupy the full screen's width and height. I then place the button on the bottom of the RelativeLayout. My expectation is that the list will be placed above the defined button, and fill the remainder of the screen with list contents. Because we are telling the list View to be placed above the button, it will never grow large enough to cover the button causing the list view to mask the button clicks from the user.
hope this helps.