Here is an example of what I am saying in title. Take the app "Feedly". If there is even ONE item i your list, it looks like this:
Now, if the list is empty, there is a nice way to handle this to avoid a big, blank, white space, like so:
How can one achieve this?
Off hand I am thinking maybe this way, see my pseudo-code:
if (adapter.size < 1) {
// change ListView Background;
}
Check out the ListView#setEmptyView method.
If your have an empty ListView, the height should be 0 and it may be no use to set the background for ListView.
I have several solutions here:
Set the background for the LinearLayout/RelativeLayout, whatever which contain the ListView according to size of adapter.
Create another ImageView which is the "All read" image, and set its visibility according to data size of the ListView adapter.
Create a view for what you want to display when adapter size = 0, and use ViewSwitcher to switch between ListView and "All read" view according to adapter size. I will recommend this one since it may be more flexible for the "All read" view. You can have button, TextView, ImageView or whatever layout element you want to put in it.
2 and 3 are similar but using ViewSwitcher may make the code readability better than setting Visibility for views.
Here is an example for using ViewSwitcher.
Related
I need help regarding adding new set of views and get value from those views.
In my example, I want to add 4 TextViews on Button click and open TimePicker from each view click and display new selected time on respective TextView.
Below is screenshot of view.
If these 4 views are fixed you could just create the xml and add them all to a single holder that you set invisible. If you mean by dynamic that it could be either 4 or 99 views, I'd recommend a RecyclerView. Plenty of examples on the internet. If you create a recyclerview with a custom adapter it is very easy to get the respective data per view.
For the future, please add more context to your question like what you've tried, what the result was and why this isn't your expected result. This is a very broad question.
Assuming that on clicking the clock button under the time section (on the right) you would want to set the selected time into the text field. You can simply call view.getParent() on click of the button and from the parent you can get the first child i.e child at 0 and set the text into the text field. This will work provided the button and textfield are within the same layout.
I'm using a ListView with a custom Adapter and custom item layout where every item has a description with a switch.
Depending on what the item is, I give the switch different thumb selector-drawables per code in getView().
swtch.setThumbDrawable(getContext().getResources()
.getDrawable(R.drawable.green_switch)); // or red_switch
This works fine, but:
When I scroll the ListView, the custom images are gone, until I touch a switch again, then the custom image of it comes back immediately.
When the state list contains an element of the “disabled” state at first, the switch will have this image instead of the image it should.
It's either a bug or an improperly use of the ListAdapter by me, I think.
A workaround for me would be to include switches with different styles in the layout and show one of them depending on the item type I have and hide the others.
Is this the only possibility?
Sound like you have problems with view recycling. You have to set all the fields in the getView each time.
Please read How ListView's recycling mechanism works. Understanding view recycling is very important when using ListViews.
Just like a ListView but instead it doesn't scroll. Its content is added programatically via an ArrayAdapter. Is there any view that can be used for this purpose?
PS: Not LinearLayout, since it doesn't get its content from an adapter (I want the observer pattern)
Edit: Let me explain a little bit more. Suppose you need a list of items, but the list itself is not scrollable, what is scrollable is the screen.
That being said, the list of items should show ALL items, not a limited amount based on a fixed height. The way to go is LinearLayout as it is both non-scrollable and shows all items within itself.
But there is a third requierement. You don't want to add(View) directly, but instead, you want something similar to an ArrayAdapter so that you have a control of the items and their position, so you can handle on item click events based on their position. As far as I know, this can't be done using a LinearLayout. So my question is, does any view exist for this purpose?
You could try using a ListView, but disable scrolling, as described here
Put your layout inside a ScrollView and yes you have to use a Linearlayout. You need to set tag for each of your view that you add dynamically and whenever your view is clicked you can get it's position by view.getTag() and perform the required operation.
Note : Adding views at run time may affect performance.
I want to create a gallery view, in which I want to select multiple images using an orange rectangular box.
It should look something like the following:
screen look like http://i.imgur.com/qAO0Q.jpg
Issues in your design:
Doing the selection by dragging the thumbs on either sides is going to be an extra headache. First of all, handling scrolling while doing selection. Secondly, handling touch/drag and deciding whether to include an item in the selection or not.
Design change:
Let me suggest a simpler way to extend your selection rectangle :
I assume the selection starts with a long click on any item. The orange selection rectangle appears over this initial item. Then a single tap on any neighboring item should include all items placed in-between the tapped and the initial item including the tapped one. That will make selection handling significantly simpler and less buggy.
Implementation:
Now this design shouldn't be too difficult to implement. Extend the Gallery widget and have members that indicate whether a selection is in progress and what is the range of selection (startIndex and endIndex) .
Override the onDraw method and draw the selection rectangle from startIndex to endIndex items.
Then you can define a custom adapter for your GalleryView, define a row XML layout file with ImageView and CheckBox and inflate this XML layout inside the custom adapter class. FYI, you will define custom adapter class by extending BaseAdapter class.
Check this example: Android custom image gallery with checkbox in grid to select multiple
What you could do:
Give ids all the images in the slide show
Use javascript to set
onclick events for the images Enable borders on the onclick event
Add to img tags
<IMG id=”1” namespace=”clicked” onclick="TriggerEvent(this)"></IMG>
Create script trigger event that changes the namespace of the selected id.
Include css to enable the borders:
.clicked{
border-style:solid;
border-width:3px;}
Actually I am able to use empty listview in my application when I am having Single list.But I am encountering with two different list which is using same xml.Now how should I proceed in order to get 2 different images to my different list when they are Empty,
If you are using imageview with id android.R.id.empty.
then simply use this
ImageView image=(ImageView)findviewbyid(android.R.id.empty);
and set the desired image.
Yep Akki that was almost correct with a little changes in my XMl.The Final code is here
All I had to use the same textview described inside the list xml file.
TextView tv=(TextView)findViewById(android.R.id.empty);
tv.setBackgroundResource(R.drawable.anniversary_reminder);
Set the visibility of whatever image you want to VIEW.GONE when your list is not empty. You can then create two different images in your layout. Have them initially set to visible.
In the onCreate method check the ArrayList you are using for your adapter when loading the list to see if it is empty. If it is not empty then set the visibility of your image to VIEW.GONE. Then all you have to do is do this same check whenever there are deletions to the list. (because you can't get an empty list by adding more entries) And whenever you make an addition to the list set the visibility of the image to VIEW.VISIBLE. (so basically wherever you do a notify of a data change on the adapter)
Hopefully this logic helps out!
ImageView emptyListImage1 = (ImageView) findViewById(R.layout.image1);
ImageView emptyListImage2 = (ImageView) findViewById(R.layout.image2);
emptyListImage1.setVisibility(VIEW.GONE);
emptyListImage1.setVisibility(VIEW.VISIBLE);