Android delete from listview [duplicate] - android

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.

Related

Android First ListView scrolls down to only half of last entry with two ListViews

I have what I thought would be straight forward to implement: A linear layout with two ListViews, both with a layout_weight="1". The first one is "fill parent" and the second one is "wrap content". The idea is that the first ListView is to occupy the entire screen when the second ListView is empty, and when both had entries the screen would be shared. Well, it almost works but when the second ListView is empty, the first ListView scrolls down only half-way through the LAST entry. The remainder of the last entry is off the screen. Does anyone understand why that is the case?
Here is my layout:
<?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" >
<LinearLayout
android:id="#+id/dots"
android:layout_width="match_parent"
android:layout_height="24dp" >
</LinearLayout>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/lni_logo" />
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="none"
android:transcriptMode="alwaysScroll"
android:layout_weight="1"
android:id="#android:id/list" >
</ListView>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="none"
android:transcriptMode="alwaysScroll"
android:layout_weight="1"
android:id="#+id/rtsalist">
</ListView>
</LinearLayout>
Actually at this stage I have not made an adapter for my second ListView so I have not been able to test it with entries in it. But the point is that it should be as if I did not have the ListView at all when it is empty. Alas such is not the case. When I remove the second ListView, the first one scrolls down to the last element correctly, displaying the entire entry.
Thanks for any insight!
Put in a line like android:paddingBottom="20dp" into your top ListView. Adjust the 20dp value until it looks the way you want it to. You can programmatically change the bottom padding as the list disappears and reappeares to make it look just right.

Android - How to set maximum size for listview

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>

Sliding List View Layout Android

I am creating an application that contains a header with a button on the right that should slide a list view to left when I click on it(similar to the chat list in Facebook)
-->
When I was including this header without putting the list view in it, it was working properly. But now I am inserting this list view inside the .xml file of the header (because the header is always shown for the user and he can click on this list view button in any layout that is shown for him).
The problem that I want to include this header to all the other layouts without changing the way they looks and show this list each time he click on the button.
I searched and found a lot of answers but every solution was about making the same as I did in the previous screen shots in each page which is not an optimistic solution for multiple pages.
*I am working on API level 10 and want to find the solution without using Actionbar.
Tell me please if more information would be helpful, any help will be really appreciated.
Thanks in advance.
UPDATE my xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GeneralRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout android:id="#+id/rel_layout"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:background="#android:color/white" android:layout_alignParentRight="true">
<LinearLayout android:id="#+id/fake_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"></LinearLayout>
<ListView android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#android:color/background_dark"
></ListView>
</RelativeLayout>
<RelativeLayout android:id="#+id/rel_layout_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<LinearLayout android:id="#+id/lin_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:orientation="vertical"
android:layout_centerHorizontal="true" android:layout_alignParentTop="true">
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="slide"
android:layout_gravity="right|center"></Button>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Try to create a RelativeLayout for the ListView with a android:visibility="gone" and all the other components in the same User Interface inside another RelativeLayout.
Then change its visibility upon the state of the menu if it's opened or closed.
Guess this should be a way to go:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:clickable="false">
<LinearLayout
android:id="#+id/thisistopbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="3dp"/>
<include here listview etc../>
</LinearLayout>
you can also make the top bar general and include it in every screen. But the basic idea is like above xml, and put the button on the top bar.

adding button to ListActivity

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

listview footer dynamically change state android

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!

Categories

Resources