ListView not draw View widget - android

I'm trying to create list item with blue overlay on top of the item. The blue overlay only appear when user touch on the item. So I'm placing view with background drawable to cover the whole item.
The problem now is that this View never get drawn at all. I could switch the background to simple color and it would not appear. If I change it to TextView instead, the text will be shown but not covered the whole item. What am I doing wrong?
The layout get updated for sure, if I change color of any TextView it would apply that change accordingly.
Here's a layout for the list item.
<?xml version="1.0" encoding="utf-8"?>
..<!-- other stuff -->..
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#drawable/item_overlay"
android:duplicateParentState="true"
android:visibility="visible"
/>

1) You can list selector, along with option drawSelectoronTop
<ListView android:listSelector="#drawable/list_selector"
android:drawSelectorOnTop="true">
</ListView>
setting drawSelectoronTop true will cause the drawable to draw over the list item(ForeGround)
2) If list selector can't be applied to your particular design, you can use FrameLayout as the list item's parentView, with foreground property
android:foreground="#drawable/list_selector"
for more about this you can refer the post Draw selector on top - for a basic linear layout?

Related

How to remove the divider from the children of an ExpandableListView?

I am making Multi Choice Questions in android. And for that i am using Expandable listview. I don't want to show spaces (dividers) between child views, and also between child and parent view. But i want divider between different expandable listviews. I have search a lot but what i found is that add transparent color between child views but it still shows space between child views. So is there any way to remove the space between the child views. This is my xml code for Expandable listview.
<ExpandableListView
android:id="#+id/list_single_choice_questions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="5dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:divider="#ffffff"
android:childDivider="#00000000"/>
android:divider to choose the parent drawable or the android:footerDividersEnabled / android:headerDividersEnabled might be what you are looking for the parent views. These are all inherited from the ListView parent.
As for child views we can look at Java methods to set it programmatically, see
Android docs on Expandable List. For example setChildDivider() :
Sets the drawable that will be drawn adjacent to every child in the
list. This will be drawn using the same height as the normal divider
(setDivider(Drawable)) or if it does not have an intrinsic height, the
height set by setDividerHeight(int).
Therefore the height used is set by setDividerHeight() or the intrinsic height.
Other than that you could change the child view's xml layout to make the space between each smaller.
<LinearLayout
android:id="#+id/child_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<!--no padding or margin-->/>
EDIT:
You could try creating a childDividerShape drawable like so:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:height="0dp"/>
</shape>
And then set it to the android:childDivider attribute.
...
android:childDivider="#drawable/childDividerShape"
...
add two below line in ExpandableListView in xml file
android:divider="#null"
android:dividerHeight="0dp"

Position textview below list & reposition it as size of list increases

In my layout, I have a textview which I need to be fixed at the top of the screen, a button to be fixed at the bottom of the screen.
Between these two, I have a list with a textview(with a drawable) below it. (please see image). I want the textview positioned below the list;i.e.; as list items grow, the textview should reposition itself. As I click on the textview, I add items to the list. I am able to achieve this, but as my list items increase, the textview goes below the list & is not visible. So what I see on screen is the top textview with the list below it & the bottom button. The textview to add items in list view is not visible, so I cannot add items to my list. I need that even if the list items increase, I want the list to be visible along with the textview.i.e.; as list items grow, the textview should reposition itself. I tried various approaches suggested with several combinations of linear & relative layouts & positioning techniques, but I am not able to achieve this.
Try 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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc xyz"
android:gravity="center"
android:layout_alignParentTop="true" />
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button"></ListView>
<TextView
android:layout_below="#+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Save"
android:id="#+id/button" />
</RelativeLayout>
You have to nest the layouts here. You could for example use a LinearLayout with vertical orientation as parent and place your textview and listview (or whatver you are using for your item list) inside the LinearLayout. Here's the idea:
-TopView: set to wrap content
-ListView: set to fill parent
-TextView: Set to wrap content
If I'm not mistaken this should set the top and bottom view to fixed sizes and make the ListView fill the remaining space.
You can try adding that button with image as footer to the listview it will work with less noumber of items when it exceed the screen hight remove footer and make the bottom layout visible(Have to add the same footer item in bottom layout also).

