I have created a listview using the default ArrayAdapter. However, to select an item from the list I have to tap the item and not just the row. How can I change it in order to just click the row the item is in rather than the item itself?
Here is the code for my listview:
ListView mListView = (ListView) findViewById(android.R.id.list);
mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.row, ans));
mListView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int i,
long l) {
Log.d(LOG_TAG,"Selected option"+i);
startActivity(intent);
finish();
}
});
Here is the xml for the listview:
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:divider="#drawable/list_divider"
android:dividerHeight="1dp" />
Here is the row.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/list_answers"
/>
Change your child views of the row to include android:focusable="false". This will allow you to click the whole row. Right now, since each individual view thinks it can gain focus, they happen "on top" of the row.
P.S. What we really need is the XML for the ROW of the ListView. Not the ListView itself. :)
Hope this helps,
FuzzicalLogic
Related
I have few listView on my application. I want to set onListItemClick on every Listview. first listView used id android.R.list so no problem on that. But i stuck on other ListView, because android.R.list already used and i can't use it anymore. So i use different id like android.R.list1 and android.R.list2. But i can't set onListItemClick on List1 and List2. My application always force stop and log said i must have ListView with id android.R.list. How to fix it ? Any idea ?
Thanks...
this my main_layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android1:id="#+id/textHint"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
android1:text="Tap to subscribe channel"
android1:textAlignment="center"
android1:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/listAvailableChannel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
And this is i set listview adapter
CustomListAvailableChannels adapter = new
CustomListAvailableChannels(AvailableChannelActivity.this, channel_id,channel_img, channel_name);
listAvailableChannel=(ListView)findViewById(R.id.listAvailableChannel);
listAvailableChannel.setAdapter(adapter);
listview.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(getApplicationContext(), ShopDetails.class);
startActivity(i);
}
});
I think You can try this way...
Given the ListView below, I wanted to perform two different actions depending on whether the user selects the text (create a new activity) or clicks the associated checkbox (add it to a favorites list). Is this possible in with this setup or will I have to use a custom adapter or even a different layout?
<LinearLayout 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:orientation="vertical">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, teams));
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String team_name = adapterView.getItemAtPosition(i).toString().trim();
Intent intent = new Intent("blah.blah.blah");
intent.putExtra("team", team_name);
startActivity(intent);
}
});
A
If I am understanding you correctly, your ListView should contain TextBox and CheckBox and TextBox and CheckBox are clickable, not ListView itself.
For this you have to make custom adapter, where you will make listeners for both the TextBox and CheckBox.
public class CustomAdapter extends BaseAdapter {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// your_costum_view should contain textbox and checkboc
view = inflater.inflate(R.layout.your_costum_view, null);
// Get your checkbox and textbox and add listeners
TextView textView = (TextView) view.findById(R.id.textView);
textView.setOnClickListener...
Checkbox checkbox=(CheckBox)view.findById(R.id.checkBox);
checkBox.setOnClickListener...
return view;
}
}
your_costum_view layout example
<?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" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Using a custom adapter will help you keep trace of your views, it will be easier than use a default adapter that you can't control. For references: http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown_custom
Using a custom adapter will definitely make your life easier. In the adapter you can make reference to both the checkbox and the textView and add an onClick Listener to each - from there you can also add code to handle each event. I would also suggest using a recyclerView instead of a ListView. It is the new thing in Android 5.0 and it really is easier to use then a regular ListView. Hope this helps:
RecyclerView Help
Set the CheckBox as focusable="false" in your XML layout.
android:focusable="false"
if don't run go to this link and see example because you need a create custom row in list view and set:
Checkbox checkbox=(CheckBox)view.findById(R.id.checkboxID);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//do stuff
}
});
I use an adapter to load a list view from a content provider. Each item in the list uses the below layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id ="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckedTextView
android:id="#+id/txt_chat"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
/>
</LinearLayout>
</LinearLayout>
I have set the list item to allow multiple choice mode :
onCreate(){
...
lv_chatMessages = (ListView) findViewById(R.id.listview_chat);
lv_chatMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv_chatMessages.setItemsCanFocus(false);
...
}
But at run time, i am not able to check any of the items in the list view. If i touch the box, the item just gets highlighted momentarily, but no check mark appears.
I can set a default for all the items if i add an attribute to the CheckedTextView :
android:checked="true"
How do i let the user change the check status of each item at runtime?
refer this link...
CheckedTextView
this may help you..
[Edit by Faizal] :
Using the toggle() inside the onItemClickListener did the trick :
lv_chatMessages.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
CheckedTextView ctv = (CheckedTextView) (((ViewGroup) arg1).findViewById(R.id.txt_chat));
ctv.toggle();
}
});
can you add this into list items
android:descendantFocusability="blocksDescendants"
android:focusableInTouchMode="false"
Since, you are using a custom adapter, you'll have to maintain the status of the checkbox in the getView() method of the custom adapter.
What you could probably do is to add another Boolean field to your ArrayList of Objects which you use to populate the adapter and check that value in the getView() method. to Something like :
if(status) {
checkBox.setChecked(true);
}
Also, update the status variable using OnCheckedChangeListener().
This method will let you toggle status and also ensure that the checkbox status is maintained even when a list item goes out of the view.
Edit 1 : When not using a custom adapter. This is how I did it.
ListView rendererList;
ArrayList<String> friendlyNames;
friendlyNames = new ArrayList<String>();
rendererList = (ListView) dialog.findViewById(R.id.rendererList);
ArrayAdapter rendererSelectionAdapter = new ArrayAdapter<String>(activity,R.layout.custom_list_layout, friendlyNames);
rendererList.setAdapter(rendererSelectionAdapter);
rendererList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
custom_list_layout was the same layout which android provides, however, I had to make slight modifications so I copied the default file and edited.
here is the custom_list_layout :
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:background="#color/white"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_vertical"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:textSize="#dimen/dialog_box_button_textsize" />
OK I've searched and seen similar issues, tried them and no avail. I have a listView with some elements and I want to click on one element and display a detail somewhere else.
This is my listView
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pedidos);
ListView listView = (ListView) findViewById(R.id.actualStoresList);
Model.initialize();
Vector<String> values = Model.stores;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
Object o = adapter.getItemAtPosition(position);
String str_text = o.toString();
Log.i("", "I have selected this: " + str_text);
}
});
CustomStringAdapter adapter = new CustomStringAdapter(this, R.layout.my_list_layout, R.id.list_content, values);
listView.setAdapter(adapter);
}
This is the my_list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/list_content"
android:textColor="#000000"
android:gravity="center"
android:layout_margin="4dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"/>
</LinearLayout>
The list gets displayed correctly. When I click on an element of the list (A textView) it "steals" the click event so nothing happens (the onItemClickListener is attached to the listView, not the TextView).
The textView has an small margin where, if careful, I can click just behind it, in fact, touching the listView. In this case, the event gets fired ok and I see the log.
I've tried to set the TextView android:focusable="false" but still, the TextView is "above" of the listView and always gets the click events.
How can I either make the TextView "transparent" so it actually clicks on the listView, or add a onclickListener to the TextView so I can handle its events?
Thanks!
Alejandro
Setting clickable property of TextView to false should solve this problem. Try this:
<TextView
android:id="#+id/list_content"
android:textColor="#000000"
android:gravity="center"
android:layout_margin="4dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:clickable="false" />
To make items not focusable, do this:
listView.setItemsCanFocus(false);
source
I think this will solve your issue.
Also make sure the
android:textIsSelectable
property is not set to true.
I'm using the excellent drag-sort-listview by Carl Bauer (https://github.com/bauerca/drag-sort-listview) to implement a drag-sort enabled listview. However, my requirement is not to need a drag handle on the list, but instead to allow the user to drag the list items using the item itself.
I've gotten that part to work, by setting the #id/drag property to the list item itself.
However, it has a side-effect of not responding to itemClick and itemLongClick events.
Is there any way to get the item clicks / long clicks to work without having a separate draggable layout?
For reference, my code looks like below -
ListView.xml:
<com.mobeta.android.dslv.DragSortListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dslv="http://schemas.android.com/apk/res/com.myproject"
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
dslv:collapsed_height="1dp"
dslv:drag_scroll_start="0.33"
dslv:max_drag_scroll_speed="0.5" />
ItemView.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="#dimen/list_item_height"
android:orientation="horizontal">
<CheckBox
android:id="#+id/check_box"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"/>
<TextView
android:id="#+id/drag"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:padding="#dimen/list_padding"
android:gravity="center_vertical" />
</LinearLayout>
Activity.java:
DragSortListView listView = (DragSortListView) view.findViewById(R.id.list);
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(arg0.getContext(), ((TextView)arg1).getText(), Toast.LENGTH_SHORT).show();
return false;
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View itemView, int index,
long id) {
Toast.makeText(getView().getContext(), ((TextView)itemView).getText(), Toast.LENGTH_SHORT).show();
}
});
As a bonus, if anyone can help enable multiple-select in addition to click/longclick, it would be much appreciated.
Thanks!
To be able to use OnItemClick and OnItemLongClick in your list you need to set this parameter to the com.mobeta.android.dslv.DragSortListView layout.
dslv:drag_start_mode="onMove"