I have a listview inside of a viewpager. I set an empty view on the listview. If this listview starts empty, when i add something to it then right or left swipes on the listview won't switch between tabs. If the listview starts with something (not empty) when i set the adapter on it, then i can swipe. If i remove the setEmptyView on the listview, it always works whether it starts with data or without it.
How can i set an empty view and still get the listview to swipe between tabs?
This is how i set the empty view:
mListview.setEmptyView(findViewById(R.id.empty_view));
the empty view is a textview directly below the listview in a linearlayout
the empty view xml:
empty_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/empty_list"
android:textColor="#color/lighter_gray"
android:layout_gravity="center"
android:gravity="center"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/no_item"
android:textStyle="italic">
</TextView>
I would suggest removing your empty view from the current layout file, and move it into its own dedicated layout file. Then inflate this new layout in your class when you want to use it as an empty view. The end result would be something like this (not compiled, coding from memory):
final View v = LayoutInflater.from(context).inflate(R.layout.empty_view, null);
mListview.setEmptyView(v);
or maybe
final View v = LayoutInflater.from(context).inflate(R.layout.empty_view, mListview, false);
mListview.setEmptyView(v);
Related
I want to add a title bar in the list view.
I have added a text view inside the listview which can act as title:
Layout File :
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/layout_rounded"
android:layout_marginRight="10px"
android:layout_marginLeft="10px"
android:layout_marginTop="30px"
android:layout_below="#id/linlaypay"
android:orientation="vertical"
android:padding="10dip" >
<ListView
android:id="#+id/rectran"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout >
Here is the code used to add the title listview dynamically:
listView = (ListView) findViewById(R.id.rectran);
TextView textView = new TextView(getApplicationContext());
textView.setText("header view");
listView.addHeaderView(textView);
The problem is ,an emty text view isadded to the listview without the text "header view".
Why?Am I missing anything?
I don't believe you can just add a view like this. The view being passed into listView needs to be inflated.
create a header.xml layout file with the text view in it with the text set.. and then run the code below.
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(header, null, false);
If I were you, I would add a title to the ActionBar, assuming that your ListView is the whole Activity. If it isn't the whole Activity or it doesn't take up the whole screen, you could add the title to the layout rather than dynamically adding it.
http://developer.android.com/reference/android/app/ActionBar.html
Check out the Android Developers documentation on the ActionBar
My activity holds 3 small listviews and I wanted to give title to each of the view by adding textview.
However the text in the Textview was not visible because the size of the text was too small.
setting the text size of the textview resolved the issue.
Thanks !!
I have a listview filled with data
at some point the user can wipeout that data and reveal the emptyview that i set up.
I want the listview to vanish from the screen animated, revealing the empty data.
How do that ?
you can use the empty view. Simply add a view with #android:id/empty to your layout and when the dataset is empty it will be automatically show. For instance:
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I'm populating an activity with a ListView that sometimes exceeds the window size vertically. Instead of the user being able to scroll the ListView, I want the ListView to just take up the space it needs and let the users scroll in the activity instead. Is this possible?
Here is an image for clarification: http://imgur.com/1I2bc
On both sides there is a ListView containing 8 elements, only the right one takes up the space it needs to show the list fully though, pushing the other views down and making the entire activity scrollable.
You need to use the methods addHeaderView and addFooterView to add views before and after the ListView.
To add several views, first put the views in a Layout or ViewGroup and add that ViewGroup as the header or footer view (ViewGroup is a subclass of View).
-have you tried to put your scroll element as top level element in your layout .xml?
Something like this:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="#+id/listView1"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</ListView>
</ScrollView>
I am using addHeaderView to add a view item to the top of a ListView. I also have a TextView to display a message saying there are no items in the list.
Here is the layout:
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/list_empty"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
And the Java code:
final ListView listView = getListView();
final View view = getLayoutInflater().inflate(R.layout.list_item_add,
listView, false);
listView.addHeaderView(view, null, true);
When there are items in the ListView then the header is shown but if I delete all items in the list (except the header view) then the header view disappears.
I would like the header view to be visible in the list view whether there are items in the list or not.
Thanks,
Remove the #android:id/empty view from your layout, or override/subclass your adapter to return false from isEmpty()
From my experience (SDK version 10):
Overriding isEmpty() in the adapter makes it work.
Then, it is optional to remove the #android:id/empty view.
I want to create a custom view (extends View) which contains an image and a text.
Is it possible to set an listener just for image if i use canvas.drawBitmap(imageBitmap, ...) method?
Thank you!
If you want your view just to show an image and a text, why not create a specific layout in your XML file and use setContentView to display the view?
If thats what you were looking for, I think that is the easiest approach. XML example coming soon.
Here is the contents of my personal view to display my main application, just to demonstrate how I can switch view's, your view would be different including a TextView and an ImageView.
dblist.xml inside of res/layout folder.
<?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:background="#FFFFFF">
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"/>
</LinearLayout>
This contents display my database results onto a Spinner
I can call that view by using:
setContentView(R.layout.dblist);
And if I want to return to my main layout, I can switch the view by calling
setContentView(R.layout.main);
EDIT:
You can manipulate the view you are working with programmatically by using findViewById method. Let's use my dblist.xml file as an example, notice that my ListView has an id of list. So I would call:
ListView myList = (ListView) findViewById(R.id.list);
With corresponds to that layout, so whenever I want to change anything to my XML file, I would reference myList use methods and callbacks to meet my needs.
I hope this helps.