My goal is to make a ListView of each row of which contains one single button which occupies the entire space in each row. Below is the code where the onItemClick method does not work.Does anyone have idea why it does not work?
I have next class Activity:
public class MyActivity extends Activity implements OnItemClickListener {
public void onCreate(Bundle savedInstanceState) {
//
//Here is a lot of code..
//.....
List<String> items = new ArrayList<String>();
ListView lv = (ListView) findViewById(R.id.listView);
if (result.getItems().size() > 0) {
// Init list view
lv.setVisibility(lv.VISIBLE);
lv.setTextFilterEnabled(true);
lv.setAdapter(new buttonAdapter(this, R.layout.list_item,
items));
lv.setOnItemClickListener(this);
}
}
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
//....
}
}
And here is my XML-element for each row in the ListView:
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item_button"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textStyle="bold"
android:textSize="16sp">
</Button>
onItemClick only fires when the item itself is clicked. If you had a TextView or a TextView and an ImageView instead of a Button, clicking the item then will trigger onItemClick.
If you want to respond to a button click, you can set up an onClickListener for each button in the adapter. Then use the view references passed to the listener to distinguish between items.
However, if you do want the onItemClick to be triggered, you will need to add the property
android:descendantFocusability="beforeDescendants"
to the ListView tag in the layout XML.
This lets the ListView handle the click first.
As to why you want a button that fills an entire listitem instead of letting the listitem be clickable itself, that still doesn't quite make sense. You should do one or the other, since whatever you do, only one of them will actually be clicked.
If the button you want to place on the listview item occupies the whole item width and height the onItemClickListener will not be envoked since there is no content space to click/touch. You can implement the onClickListener() (or onTouchListener()) for the button. But why button, you can do the same without button but just with click on the listview item as it is.
Related
I am using listviews instead of button in my application. I want to set OnClickListener() on listview instead of setOnItemClickListener().
here is my code:
listview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), BillingActivity1.class));
}
});
can someone suggest a method to do OnClickListener()?
thank you
Ok, I understood your problem.
Lets analyze cases:
Listview with items
If your ListView has some items, the good approach is to set anyway an onItemClickListener and, based on which item has been clicked, do something. You can also do the same thing for each item without considering which item has been pressed, but this is still the best approach.
ListView with no items
From Docs:
the list view will be hidden when there is no data to display.
ListView (usually) has the height set to wrap_content, so even setting the onClickListener on an empty list won't work since the list will result having height of 0, being not able to be clicked (you can't click a view with no height since it is not visible).
Perform actions on an empty ListView
If, as it looks like, you need to do some stuffs on your ListView even if it's empty, just add a Button or a FloatingActionButton to your Activity and then use those: you can both keep the button in any case (like an "Add item" Button) or you can make it visible only if the ListView is empty. something like:
xml
<Button
android:id="#+id/buttonEmptyListStuffs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List is empty, click me!"
android:visibility="gone"/>
activity
//init the button and do other stuffs
...
buttonEmptyListStuffs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//go to activity 2
}
});
...
List<MyListViewItem> myListViewItems = //init your list of items for the listView
buttonEmptyListStuffs.setVisibility(myListViewItems.size() > 0 ? View.GONE : View.VISIBLE);
...
Note: I wrote this code by hand without compiler so it might not be perfect, just take the concept behind it
As Pskink said from comments
I forgot to mention that ListView has setEmptyView(View) which allows you to set a custom layout for the Listview if it is empty. Refer to his link for a good tutorial
USe this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
startActivity(new Intent(getApplicationContext(), BillingActivity1.class));
}
});
If you really want to handle click on any Point of your listView. You can put the list inside a FrameLayout or LinearLayout and then add your onClickListener on the layout.
findViewById(R.id.my_parent_list_layout).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// DO your stuff
}
});
If your list item count is empty the dynamaically change the visibility of the list view with a button(switching the visibility with list view and button) and give the click action to the button.
I want to be able to click on every item on my list, and then move to another activity (this I know how to do)
And also when I click (exactly on the checkbox) then it should be clicked, because now, wherever I click on the item line, the checkbox is getting clicked.
I also want, once I click on any of the checkboxes, to get a count, because I want to put a progress bar, to show how far my students went in my book in the app,
String[] items = getResources().getStringArray(R.array.chapters);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,android.R.id.text1, items);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
CheckedTextView check = (CheckedTextView)view;
check.setChecked(!check.isChecked());
}
});
You are now checking the checkbox on the listView item click, if you want to do it on the checkbox click you should make your custom adapter instead of working with the default adapter.
Try to add checkbox in separate view of list or list item. For ex. if you have RelativeLayout then you can add your checkbox toLeftOf your listview that way your checkbox won't get clicked as you click on list item.
So I guess it's not possible to make it get clicked when only clicking on it,
So I will try to make separate check boxes, as you suggested,
Any code idea for how to get all the check boxes values (count) so I can make a g
This is the situation: I have a Listview with some items. Inside each item there are two clickable views that are only shown after user clickes in their parent item.
It works like this:
productList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, long id) {
TextView txtAdd = (TextView) view.findViewById(R.id.txt_add_units);
txtAdd.setVisibility(View.VISIBLE);
txtAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
...
}
});
TextView txtRemove = (TextView) view.findViewById(R.id.txt_remove_units);
txtRemove.setVisibility(View.VISIBLE);
txtRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
...
}
});
basketProductsListAdapter.notifyDataSetChanged();
}
});
The first time a listview item is clicked, its txtAdd and txtRemove child views are shown and then if user clicks them, code inside their onclickListener is executed.
The problem appears ONLY IN THE FIRST listview item. First time it is clicked txtAdd and txtRemove are shown, BUT THE FIRST TIME user clicks over the child views, their parent's ItemClickListener is fired again, and then the child views (txtAdd and txtRemove) are not receiving the clickListener. Following times they are work perfectly.The weird thing is that it only happens first time on the first item in the listView.
Any ideas? thanks in advance!
Add this line to your listview xml android:descendantFocusability="blocksDescendants"
The Thing is our child view may be focusable so the first click will be for the focus and after that the itemClick will work. to avoide focusable add that line in the xml
Still if it does not work then try adding
android:focusable="false"
android:focusableInTouchMode="false"
to the child View of the listview
My application consists of 2 activities.The first activity contains a TextView,if you click on it, you move to the second activity that consists of a ListView and a button Done. The ListView contains TextViews with a CheckBox. If you click on button then it finish activity and returns selected text item. If you go back to the list view the selected checkboxes restored.
Code would be appreciated.
First of all I suggest you to use CheckedTextView control instead of a CheckBox & TextView. CheckedTextView serves as the combination of checkbox and textview and is easy to handle and implement.
Secondly, you should have an ArrayList of boolean of the exact size as the ListView no. of Items. Then you can set the ArrayList items accordingly in the OnListItemClick function of ListView. At any time and anywahere in your code, you can get the reference of your selection of the ListView. Its simple and efficient.
Here is a sample OnListItemClick code:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
arrCheckBox.set(position, !arrCheckBox.get(position));
CheckedTextView ctvListItem = (CheckedTextView)v.findViewById(R.id.ctvCustomLVRowID);
ctvListItem.setChecked(arrCheckBox.get(position));
}
Here arrCheckBox is a boolean ArrayList which is keeping record of our selection and size of this array is same as no. of ListItems. I hope now you can figure it out.
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="#+id/ListView01" android:layout_height="wrap_content"
android:layout_width="fill_parent"></ListView>
</LinearLayout>
Java code:
public class ListViewMultipleChoiceExample extends Activity {
private ListView lView;
private String lv_items[] = { "Android", "iPhone", "BlackBerry",
"AndroidPeople", "J2ME", "Listview", "ArrayAdapter", "ListItem",
"Us", "UK", "India" };
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
lView = (ListView) findViewById(R.id.ListView01);
// Set option as Multiple Choice. So that user can able to select more
// the one option from list
lView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, lv_items));
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
I want to know how to open another ListView after one of rows on the previous ListView has been clicked?
Bee VOA reader could be a good example to see what I'm talking about.
List A List B
DeskTop Development ---> Buttons
TextView
ScrollBar
Mobile
Graphic
Game
So there is one list A on the phone's screen, and there are many topics in the list A
If I clicked on one of the topic,let's say DeskTop Development, then the whole ListView
will be slided away from the screen and the new List B will be presented.
So how to implement it?
Opening a new Activity while passing the appropriate data to display the correct information is a good way to do what you're doing. This is a code example of a list of states which goes to a list of cities in the selected state from the previous list. The state listview from the first activity sends the name of the state to the city listview in second activity which displays a list of cities from that state.
private ListView lv;
setContentView(R.layout.displaylayout);
lv = (ListView)findViewById(R.id.DisplayList);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(view.getContext(), DisplayLocationsCities.class);
myIntent.putExtra("state",lv.getItemAtPosition(position).toString());
startActivityForResult(myIntent, 0);
}
});
//////////////////////back button code allowing you to go back to the previous list (close anything that need to be closed in onDestroy()):
this.backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// equivalent of 'return'
finish();
}
});
//////////////////////adding items to your list dynamically:
String [] list = {"New York","Illinois","California","Wisconsin"};
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, list));
//////////////////////Extra Credit: Layout for each listView item - R.layout.list_item (list_item.xml in the res/layout folder):
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:cacheColorHint="#android:color/transparent"
android:textSize="12sp" android:textColor="#000000" android:textStyle="bold" android:gravity="center">
</TextView>
Implement the onListItemClick() method so that on click it starts a new ListActivity. This way the navigation in the menu will be easier as you will be able to use the back button.