My app is using landscape full screen mode and the navigation drawer. I am using listView in my app along with an edittext. The edittext is the search bar that will search the listview. Both the listview and the edittext are in the navigation drawer. But, when there is no list item that matches the searched word, the listview gets empty.
So how can I add a "Item not found" message instead of the blank listview?
I searched a lot on the internet and found a method setEmptyView(); but I couldn't understand it and hence it is not working. Please help me! Maybe this question is already asked here but please give me an easy explanation.
Here is my code:
MainActivity.java
public class MainActivity extends FragmentActivity {
final String[] data = {"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Flourine","Noen","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium"};
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
final EditText searchBar = (EditText) findViewById(R.id.searchbar);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
navList.setAdapter(adapter);
searchBar.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
//the code below will automatically close the keyboard when the user will touch the listview
navList.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(navList.getWindowToken(), 0);
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
}
mainactivity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Swipe from the left to open the drawer"
android:id="#+id/textView"
android:layout_gravity="center" />
</FrameLayout>
<LinearLayout
android:id="#+id/left_drawer_layout"
android:layout_height="fill_parent"
android:layout_width="240dp"
android:background="#111"
android:orientation="vertical"
android:layout_gravity="start" >
<EditText
android:id="#+id/searchbar"
android:layout_width="230dp"
android:layout_height="40dp"
android:textColor="#bfc2d1"
android:singleLine="true"
android:padding="10dp"
android:background="#drawable/search_bar"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:imeOptions="flagNoExtractUi"
android:hint=" search" >
</EditText>
<TextView
android:id="#+id/notfound"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#android:style/TextAppearance.Medium"
android:gravity="center">
</TextView>
<ListView android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Yes you have mentioned about setEmptyView() correctly, you should use it if you would want to show empty message whenever ListView gets empty.
Now here is a xml layout and code depicts how to use setEmptyView exactly.
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/layoutTitlebar" >
<ListView
android:id="#+id/listViewFriends"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/friendBGColor"
android:cacheColorHint="#00000000" >
</ListView>
<TextView
android:id="#+id/empty"
style="#android:style/TextAppearance.Large"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="#string/strNoRecordsFound" >
</TextView>
</LinearLayout>
Now, you have to set this empty view (i.e. TextView) to ListView by using:
ListView listViewFriends = (ListView) findViewById(R.id.listViewFriends);
// set your adapter here
// set your click listener here
// or whatever else
listViewFriends.setEmptyView(findViewById(R.id.empty));
This method is all that you need
http://developer.android.com/reference/android/widget/AdapterView.html#setEmptyView(android.view.View)
Have in mind it accepts a view, so you have to inflate the layout and fill out any/all text you may have there by yourself.
EDIT:
Let's assume you have a layout named "empty_text" like this:
<?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="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="There are no entries in this list"
android:gravity="center"/>
</LinearLayout>
Hint: I burned in the string for example purposes, heed the IDE warning and use a string identifier for I18n's sake
Now you would use this code to make it all work with the ListView:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View emptyTextView = inflater.inflate(R.layout.empty_text, null, false);
listView.setEmptyView(emptyTextView);
This code assumes you are executing inside an Activity, but just in case you don't plan on pasting it on an Activity, any Context instance will work.
That should be it.
ListView lv = (ListView)findViewById(android.R.id.list);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
lv.setEmptyView(emptyText);
Then your listview will automatically use this view when its adapter is empty.
Detailed Solution:
Layout:
<ListView
android:id="#+id/listViewFriends"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/friendBGColor"
android:cacheColorHint="#00000000">
</ListView>
<TextView
android:id="#+id/empty"
android:text="#string/strNoRecordsFound"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#android:style/TextAppearance.Large"
android:gravity="center">
</TextView>
</LinearLayout>
Class File:
ListView listViewFriends = (ListView) findViewById(R.id.listViewFriends);
listViewFriends.setEmptyView(findViewById(R.id.empty));
Source
I have implemented search functionality in my listview which is in my navigation drawer. But currently it has only one textview. I want to be able to add two textviews on the same line without the need of a different class or adapter. Having said that, I still want to be able to search the first textview. So basically my question is : How do I add two textviews to a row in a listview without using any other class?
Here's my code:
listitem_row.xml
<!-- This is the xml which i use to display one textview on a list row-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
</LinearLayout>
main_activity.xml
<!--This is my main activity xml where I have the navigation drawer and the
listview inside the drawer-->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Swipe from the left to open the drawer"
android:id="#+id/textView"
android:layout_gravity="center" />
</FrameLayout>
<LinearLayout
android:id="#+id/left_drawer_layout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginLeft="-65dp"
android:background="#111"
android:orientation="vertical"
android:layout_gravity="start" >
<EditText
android:id="#+id/searchbar"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:textColor="#bfc2d1"
android:singleLine="true"
android:drawableLeft="#drawable/search"
android:padding="10dp"
android:background="#drawable/search_bar"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:imeOptions="flagNoExtractUi"
android:hint="Search" >
</EditText>
<RelativeLayout
android:id="#+id/empty"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginLeft="-65dp"
android:background="#111"
android:orientation="vertical"
android:layout_gravity="start" >
<TextView
android:id="#+id/empty_text"
android:text="#string/notfound"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textColor="#ffababab"
android:textSize="15dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:gravity="center">
</TextView>
</RelativeLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
//This is my mainactivity
public class MainActivity extends Activity {
final String[] elename ={"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Fluorine","Neon","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon","Caesium","Barium","Lanthanum","Cerium","Praseodymium","Neodymium","Promethium","Samarium","Europium","gadoliium","Terbium","Dysprosium","Holmium","Erbium","Thulium","Ytterbium","Lutetium","Hafnium","Tantalum","Tungsten","Rhenium","Osmium","Iridium","Platinum","Gold","Mercury","Thallium","Lead","Bismuth","Polonium","Astatine","Radon","Francium","Radium","Actinium","Thorium","Protactinium","Uranium","Neptunium","Plutonium","Americium","Curium","Berkelium","Californium","Einsteinium","Fermium","Mendelevium","Nobelium","Lawrencium","Rutherfordium","Dubnium","Seaborgium","Bohrium","Hassium","Meitnerium","Darmstadtium","Roentgenium","Copernicium","Ununtrium","Ununquadium","Ununpentium","Ununhexium","Ununseptium","Ununoctium"};
final String[] nos = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118"};
//I have already added the String elename to my listview
//and I want to add String nos to it without using any other class.
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//The line below adds the string elename to the listview
//but using the same line i want to add the string nos if possible.
adapter = new ArrayAdapter<String>(this, R.layout.listitem_row,R.id.textView1, elename);
final EditText searchBar = (EditText) findViewById(R.id.searchbar);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
navList.setAdapter(adapter);
navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
//Change view according to numbers
drawer.closeDrawer(linearLayout);
}
});
//filter list view after search instantly
searchBar.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
//The line below removes all the spaces while searching
String searchedquery = cs.toString().replaceAll(" ","");
//The line below searches the listview and shows the results
MainActivity.this.adapter.getFilter().filter(searchedquery);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
//hide the keyboard after search on touch list view
navList.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
//The code below hides the keyboard when the user touches the listview
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(navList.getWindowToken(), 0);
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
navList.setEmptyView(findViewById(R.id.empty));
}
}
For binding two TextViews you need to create Custom Array Adapter. Go to below links for better understanding:
https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
http://prativas.wordpress.com/category/android/custom-listview-in-android-using-custom-arrayadapter/
http://jmsliu.com/1390/rss-reader-app-android-tutorial-1-listview-and-arrayadapter.html
https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
Update:
ListView searching Functionality:
http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/
http://www.myandroidsolutions.com/2013/08/04/android-listview-with-searchview/
http://sunil-android.blogspot.in/2013/09/custom-listview-alertdialog-with-filter.html
http://android-solution-sample.blogspot.in/2011/10/android-search-in-custom-listview.html
Here are the ideas. Go to any links and customized as per your requirement.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>
</LinearLayout>