Reputation: 85
Very green Android developer here. I have a handheld Zebra scanner I am trying to develop an android application for. I have mapped the volume buttons to scroll through a listview, but the only way I can tell it is indeed scrolling is to put the selected item's text in an EditText. I would like for the selected item to be highlighted instead (similar to touching an item). Is this possible? Below is the code I am using for the volume keys. Any help would be greatly appreciated.
if (keyCode == Android.Views.Keycode.VolumeDown)
{
if (scrollItem < itemList.Count - 1)
{
scrollItem += 1;
itemListView.RequestFocusFromTouch();
itemList.SetSelection(scrollItem);
item = partsAdapter.GetItemAtPosition(scrollItem);
itemEditText.Text = item.partNbr;
saveItemDesc = item.partDescription;
}
return true;
}
//scroll up through parts listing
if (keyCode == Android.Views.Keycode.VolumeUp)
{
if (scrollItem > 0)
{
scrollItem -= 1;
itemList.SetSelection(scrollItem);
item = partsAdapter.GetItemAtPosition(scrollItem);
itemEditText.Text = item.partNbr;
saveItemDesc = item.partDescription;
}
return true;
}
OnCreate...
{
...
itemList = FindViewById<ListView>(Resource.Id.itemListView);
partsAdapter = new PartsAdapter(this);
itemList.Adapter = partsAdapter;
itemList.ChoiceMode = ChoiceMode.Single;
itemList.OnItemClickListener = new ListListener(this);
}
Upvotes: 3
Views: 288
Reputation: 70
I would like for the selected item to be highlighted
Add the following line in your ListView
item layout :
android:background="@drawable/bg_key"
Define bg_key.xml
in Drawable
folder like this :
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/pressed_color"/>
<item
android:drawable="@color/default_color" />
</selector>
Then, add the OnItemClickListener
in your ListView
:
listView.OnItemClickListener = new MyListener(this);
public class MyListener : Java.Lang.Object, AdapterView.IOnItemClickListener
{
private MainActivity mainActivity;
public MyListener(MainActivity mainActivity)
{
this.mainActivity = mainActivity;
}
public void OnItemClick(AdapterView parent, View view, int position, long id)
{
view.Selected = true;
}
}
This way, only one item will be color-selected at any time. You can define your color values in Resource/values/colors.xml
with something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="pressed_color">#4d90fe</color>
<color name="default_color">#ffffff</color>
</resources>
Upvotes: 1