Adding bottom margin to ListView last element

I need to add to add ListView with complicated items background: different for even/odd and rounded corners at the top and bottom. It looks like this:
I have implemented all this stuff via level-list, but there is one more thing I want to do.
Now the bottom item is near the bottom of the screen. It is better to add some space.
I don't want to add bottom margin to ListView, I need margin only for last item.
The ways I see to do this:
Footer
A kind of hack – add footer with empty TextView to ListView. But footers are quite unstable things, they usually disappear after notifyDataSetChanged and there is no way to get them back
Image with transparent pixels
I asked designer to add transparent pixels to bottom background resource. Unfortunately, in this case vertical centering is completely broken.
For example, there is 9patch like this:
And layout 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"
>
<!-- View with background with transparent pixels on bottom -->
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="#+id/item"
android:background="#drawable/some_bgr"
android:padding="10dp"
>
<TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"
android:text="Title"
android:layout_gravity="center"
android:textSize="18sp"
/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Detail"
android:layout_gravity="center"
android:textSize="18sp"
/>
</LinearLayout>
<!-- Just for marking place took by view -->
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_below="#id/item"
android:background="#88ff55"
/>
</RelativeLayout>
The result:
As you see, centering is not working. Unfortunately.
(BTW, if specify this 9patch as background for TextView, centering works good. If you know any article, explaining this, please let me know.)
Add bottom margin to last item in Adapter implementation
That should work, but for unknown reason I still can't get it work.
I don't like this way, because I don't like to modify dimensions in code.
So
There is already imaginary way – construct some XML drawable with particular bitmap and margin. According to drawables concept it should be possible, but I can't find implementation. May be somebody knows?
Any other ideas?
In your ListView, set a paddingBottom and clipToPadding="false".
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"/>
This also works for RecyclerView.
Only use android:scrollbarStyle="outsideOverlay" if you want the scroll bar to not overflow into the padded area.
add an empty footer in your list like this:
TextView empty = new TextView(this);
empty.setHeight(150);
listview.addFooterView(empty);
you can also do it from code if you want, for example here I react to
to EditText different situations:
if(s.toString().length()>0)
{
contacts_lv.setClipToPadding(false);
contacts_lv.setPadding(0,0,0,270*screenDensity);
}
else
{
contacts_lv.setClipToPadding(true);
contacts_lv.setPadding(0,0,0,0);
}
Clocksmith's answer is the best and pretty clever. You can also create an empty footer view.
Add these two lines in your listView XML code:
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true"
Another solution might be that you make a mock view with certain height.
In your adapter in getViewCount return 2.
In getCount return yourData.size+1.
In getViewType check if the element is last element return 2;
Use this type in getView to populate the mockview.
I guess you want to add margin only to last item:
So you can do in this manner, in your getview method the index of the list item and check if its the last item, then progrmatically add margin to the view.

Adding background in Activity makes my view seem faded

I have an Activity with a ListView.I want to set as a background a simple white picture with a logo in the right lower corner.My problem is that when i set as background my picture it kinda 'sits' in front of my listView.Making the textViews seem faded.How can i effectively add a background without these side effects?Thank you for your time.
EDIT:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg">
<dragNDropAdapter.DragNDropListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</dragNDropAdapter.DragNDropListView>
</RelativeLayout>
Put imageview & listview in relative layout.
Set attribute for your image where you want to place it and for list as well.

How create and show listview in onDraw method?

I am new in Android, so please help me.
In onDraw method I draw a lot of things, which I would like to have as a background. And when I click in the corner, I would like to have listview (with SeekBars, buttons, etc) and everything which i drow before as a background. This background doesn't have to scroll when the listview is scrolling. How to create and set such listview?
My .xml:
<?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"
android:orientation="horizontal" >
<com.project.MyView
android:id="#+id/main_view"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</LinearLayout>
Make your custom view as background of whole screen . You can do this by making the root view as your custom view .Then calculate the touch on corner and make your child view i.e. list view visible .

Categories

Resources