ListView item not found - android
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
Related
Can we add multiple listviews in a single layout
I have Three list to display.How to add them on single layout? my layout file is <?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="fill_parent" android:orientation="vertical" > <ListView android:id="#+id/list1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ListView android:id="#+id/list2" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ListView android:id="#+id/list3" android:layout_width="match_parent" android:layout_height="wrap_content" /> I am using CustomAdapter for each list mylistview1 = (ListView) findViewById(R.id.list1); mylistview2 = (ListView) findViewById(R.id.list2); mylistview3 = (ListView) findViewById(R.id.list3); //1 CustomAdapterA adapterA = new CustomAdapterA(this, rowItems1); mylistview1.setAdapter(adapterA); mylistview1.setOnItemClickListener(new OnItemClickListener() { #Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { String member_name = rowItems1.get(position).getMember_name(); Toast.makeText(getApplicationContext(), "" + member_name, Toast.LENGTH_SHORT).show(); } }); //2 CustomAdapterM adapterM = new CustomAdapterM(this, rowItems2); mylistview2.setAdapter(adapterM); mylistview2.setOnItemClickListener(new OnItemClickListener() { #Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { String member_name = rowItems2.get(position).getMember_name(); Toast.makeText(getApplicationContext(), "" + member_name, Toast.LENGTH_SHORT).show(); } }); //3 CustomAdapterS adapterS = new CustomAdapterS(this, rowItems3); mylistview3.setAdapter(adapterS); mylistview3.setOnItemClickListener(new OnItemClickListener() { #Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { String member_name = rowItems3.get(position).getMember_name(); Toast.makeText(getApplicationContext(), "" + member_name, Toast.LENGTH_SHORT).show(); } }); I tried with putting each layout in separate Linear Layout but didnot worked, can anyone suggest what should be done here???
Try making making each of the ListViews take up exactly 1/3 of the screens height and see if they're visible then. <ListView android:id="#+id/list1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1 /> <ListView android:id="#+id/list2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1 /> <ListView android:id="#+id/list3" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1 />
Initially a listview looks like it occupies the whole screen. You have to set their widths accordingly to see them in one screen.
If your idea is to finish one list the next one will be visible the answer is: No, you cannot do that. On your adapter you should Override getViewTypeCount() and return 3 and then generate the correct view for each position. If you're idea is to have each list occupying 1/3 of the screen you must use the layout_weight parameter in your XML like this: <?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="fill_parent" android:orientation="vertical" > <ListView android:id="#+id/list1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <ListView android:id="#+id/list2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <ListView android:id="#+id/list3" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" />
two textviews listview android
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>
ListView Item not clickable
im trying to make an activity that searches a DB based on an input string and then returns the results in a list view. All this is working however, My listview items are not clickable. I have tried changing the focus and clickable attributes but nothing has worked so far. Main XML (add_friend_layout.xml): <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="#+id/AddFriendHeaderLayout" android:layout_marginTop="3dip" android:layout_height="45dip" android:orientation="horizontal" android:layout_width="fill_parent" android:gravity="center"> <EditText android:id="#+id/et_SearchFriend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:ems="10" android:inputType="text" /> <Button android:id="#+id/bt_SearchFriend" android:layout_width="wrap_content" android:layout_height="40dip" android:focusable="false" android:icon="#android:drawable/ic_search_category_default" android:text="Search" > </Button> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="60dip" android:layout_marginBottom="5dip" android:layout_alignParentBottom="true" android:layout_above="#+id/AddFriendHeaderLayout" android:id="#+id/searchFriendFooterLayout"> <ListView android:id="#android:id/list" android:layout_width="match_parent" android:clickable="true" android:layout_height="wrap_content"> </ListView> </LinearLayout> </RelativeLayout> ListView XML (single_listview_layout.xml): <?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="wrap_content" android:clickable="true" > <TextView android:id="#+id/tv_list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:textSize="20sp" > </TextView> </LinearLayout> Main Java Function (SearchFriendActivity.java): public class SearchFriendActivity extends ListActivity { //Progress Dialog private ProgressDialog pDialog; private Button bt_searchFriend; private EditText et_Search_String; private ListView tv_listview; String[] arrFriends; String[] arrUserId; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try{ setContentView(R.layout.add_friend_layout); bt_searchFriend= (Button) findViewById(R.id.bt_SearchFriend); et_Search_String = (EditText) findViewById(R.id.et_SearchFriend); tv_listview = (ListView) findViewById(R.id.tv_list); String searchString=""; //searchString = String.valueOf(et_Search_String.getText()); Log.i ("log_search","value of searchString: " + searchString); bt_searchFriend.setOnClickListener(new OnClickListener() { #Override public void onClick(View view) { try{ searchFriends aTask = new searchFriends(); aTask.execute(String.valueOf(et_Search_String.getText())); String rResult = aTask.get(); // Convert aResult to array arrFriends = convertJSONData(rResult); arrUserId = convertJSONDataToUserArray(rResult); setListAdapter(new ArrayAdapter<String> (getBaseContext(), R.layout.single_listview_layout, R.id.tv_list, arrFriends)); }catch(Exception e){ Log.e ("log_search_load",e.toString()); } } }); tv_listview = getListView(); //handler for List Item click tv_listview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // When clicked, show a toast with the TextView text Toast.makeText(getApplicationContext(), "Friend request sent for " + ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); Log.i("log_search_listclick", "Listview clicked"); } }); }catch(Exception e){ Log.e ("log_search_load2",e.toString()); } }
Firstly, don't use focusable and clickable. When you use setOnclick or another they are auto creating. Secondly, remove try cache blog then start application. Because when the problem created program will contining and you can't understand where is the problem. ===>Thirdly<==== you are used (tv_listview=...) second twice tv_listview = (ListView) findViewById(R.id.tv_list); tv_listview = getListView(); then you are trying tv_listview.setOnItemClickListener..... If second view is not first view seton not working on "R.id.tv_list".
Android setOnclick listner to listview
Actually i have a viewpager that holds a Imageview and a listview over it, So when i click on the Listview i need to hide the listview and the bottom bar to make the ImageView fullscreen for all the views in side the viewpager. And onclick of the image view get back the views. But my issues are: I cant implement onClickListner on Listview, to make the whole view to handle the tap. Am forced to use the onItemclickListner and it only hides when you click on the item(which is not desired). When i click Listview(which is inside Fragment) inside the pager it doesn't hide for all the views except for the currentview, But am able to hide the actionbar and the bottom bar(on the Fragment Activity). a screen shot for reference: How can i achieve this or this is there any better way that i can hide the views. Here is my ListView in side a Fragment: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="#+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" /> <ListView android:id="#+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#android:color/transparent" android:cacheColorHint="#android:color/transparent" android:divider="#android:color/transparent" android:listSelector="#android:color/transparent" android:scrollbars="none" /> </RelativeLayout> Here's my Fragment Activity layout: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/detailLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="#+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="#+id/form" > </android.support.v4.view.ViewPager> <RelativeLayout android:id="#+id/form" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" android:padding="2dp" > <EditText android:id="#+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_toLeftOf="#+id/btnSend" android:drawableRight="#drawable/edit_keyboard" android:inputType="textMultiLine" android:maxLines="3" /> <ImageButton android:id="#+id/btnSend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="#drawable/send" /> </RelativeLayout> </RelativeLayout> Here am successful with the onitemclicklister to the ListView: listComments.setOnItemClickListener(new OnItemClickListener() { #Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub if (getSherlockActivity().getSupportActionBar().isShowing()) { getSherlockActivity().getSupportActionBar().hide(); listComments.setVisibility(View.GONE); form.setVisibility(View.GONE); } else { getSherlockActivity().getSupportActionBar().show(); listComments.setVisibility(View.VISIBLE); form.setVisibility(View.VISIBLE); } } }); imageView.setOnClickListener(new OnClickListener() { #Override public void onClick(View v) { // TODO Auto-generated method stub getSherlockActivity().getSupportActionBar().show(); listComments.setVisibility(View.VISIBLE); form.setVisibility(View.VISIBLE); } });
Could you set the clickable property for the listView and check ? Also, whenever you add a new edittext to the do you add it dynamically ? If so that would mean that the you are adding items to the listView dynamically. The listView would not know if you have clicked on an item of the view itself hence a callback would get registered when you click the item as that takes preference.
Can't click on ListView whose rows are made of WebViews
I have an Activity which extends ListView The ListView that I use is an extension of ArrayAdapter and each row of the ListView primarily consists of a WebView The problem is that touching the WebView does not call OnListItemClick. Any thoughts on how I can be able to touch the webview and call OnListItemClick? Or is there something else that I'm doing wrong? Thanks! Here is a skeleton of the Activity class, along with the relevant XML files for the layout PS, I've tried setting android:clickable="false" in my layout, but this doesn't let me click on the rows of my ListView. Interestingly, I can still scroll through the list, but I just can't click! public class ListOfItemsFromTopicActivity extends ListActivity { private Topic t; #Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.items_from_topic_skeleton); TopicFactory tf = new TopicFactory(); t = tf.build_topic(3); ListView itemlist = getListView(); ListOfItemsAdapter adapter = new ListOfItemsAdapter(this, R.layout.items_from_topic_row, t.getItems()); itemlist.setAdapter(adapter); } protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); // do some stuff that never gets done } private class ListOfItemsAdapter extends ArrayAdapter<Item> { private ArrayList<Item> items; private int text_view_resource_id; private LayoutInflater layout_inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); public ListOfItemsAdapter(Context context, int a_text_view_resource_id, ArrayList<Item> some_items) { super(context, a_text_view_resource_id, some_items); this.items = some_items; this.text_view_resource_id = a_text_view_resource_id; } #Override public View getView(int position, View convertView, ViewGroup parent) { View v = layout_inflater.inflate(this.text_view_resource_id, null); Item o = items.get(position); WebView page = (WebView)v.findViewById(R.id.item); page.loadDataWithBaseURL("fake://a/bcde", o.getShort_title(), "text/html", "utf-8", ""); return v; } } } items_from_topic_skeleton.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" > <TextView android:background="#drawable/blackgrad" android:gravity="center" android:id="#+id/section_title" android:layout_height="50dip" android:layout_width="fill_parent" android:textColor="#ffffff" android:textSize="18sp" /> <ListView android:background="#ffffff" android:cacheColorHint="#ffffffff" android:id="#android:id/list" android:layout_height="fill_parent" android:layout_width="fill_parent" /> </LinearLayout> items_from_topic_row.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:padding="5dip" > <LinearLayout android:gravity="center" android:layout_height="60dip" android:layout_width="fill_parent" android:orientation="horizontal" > <LinearLayout android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent" android:minHeight="55dip" android:orientation="horizontal" android:padding="5dip" > <WebView android:id="#+id/item" android:layout_height="fill_parent" android:layout_width="fill_parent" android:scrollbars="none" /> </LinearLayout> <ImageView android:id="#+id/disclosure" android:layout_height="29dip" android:layout_width="29dip" android:paddingRight="5dip" android:src="#drawable/disclosure" /> </LinearLayout> </LinearLayout>
If you add a focusable element to a ListView, it causes the ListView items to no longer be selectable. Try setting android:clickable = false on your WebView.
Just make your onTouch event to do whatever you want. e.g: My list item consists of TextView, WebView and Linear layout: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="66dp" android:orientation="vertical" android:id="#+id/layout"> <TextView android:layout_width="wrap_content" android:id="#+id/textView" android:layout_height="wrap_content" /> <WebView android:id="#+id/webvie" android:layout_width="match_parent" android:layout_height="wrap_content"> </WebView> </LinearLayout> and I want to perform on click action - on the whole listitem. holder.layout.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View view) { //do stuff } }); But when I run my app, only texview is clickable. What to do? Set up onTouch listener on webView: holder.webView.setOnTouchListener(new View.OnTouchListener() { #Override public boolean onTouch(View v, MotionEvent event) { holder.layout.callOnClick(); return true; } }); supports only API 15+ tough...