ContextMenu for blank space of an Android ListView - android

Is there a way I can get a context menu pop up when a user long presses on the blank space of a listview? I know that this can be done by setting wrap_content to the layout_height parameter of the listview. In fact I have been doing that successfully for a while. However, sometimes this wrap_content behaves very strangely and though there is enough space on the screen the listview restricts itself to a % of the screen and items scroll within that space. To avoid that problem I have moved to the path of setting the height as 0dp and weight as 1. However, that has disturbed the functionality I had in terms of long pressing the empty area of a list to add a new item to the list. Any help will be greatly appreciated.
Note: I have looked at multiple similar questions on SO throughout the day today but couldn't find any conclusive and elegant solution.

You can use ListView#addHeaderView() or ListView#addFooterView() to add extra view at the top or bottom of ListView, which you can make it looks like blank space.
Also I suggest you use match_parent to the layout_height attribute of ListView.

Related

How do I make UI elements fill the entire Android Screen

I am trying to add a bar similar to the "Today" bar here, but I cannot figure out how to force the width to match the screen. I am just using a simple textview (if there is a better way.. that's awesome, please tell it to me, but i cant find anything for it)
Here is what is happening with match_parent or fill_parent I recolored the textview to show the margins (that I dont want)
match_parent should fill the entire screen.
Check your parent activity layout or your fragment layout and you should find your margins there.
BTW, if you have a list of items like the one in the picture and you want it to have sections (like 'today', 'yesterday', etc) you should probably use recyclerview with headers instead of text view. Check this library for this, it's pretty easy:
https://github.com/cymcsg/UltimateRecyclerView

Android - keeping a single element contained on screen

I'm experimenting to see if the layout scheme I want to use is possible. I want to have an XML layout that's scrollable. Within that scrollable layout, I want to have a single line going horizontally across the screen (I used just a View with a fixed height and different color). When that horizontal line reaches the top of the screen, and as I scroll down through the layout, I want it to stop and remain at the top of the screen while being able to scroll through everything below it.
I've been messing around in XML trying to get it to work, basically putting a bunch of junk before it and after it.
Any ideas as to how that might work? Would I have to do something fancy with the java code to fix the red line at the top when the scroll position reaches a certain point? Your help would be greatly appreciated.
I am assuming you want something like Gmail app where when you scroll a mail the header sticks on top... To do this, you need 2 views. You have your regular scroller below and overlay a fixed view on top (you can use a relative layout to do this). When your cell goes past a certain spot, you want to populate and set the visibility of the fixed view to VISIBLE. This would give the impression that the view you want to 'stick' to the top really just got stuck rather than scrolled out of view. You'll have to work out the opposite scrolling scenario too based on the location and height of the scrolled cell/view.
HTH

What does android:isScrollContainer do?

Does anyone know what android:isScrollCOntainer = (boolean) or $(View).setScrollContainer(boolean) do?
At first I thought this would be the answer to set a View inside a ScrollView NOT to scroll with ScrollView, but it doesn't seem to be the case.
On Android Developers it says,
"Set this if the view will serve as a scrolling container, meaning that it can be resized to shrink its overall window so that there will be space for an input method. "
Can anyone kindly explain what this description means?
What is a scrolling container in this case?
What kind of input method is available?
A scrolling container is one where the size of the container is independent of it's content.
For instance you can make a ScrollView or ListView of height 100 pixels, but you can fit as much content in as you want. Similarly regardless of the size of the content in the view, you can set the size the of the View to whatever you'd like.
If a container is scrollable, then Android knows it can shrink the size of the container without rendering parts of the content of the container inaccessible (since the user can just scroll down to see things not on screen). It uses this for when the SoftKeyboard is opened - if a container is scrollable it will shrink it as much as possible in an attempt to keep all of the elements on screen.
So ScrollView, ListView, GridView etc are all examples of scrolling containers.
I am looking in to the same thing and I am not sure exactly what it means either. The input method is however the soft keyboard. Changing it affects how the views resize when an edittext is clicked and the keyboard pops up. Look in to android:windowSoftInputMode for more information.
I hope this was at least a little bit helpful!

Is there a way to define a list height as the height of all its items? (Android)

I asked this question earlier, but I didn't really understand the only response I got. I'm having some problems writing a program that adds to a list dynamically at runtime. The program has some things above the list and the idea is that the user scrolls down to see the list that the program generates based on the user's input. However, when I use a list it is impossible for the user to see the lower parts of the list because the list takes up a set amount of space and if you try to scroll, the parent ScrollView takes over and assumes that you are trying to scroll the entire activity.
Is there a way to make the List height expand dynamically to wrap the children that are added so that the user can see all of the items in the list while using the Scrollview's scrolling ability? I have tried setting the height to wrap_content, but that did not work. Any help is appreciated. Thanks.
The Android engineers have stressed that ListViews should never be put in ScrollViews. This answer might be of some help to you.

Scrolling issues with GridView in Android

I am having weird scrolling issues in my pretty simple GridView. Each item in the Grid is simply an ImageView and a TextView. The activity itself is simply an application selector. It looks like a rough version of the basic App Tray in Android.
The issue is that after spending some times scrolling through my view, it will inevitably allow me to continue scrolling past the top row of icons, to a blank screen, and the scroll bar will disappear, leaving me stuck.
It doesn't happen every time I go to the top of the view, only sometimes, and usually only after some excessive scrolling.
If I happen to notice the problem and catch it before the top row disappears off the bottom of the screen, I can usually scroll back through the view and spot some icons missing. There are empty spaces in the grid, and I can only assume that those icons have been moved to some bizarre position, which is allowing the view to scroll past the top.
This is my first Android app beyond a basic Hello World, so it's likely that I've just screwed up something in my layout files. I also realize that this is probably a pretty confusing description, so I'm hoping someone has experienced this and my search abilities simply were unable to find it.
I can post my layout files or other code if someone thinks that's useful.
Oh, and the program is built against 1.5, but is running on 2.2 (whatever state of 2.2 that was that snuck out last week) on my phone. I don't have enough apps to test this on an emulator, but could probably set something up if someone felt it necessary.
Thanks in advance for any help on the issue.
I had same problem, but I post to GridView only ImageView's so pengwang's answer didn't help me. I found explanation for this bug https://code.google.com/p/android/issues/detail?id=16218
The problem is that GridView does not currently handle items with differing heights.
My solution is to use exactly specified layout_height same for all GridView's items.
i have solved it: in the link GridView cannot show image you can try
It seems that if you scroll off the bottom of the GridView and back again very fast a couple of times, you eventually will notice that the elements of the view get shifted one place to the "left" from where they originally were. When this happens, you then can scroll all the way off the top of the GridView. When you do reach the top of the list, item 0 is not displayed, having item 1 in its place and nothing but blackness above that row.
Clicking on any item in this situation resets the list and everything is back to normal.
This must be a bug. It's highly reproducible, and I don't think anything I'm doing is causing the mix up. At this point, adding to the list adapter has stopped.
Right now I'm working with Android 2.3.3.
I just ran into this exact same issue myself -- I have a grid of images that have different heights. After reading the Android developer's doc on GridView, GridView.LayoutParams and GridLayout, I did two things that fix my issue:
find the tallest image in each row, and store the height in a map of row -> rowHeight. Each image that I download has width and height info, so this is easy for me to do by overriding my adapter's addAll(items) method.
in getView(), I create a GridView.LayoutParams(MATCH_PARENT, getRowHeight(position)), which sets each grid item to the max row height for its specific row.
wrap my ImageView inside a LinearLayout. I have tried other layout but LinearLayout is the one that works. Set android:scaleType="fitCenter" and android:adjustViewBounds="true" for the image view.
After above 3 steps I finally got the grid to look right, they have different heights, and there's no scrolling issues.
I had a similar, but probably less common problem, where just scrolling a little bit would scroll the gridview to the end and I was not able to scroll up at all. This only occurred when the gridview was smaller, for example when the keyboard was visible. The problem was that I used:
android:paddingBottom="400dp"
android:clipToPadding="false"
Making padding bottom much smaller fixed my issue. I hope this helps at least someone!

Categories

Resources