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.
Related
My ListView item consists of the following components - ImageView and then under it there are two more ImageViews - like and dislike.
So when I click on either like or dislike I want to be able to get corresponding ListView item id.
The only way that I am able to do it at the moment is if I first click on the ListView item and then on the like ImageView.
My question is - is there any way to get corresponding ListView item id without having to first click on the ListView item and only then on the like ImageView?
Here is my code
cursorAdapter = new PostCursorAdapter(this, null, 0);
ListView list = (ListView)findViewById(android.R.id.list);
list.setAdapter(cursorAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, final long l) {
ivLike = (ImageView)findViewById(R.id.ivLike);
ivLike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("Like","Like button for the post with id=" + l + " has been clicked");
}
});
}
});
I was able to solve it myself. What I needed to do was to add setOnClickListener to my like and dislike ImageViews not in MainActivity but in PostCursorAdapter. Thanks everyone!
EDIT: I did a workaround and it SORT OF fixed my issue.
My layout for the spinner looks like
<LinearLayout>
<Spinner>
<TextView> //used as a label
</LinearLayout>
I removed the LinearLayout. Though the City spinner does not become auto-populated with items whenever a State is selected, I am able to manually input values for the City.
What could the linearlayout possibly do to mess up my spinner's behavior?
I have a registration form with multiple Edittexts and 3 spinners at the bottom namely State, City, Area.
What I want to be able to do is the following
onItemSelect a State, City and Area will show the first items in their list
onItemSelect a City, Area will show the first item in its list
onItemSelect an Area, it will show the item I selected.
Code is working fine on Kitkat. But on JellyBean and below, whenever I select a State, the other spinners do not show anything. After selecting a State, when you click on the City, it will show correct dataset, however when I click on an item the spinner remains blank. (onItemSelected does not fire.)
The weird part is that, whenever the spinners misbehave, if I click on one of the Edittexts in my view (softKeyboard will be shown) > Press back button > Spinner works. I'm not sure if it has something to do with focus? But I already tried to set the spinners to focusable and focusableInTouchMode then request focus, but to no avail.
this is how I set the adapters and content of the spinners
state.setAdapter(stateAdapter);
state.setSelection(0, false);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
stateSelection = parent.getItemAtPosition(position).toString();
cityAdapter = new ArrayAdapter<String>(getActivity(), R.layout.sherlock_spinner_item, getDistinctCity(stateSelection));
cityAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
city.setAdapter(cityAdapter);
areaAdapter = new ArrayAdapter<String>(getActivity(), R.layout.sherlock_spinner_item, new String[]{});
areaAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
area.setAdapter(areaAdapter);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
city.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
citySelection = parent.getItemAtPosition(position).toString();
areaAdapter = new ArrayAdapter<String>(getActivity(), R.layout.sherlock_spinner_item, getDistinctArea(stateSelection, citySelection));
areaAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
area.setAdapter(areaAdapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
area.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
areaSelection = parent.getItemAtPosition(position).toString();
postalSelection = getDistinctPostal(stateSelection, citySelection, areaSelection);
postal.setText(postalSelection);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
If anyone can explain to me why the spinners are behaving like this, or possibly lead me to the right direction, that would be awesome. Thank you
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
When i try to remove a specific item from a list View:
buyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tempToken -= selPerk.cost;
plrPerks.add(selPerk);
String tokStr = String.valueOf(tempToken);
tkn.setText(tokStr);
shopItems.remove(selPerk);
selPerk = new Perk();
perkDialog.dismiss();
}
});
It always seems to remove the last item. This is where i open the dialog:
perks.setClickable(true);
perks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Perk perk = (Perk) perks.getItemAtPosition(position);
showItem(perk);
}
});
}
This is the show Item function:
public void showItem(Perk perk) {
if (tempToken >= perk.cost) {
selPerk = perk;
How do i remove a specific item from a list and list view respectively?
Thanks for your time :)
In "setOnItemClickListener" listener, you are getting the perk object. So you can remove that object from your list like this-
shopItems.remove(perk);
and then you can call-
your_adapter.notifyDataSetChanged();
to refresh your listview.
To remove a specific item from a list view, your can call removeView(View toBeRemoved) if you have a reference to the view you wish to remove. If you have the index, you can call removeView(int index).
http://developer.android.com/reference/android/widget/ListView.html
You can remove a specific item from a list in the same way, using remove(Object item) or remove(int index).
http://docs.oracle.com/javase/7/docs/api/java/util/List.html
Hope this helps!
I fixed it. Whenever i removed an item i had to do it like so:
shopItems.remove(selPerk);
perk_adapter.notifyDataSetChanged();
So i had to notify my listeview adapter that i removed an item.
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.