this is the activity
public void onListItemClick(ListView parent, View v,int position, long id) {
String str;
if (nRowSelected>=0) {
View row=parent.getChildAt(nRowSelected);
if (row!=null) {
row.setBackgroundColor(0xFFFFFFFF);
}
}
nRowSelected=position;
v.setBackgroundColor(Color.GRAY);
}//onListItemClick
this is my listview
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="425dp"
>
</ListView>
i need highlight single choice. i choose/focus row number 1. but when i scroll, the focus is more than one. the row focus in row 8 too
this is the capture
and
how to fix that?
You are fighting the way Adapter's recycle the row layouts... You need to extend your current Adapter and override getView() to highlight the correct row (and only the correct row).
At the most basic level it would look like:
public View getView(...) {
View view = super.getView(...);
if(position == mRowSelected) {
view.setBackgroundColor(Color.GRAY);
}
else { // You must use a default case to "un-highlight" the reused layout
view.setBackgroundColor(0xFFFFFFFF);
}
return view;
}
Add this to the xml:
android:cacheColorHint="#00000000"
Replace your xml code with following::
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="425dp"
android:cacheColorHint="#00000000">
</ListView>
Hi Please see the below code might be help it is single choice selection example follow this it work good .
public class List17 extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Use the built-in layout for showing a list item with a single
// line of text whose background is changes when activated.
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1, mStrings));
getListView().setTextFilterEnabled(true);
// Tell the list view to show one checked/activated item at a time.
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// Start with first item activated.
// Make the newly clicked item the currently selected one.
getListView().setItemChecked(0, true);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// Make the newly clicked item the currently selected one.
getListView().setItemChecked(position, true);
}
private String[] mStrings = Cheeses.sCheeseStrings;
}
This is list activity it does't matter that you have list view or list activity.
Related
I'm developing an application for Android TV. I made a custom view where I represent list items (in RecyclerView).
How can I get a focus on my items and highlight them (a simple example would be wonderful)?
I did try to add
android:focusableInTouchMode="true"
android:focusable="true"
to .xml file, but I've not seen any changes.
EDIT: I want to get focus when navigating through items by dpad
in youf adapter do something like this
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.YOUR_LAYOUT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do whatever you want
viewHolder.YOUR_LAYOUT.setBackgroundResource(R.drawable.ripple_effect);
}
});
}
Why don't you use setOnitemClicklistener on your listview or recycleview . This is how you can do this:
ListView lv = getListView();
setContentView(lv);
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View view,int position, long arg3)
{
Toast.makeText(Activity.this, "" + position, Toast.LENGTH_SHORT).show();
//========== Here you can change the color of selected item ========
TextView Tv_title = (TextView)view.findViewById(R.id.title);
Tv_title.setTextColor(Color.RED);
//====== Your can make any view color change here just like i did for textview ===
}
});
If still you are not getting focusable try using this solution :
Use android:descendantFocusability="blocksDescendants" in your XML file of the first parent layout inside the list.
Hello guys I want to highlight multiple items in a list view.
So I tried SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); but it didn't help
I am using a custom adapter and extending BaseAdapter
I am using ListView and not AbsListView
I don't want to use CAB because it doesn't go well with the design of my app
I don't want to use the getView method of the adapter as well.
I don't want to use checkboxes either, I guess I will use a boolean for each item and pass it to getviews if I don't get a solution here, but that doesn't seem too elegant and neat. I believe there is a proper built-in way of doing it without using getview() of the adapter
I tried:
android:drawSelectorOnTop="false"
android:listSelector="#android:color/darker_gray"
in the xml but it is highlighting only one of the items and as soon as I click on another item, it highlights it instead...
So is there any proper way of doing it?
Here is how my app looks:
You can make the same logic as CAB but avoid using CAB.
Your list item should have the FrameLayout at root like
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="?android:attr/activatedBackgroundIndicator">
....
Set onItemClickListener to change choice mode on long press
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (mInMultiChoiceMode) {
// if already in multi choice - do nothing
return false;
}
mInMultiChoiceMode = true;
// set checked selected item and enter multi selection mode
final AbsListView list = (AbsListView) arg0;
list.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
list.setItemChecked(arg2, true);
return true;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (mInMultiChoiceMode) {
//exit multi choice mode if number of selected items is 0
if (((AbsListView) arg0).getCheckedItemCount() == 0) {
((AbsListView) arg0).setChoiceMode(AbsListView.CHOICE_MODE_NONE);
mInMultiChoiceMode = false;
}
} else {
// do whatever you should as in normal non-multi item click
System.out.println("CLICK");
}
}
});
SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
It should be enough, but you have to use getView, to differentiate the selected and unselected state.
You can use isItemChecked() method to determine weather the item is selected or not, so you don't have to introduce a boolean variable for each items.
Edit:
Something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ListView list = (ListView) parent;
if(list.isItemChecked(position)){
//...
}
else{
//...
}
use
SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
then manually do it in the adapter.
I have a Layout in Android which has a ListView which then inflates another layout as its rows using an adapter, pretty standard.
The row layout which is inflated X times depending on how many rows there needs to be contains a spinner. I add a seSelectedItemListener to the Spinner yet when the Spinner is pressed, the options are displayed, then an item is pressed/selected, then the OnItemSelectedListener constructor is fired yet the overrided onItemSelected is not. If i inflate the row layout seprately not in a List View and select an item in the Spinner then the onSelectedItem fires...
Any ideas why this happens or how/if it can work this way?
OnItemSelected class:
class SelectedSizeChangeListener implements OnItemSelectedListener {
Spinner product_size;
TextView product_sell_price;
TextView product_cost_price;
Drink item;
Drink_Size drink_size;
SelectedSizeChangeListener()
{
String s = "here1";
}
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String s = "here2";
}
public void onNothingSelected(AdapterView parent) {
String s = "here3";
}
}
Adding the onItemSelected class to the spinner:
product_size_spinner.post(new Runnable() {
public void run() {
product_size_spinner.setOnItemSelectedListener(new SelectedSizeChangeListener());
}
});
Any help would be great!
Adrian
Try
android:focusable="false"
android:focusableInTouchMode="false"
in list view
Pro-tip:
try to use PopUpMenu rather than Spinner.
Example android-popupwindow in list View
I have what I consider to be a strange dilemma, although YMMV.
I'm using a layout file that describes each line/row in a ListView (nothing too exotic about that). I have an id assigned to each one, such as:
android:id="#+id/checkBox1"
android:id="#+id/checkBox2"
android:id="#+id/checkBox3"
android:id="#+id/contactLabel" // a TextView
Now this doesn't seem to make sense, as these ids should be unique, so what is the id of the second
row? That is, if "row 1" honors the specified ids of checkbox1, checkbox2, checkbox3, and contactLabel, what would the "row 2" ids be?
I'm curious, but also I need to know because I want to save the values of the checkboxes to a SharedPreferences object.
Who has a clue about how to get around this?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Update
The first thing I need to solve is how to respond to a click on the ListView. This is my current conundrum related to all of this:
ListView doesn't know it's been clicked, or won't tell me
I've added this event handler to my ListActivity:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
...but it's not getting called. I click on the Contacts that display, but no go.
I also tried it this way:
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
}
});
...still no joy...I put breakpoints on both "Toast" lines, and they never get reached.
I read here:
http://www.vogella.de/articles/AndroidListView/article.html#listsactivity_layout
...that, "In case you need more the just a ListView in your Activity, you can use you own layout for
ListActivity."
...which I do, because I add a header and a footer in addition to the listview.
It goes on to say, "In this case your layout must have an ListView element with the android:id
attribute set to #android:id/list."
So I added this to my layout:
...but it makes no difference one way or the other.
The ID's for the items within the ListView widget are referenced through their parent view when you inflate it in your getView() method.
To elaborate, you would have something like this is you ListView adapter.
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
convertView = LayoutInflater.from(ctx).inflate(R.layout.list_view_item, null);
}
Nown, a new view instance exists as the convertView. You can access your widgets using covertView.findViewById(R.id.checkBox1), convertView.findViewById(R.id.checkBox2), etc.
Each of these views is a child of your ListView. You can reference each individual view from your ListView using the getChildCount() and getChildAt() methods from the ListView object.
However, since it is recommended to use the convertView view to recycle views, in that case you will only have reference to the views on screen at a time.
Also, with regards to the SharedPreferences, all the views in your ListView are populated by an Adapter subclass which would be the actual object that puts the values in the Checkbox and TextView widgets. This Adapter has a dataset that you provide it. Why not reference the values from the dataset directly, instead of trying to find them from the list items which are populated from the dataset in any case ? You can write to the dataset from the ListView when someone clicks a CheckBox so you have an easy ordered reference to all the items in the ListView.
UPDATE: Added dummy source code
OK. Let's start with a hypothical list. We want to display say five items on the list. For simplicity, I'll assume each has a TextView and a Checkbox. So my container class is:
class Item {
String textView;
boolean checked;
}
Now in my Activity where I want to display this list, I put an ArrayList of items (you can use just about any datastructure) as a class variable. Then I get the ListView reference and assign it an adapter.
class MyActivity extends Activity {
ArrayList<Item> listItems;
.....
onCreate(Bundle icicle) {
.....
ListView listView = (ListView) findViewById(R.id.listView1); // this will be you list view
MyAdapter listAdapter = new MyAdapter();
listView.setAdapter(listAdapter);
....
// Rest of your Activity
....
MyAdapter extends BaseAdapter {
int getItemCount() {
return listItems.size();
}
Item getItem(int position) {
return listItems.get(position);
}
View getView(int position, View convertView, ViewGroup parent) {
// Here's the important part
Item currentItem = listItems.getItem(position); // Since the array is a class variable, you can do either get or getItem
..... // do the standard individual item inflating ....
checkbox = convertView.findViewById(R.id.checkbox);
checkbox.OnItemSelectedListener( new OnItemSelectedListener() { // or whatever listener there should be... I didn't check
... // do whatever...
currentItem.setChecked(true);
}
When you want to retrieve what items were clicked, just iterate through the Item class and find which ones are true or you perform whatever action you want within the Listener since you have a reference identifying individual members of the ListView dataset (here listItems ArrayList).
Apologies for any errors. Didn't do any checking.
I have a ListView that is populated with 50 items.
This is the xml that I use for my ArrayAdapter.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="#drawable/listview_background"
android:textColor="#ff000000">
</TextView>
I type
ArrayAdapter<String> list = new ArrayAdapter<String>(this, R.layout.my_simple_list);
to create a new ArrayAdapter. Then I add a String[] items to the list. Then I call
setListAdapter(list);
To add Strings from the String[] items to list, I use a for loop to loop through the String array and add each String to list using command: list.add(items.get(i)); where i is my for loop counter.
for(int i=0;i<items.size();i++){
list.add(items.get(i));
}
This gets my ListView created and populated. I also have an onListItemClick function.
public void onListItemClick(ListView parent, View v, int position, long id) {
String Select = this.getListAdapter().getItem(position).toString();
if(copyitems.contains(Select)){
v.setBackgroundResource(R.drawable.listview_background);
copyitems.remove(Select);
}else{
copyitems.add(Select);
v.setBackgroundColor(Color.GRAY);
}
}
In the onListItemClick function copyitems is an ArrayAdapter variable that I declare as a global variable. So what this function does is store the text from the selected items of the listview so later I can copy that selected text. So if an item is selected it's text is added to copyitems and if the same item is selected it will be removed form copyitems. Every time an item is added to copyitems the background of that item is changed to Gray and if an item is removed from copyitems the background of that item is set back to original. All this work fine. But when I have lots of items(like 50) they don't all fit to the screen so if I want to select an item that is not on screen then I have to scroll down or up to get to the item. The wierd this is that when I scroll away from a selected item and comeback to it the Background changes to original and a different item has the Gray background. So basically if my screen fits 10 items and I select the 1st one and scroll to select others that are passed 10(for example 11). When the 1st item leaves the screen and the 11 comes in, it is already colored Gray but I didn't selected. When I scroll back up to see the 1st item, the background may or may not be Gray. If its not Gray so other item's Background is set to Gray like the 2nd one. Is this happening because the items are recycled when they go off the screen and because of it the positions change of the items? If it is how can I disable that so I only have items background being gray if I have selected that item. If there is a better approach to what I'm trying to do please tell me. I'm new to android.
You should write your own Adapter. I would recommend you to extend from BaseAdapter. Code would look like this:
// These are members of your activity
private List<String> mList = null;
private int mNewlyAddedItem;
private class Adapter extends BaseAdapter {
#Override
public int getCount() {
return mList.size();
}
#Override
public Object getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return (long)position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(position == mNewlyAddedItem) {
convertView.setBackgroundColor(Color.GRAY);
} else {
convertView.setBackgroundResource(R.drawable.listview_background);
}
// Set text to our TextView
TextView nameText = (TextView)convertView.findViewById(R.id.text1);
nameText.setText(mList.get(position));
return convertView;
}
}
Instead of background changing inside your onListItemClick handler you should just assign mNewlyAddedItem:
public void onListItemClick(ListView parent, View v, int position, long id) {
String Select = this.getListAdapter().getItem(position).toString();
if(copyitems.contains(Select)){
copyitems.remove(Select);
mNewlyAddedItem = -1;
}else{
copyitems.add(Select);
mNewlyAddedItem = position;
}
}
mNewlyAddedItem = position;
I hope I understood your task right. Anyway, you got the point: you should change adapter code to draw your views based on some state (which you can set using Activity member variables), change the state and your views will update (you can also call getListView().invalidateViews() to force redrawal of all views if it is not happening at some point).