I'm trying to show a number of items on a listView but it's not showing on the screen.
It's just dummy information but it's still not working. I've tried to see a bunch of tutorials but found nothing.
Here is the xml file
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:fitsSystemWindows="true"
android:id="#+id/container"
tools:context="com.example.pedrofialho.musicristo.SearchActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="55dp" />
</RelativeLayout>
and here is the java code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_actvity);
container = (ViewGroup) findViewById(R.id.container);
LayoutInflater inflater = LayoutInflater.from(this);
View rootView = inflater.inflate(R.layout.listview_search,container,false);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
list = (ListView) rootView.findViewById(R.id.list);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
String[] items = {"Comunhão","Passo a Passo","Tudo bem?","Esta lista não tem sentido"};
mArrayList = new ArrayList<>(Arrays.asList(items));
mArrayAdapter = new ArrayAdapter<>(this, R.layout.activity_search_actvity, R.id.list, mArrayList);
list.setAdapter(mArrayAdapter);
}
On the screen it shows nothing just a white screen.
Thank you in advance
Here is the proper way of ArrayAdapter initialization. You have to provide a layout containing eg. a TextView (if you want to show texts), so you should modify the line with new ArrayAdapter... to something like this: mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mArrayList);, because now your adapter gets the layout of your activity, not an item layout...
If you have to use a custom layout for each item, you should pass its id, and also the id of the TextView.
If you need a more complex item view, you can extend either ArrayAdapter or BaseAdapter classes to achieve the creation of item views.
View rootView = inflater.inflate(R.layout.listview_search,container,false);
inflates a new screen, which is not at screen until you put it explicitly, even adding it to a container part of the current View hierarchy or replacing the current content (calling again setContentView). Accordingly to the layout you posted, activity_search_actvity.xml I assume, contains already a ListView, so get rid of the inflater, and simply use findViewById to retrive the object part of the layout you set with setContentView(R.layout.activity_search_actvity);. You also have instantiated the ArrayAdapter with the wrong parameters. The second parameter is the layout you want to use a list's item and the third is the id of a TextView you want to use to show your dataset.
You are passing wrong parameter to array adapter. In Your case it will be as ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mArrayList)
For details ArrayAdapter in android to create simple listview
Use app:layout_behavior="#string/appbar_scrolling_view_behavior" in Relative layout.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="55dp" />
</RelativeLayout>
As explained by the support library guidelines:
Use this
ArrayAdapter <String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.array);
Don't forget to attach the list view to it.
ListView listView=getListView();//Your activity shoudl extends ListActivity
listView.setAdapter(adapter);
Related
I have red many solutions regarding this but none of them solved my problem.
What i want to do is, when my list view is empty its ok if the default empty text view shows up (which is declared in xml) but what if i want to change its text at some point in an activity that extends listfragment according to my need and make it visible....
Recently i tried this:
public void setEmptyText(CharSequence emptyText) {
ListView mListView = (ListView) NotificationsView.findViewById(android.R.id.list);
View emptyView = mListView.getEmptyView();
((TextView) emptyView).setText(emptyText);
}
It doesn't give any error but it is not solving the problem too. Any Help would be appreciated thanks
EDIT:
here is my layout of listfragment
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/fragment_bg"
android:divider="#null"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_style" >
</ListView>
<TextView
android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/fragment_bg"
android:text="No data" />
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
If you are using ListFragment all you need to do is call the method
setEmptyText("Your empty text"):
The system will recognize the view label with the android:id="#id/android:empty" id and will change the text for you. No need to reference it or do any cast, it's all built in the ListFragment
By default the listview or adapter (don't remember which one) makes it invisible. Call emptyView.setVisibility(View.VISIBLE); to make it visible.
This is how to change the value of a text view:
TextView tv = (TextView) findViewById(R.id.empty);
tv.setText('yourText');
I want to allow my ListView to be wrapped to its content on its width and making it aligning to the parent centre.
This is what a normal ListView is giving me
This is almost what i want to achieve, only that i want the ListView to wrap by the longest test in the ListView. Currently this is achieved by declaring the dp which is something I would like to avoid if possible
Some codes for first image:
<ListView
android:id="#+id/lst_test"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content" >
</ListView>
Some codes for second image:
<ListView
android:id="#+id/lst_test"
android:layout_width="200dp"
android:layout_height="wrap_content" >
</ListView>
Do I have to use custom adapter for this or I can stick with the current one? If I would need to use custom adapter, how do i go about doing this?
You don't need to create custom adapter. Here is an example which can help you.
public class MainActivity extends Activity {
HashMap <String, CheckBox> options = new HashMap<String, CheckBox>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list_view = (ListView) findViewById(R.id.listView1);
ArrayList<String> str = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
str.add("Example " + i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_layout, R.id.textView1, str);
list_view.setAdapter(adapter);
}
}
Above I have created an activity in which i am getting listview and setting data using ArrayAdapter.
I have created two layouts :
First layout: Contains listview.
Second layout: Contains textview which need to display.
Main logic to align data at center of Listview is you need to set gravity of textview as center.
You can manage second layout according to your needs.
Here is my first XML layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
Here is my second layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="TextView" />
</RelativeLayout>
If you are still facing any problem then you can ask. If you need more flexibility then you can create custom adapter by inheriting ArrayAdapter class.
Here is an example of custom adapter: How do I link a checkbox for every contact in populated listview?
Hope it will help you.
I can't seem to find a way to make a layout that contains a ListView that show the entire list of my contacts (its a long list), with 2 buttons one next to the other below the list.
its freaking me out, nothing i do seems to work. i searched some related quiestions and found this one: Android Layout with ListView and Buttons
but none of the alternative solutions over there is working for me.
when i look at the graphical presntation (of eclipse) it looks fine, but when i run it on my device i can only see the list of contacts. the listView overlaps the buttons.
I am using a simpleList that render on another layout that represent a row. i'm starting to suspect something i do programtically ruins it, because i ran over my java code and found no mention of the activity_contact_list that i was talking about. only connection to the row layout.
so i got this activity_contact_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_alignParentTop="true" >
</ListView>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="1.0"
android:orientation="horizontal" >
<Button
android:id="#+id/action_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancle" />
<Button
android:id="#+id/action_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
</LinearLayout>
</LinearLayout>
this is after many tries such as: using RelativeLayout instead of the LinearLayout, puting the ListView's code below the LinearLayout of the buttons, and many more..
and here is the code of the activity: ContactList.java:
public class ContactList extends ListActivity{
//some global variables for late use.
public Model model;
SimpleAdapter adapter;
List<Map<String, String>> data; /* data is a list of Map */
String[] from = {"name","phone"}; /* names of the columns */ //TODO - add date!
int[] to = {R.id.name, R.id.number};
List<Contact> contacts; /* list of contacts */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_contact_list);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayHomeAsUpEnabled(true);
model = Model.getInstance(this);
data = new ArrayList<Map<String, String>>();
contacts = fillContactsList();
fill_data();
adapter = new SimpleAdapter(this, data, R.layout.contact_list_row, from, to);
setListAdapter(adapter);
}
i commented the setContentView line cause it doesnt compile otherway...
As is said. the list works perfactly. but it overlaps the buttons at the bottom of the activity. any idea? i'm sure its something silly i'm missing..
thanks in advance!
Did you try to use Relative Layout ?
Change Linear Layout to Relative Layout that's all.
As Linear Layout Doesn't support align parent..
Edited
set List Height to mach parent.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_below="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="#+id/action_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancle" />
<Button
android:id="#+id/action_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
</LinearLayout>
How long is your ListView. Maybe it is longer than your screen and is the LinearLayout with buttons shown outside your screen.
What happens when you rearrange with the buttons above the ListView.
EDIT:
I think you have to set a Max-size in your ListView. Maybe you can try something like this in your java-code, but I don't know if it is scrollable then...
ListView listView = (ListView) findViewById(R.id.listView):
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) listView .getLayoutParams();
params.height = 200; // or something bigger / smaller
listView .setLayoutParams(params);
<TextView
android:id="#android:id/empty"
style="#style/FacebookFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/no_result_found"
android:textColor="#color/white" />
this is the xml code for empty view. I am setting empty view as follows:
list.setAdapter(adapter);
list.setEmptyView(findViewById(android.R.id.empty));
I have the items in the listview even then also before listview is populated the empty view is shown.
I don't want that empty view to be shown when listview contains some item.
Please help.
if you are using ListActivity then no need to set the empty view manually. Remove list.setEmptyView(findViewById(android.R.id.empty)); from your code.
Change your layout like below then it's automatically add EmptyView if list has no item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
You have to set the empty view before setting the adapter, if your MainActivity is not extending ListActivity.
Try this
list.setEmptyView(findViewById(android.R.id.empty));
list.setAdapter(adapter);
Sorry for the late answer!
Alternate method: Make your activity extend from ListActivity. And add a textview to your list layout with id android.R.id.empty
Try this
View view = getLayoutInflater() .inflate(<resource id of the empty view>, null);
ViewGroup viewGroup= ( ViewGroup)list.getParent();
viewGroup.addView(view);
list.setEmptyView(view);
list.setAdapter(adapter);
There are some correct answers, however, I think this is the best approach I've found:
View emptyView = getLayoutInflater().inflate(R.layout.empty_list_cart, null);
addContentView(emptyView, listView.getLayoutParams()); // You're using the same params as listView
listView.setEmptyView(emptyView);
listView.setAdapter(adapter);
Whatever #dhawalSodhaParmar has explained is correct. But there is a confusion when you look at the way the answer is put down.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SampleActivity">
<ListView
android:id="#+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:divider="#null"
android:dividerHeight="0dp" />
<!-- Empty view is only visible when the list has no items. -->
<TextView
android:id="#+id/empty_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="NO DOCUMENTS AVAILABLE!"
android:textColor="#525252"
android:textAppearance="?android:textAppearanceMedium"
android:visibility="gone"/>
</RelativeLayout>
Suppose if you already have a list view and adapter backing the same with data. And assuming everything is working fine, whoever wants to implement this empty list view screen has to start like below:
Step 1:
Modify the existing Activity XML file which has the list view with the one I have used here. You can use either a Linear Layout or Relative Layout, but preferably Relative because of the flexibility. And a textView like I have written.
Step 2:
Go to your Activity Java file, and after your setContentView
ListView emptyListView = (ListView) findViewById(R.id.list);
// set your adapter here
// set your click listener here
// or whatever else
emptyListView.setEmptyView(findViewById(R.id.empty_text_view));
That's it, now run and you are set!
// first of all create a global variable of Text view
// which is displayed when the list is empty
private TextView mEmptyStateTextView;
//then in onCreate(Bundle savedInstanceState)
/Setting empty text view
mEmptyStateTextView=(TextView) findViewById(R.id.emptyview);
view.setEmptyView(mEmptyStateTextView);
//then in onLoadfinished method
mEmptyStateTextView.setText(R.string.no_earthquakes);
I have a ListView that I am creating. The content is set from a separate adapter class. When the adapter returns zero items I want the empty view to display from a separate xml. This is happening inside a SherlockFragment. However for some reason I cannot get the emptyView to display.
Help Please?
Code:
RelativeLayout fragView = new RelativeLayout(getActivity());
ListView listView = new ListView(getActivity());
listView.setEmptyView(getActivity().findViewById(R.id.emptyView));//This is having no effect
scoreViewAdapter = new ScoreViewAdapter(getActivity(),
databaseHelper.getAllTimeRecords());
listView.setAdapter(scoreViewAdapter);
if (scoreViewAdapter.getCount() < 1)
listView.//can I set something here to force or change my view? From the methods available to ListView I don't see any potential solutions..
fragView.addView(listView);
Here is the xml....pretty standard.:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/emptyView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/emptyText"
android:src="#drawable/ic_android_logo" />
<TextView
android:id="#+id/emptyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical"
android:text="No Items Found"
android:textAppearance="?android:attr/textAppearanceLarge" />
Thanks to Faizan's feedback to lead me here. Here is the solution:
View empty_view = inflater.inflate(R.layout.empty_view, null);
...
if (scoreViewAdapter.getCount() < 1)
fragView.addView(empty_view);
fragView.addView(listView);
You have to inflate that xml using LayoutInflater and then call
listView.setEmptyView(inflated_view);
You can get get the inflater from the onCreateView overrided method anc do
View inflated_view = inflater.inflate(R.layout.my_empty_layout,null);
I hope it will resolve the problem.
I believe you extends FragmentList and not ListActivity
so you actually can use setEmptyView