Changing the font colour of one item in a ListView? - android

The title pretty much says it all. I've got a ListView I'm using in my navigation bar/side menu and want to change the colour of the first item. I can't seem to get a hold of the view itself to setTextColor() or whatever the method would be, so how else can I do this?

You want to change the colour of the textview (or whatever view) in your adapter by overriding or implementing getView().
I assume you don't have a custom adapter or you wouldn't be asking this question, so you might want to check out some tutorials on how to use a custom adapter to create a custom appearance for your list items.

Related

ListView selected item highlight

I'm working on a GridView with setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL). By default, rows are not getting highlighted (with the blue highlight you see on WhatsApp or Gallery apps), so I am wondering, does the Android API already takes care of highlighting the selected items and I am missing something (if so, what am I missing?) or do I have to take care of highlighting selected rows by myself (if so, how to highlight, not just change the background)?
Here's my code:
gridView.setOnItemClickListener(this);
gridView.setEmptyView(view.findViewById(R.id.empty));
gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
// multiChoiceModeListener is a subclass of AbsListView.MultiChoiceModeListener
// with no particular code on its abstracts methods.
gridView.setMultiChoiceModeListener(multiChoiceModeListener);
Use this for your ListItem background (XML layout)
android:background="?android:attr/activatedBackgroundIndicator"
It's basically only a selector, you can also build yourself: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/drawable/activated_background.xml
EDIT:
Given your comment, this solves it:
android:foreground="?android:attr/activatedBackgroundIndicator" with a FrameLayout
Also related question: How to set foreground attribute to other non FrameLayout view

Preselect items in listView Android

All i need is an idea or path to continue searching, i have a list of items that i add to a listview adapter. And i am trying to make some items in the list to have a specific background color (as an example) when the activity loads. Something like having those rows "preselected". The method public ListView getListView() does not help me much.
Thank you either way.
Need to set Multiple Choice Mode via setChoiceMode(), and then you can call setItemChecked() for each of the items you want checked. Then you probably have to make a custom adapter that overrides getView(), cast the parent to ListView and ask if the View (a list item) you are getting at the position given is in fact checked by calling something like getCheckedItems() on the parent. Then you can do what ever you want with that view, like set its background color etc.

ListView Item and OnClick problem

When you touch some grid or listview items in an android app, he change the background color telling you that you have pressed it.
I'm having a problem in my app. When I create that a listview for instance, and I press it in my phone, he change color, but if I set the OnClick property, the item dont change it color. Whats wrong? The OnClick works as its suppose.
Thanks
Generally with ListView you want to use an OnItemClickListener.
This gives you a chance to perform an action based on the position in the list that was clicked.
If you need access to the view adapter from within onItemClick then you can use AdapterView#getAdapter() on the AdapterView that is supplied to you.

Android: How to work with Checked ListViews

I wish to work with checked list views wherein only one item can be selected at a time. Some queries related to this:
Is it advised to work with CheckedTextView as the ListView items, or a combination of CheckBox and TextView?
If using CheckedTextView, the text comes first and the checkbox appears on the right edge. Is it possible to make the checkbox appear on the left of the TextView?
How can I make one of the items as checked in onCreate()?
Note: I am using array adapter and calling setAdapter() to populate list.
You need to extend ArrayAdapter and use LayoutInflater to inflate the row layout as you need. This way you have full flexibility in list creation.
Please check this example, where basic idea is described:
Custom list view

Image+TextView+radiobutton in spinner

First, I want to imitate the effect like "android.R.layout.simple_spinner_dropdown_item"
which is TextView+radiobutton
but either the getView() or getDropDownView only cares about one row at a time, while the whole radiobuttons in spinner is like one RadioGroup, and when dropdown view is opened, the radiobutton with which item is selected before must be checked.
so how can I imitate the layout like simple_spinner_dropdown_item?
Second, I want to add different ImageView in each row, so I can't just use the default simple_spinner_dropdown_item, I must redefine the ArrayAdapter.
So, is there a best way to get through these?
I like the layout in simple_spinner_dropdown_item, but I also want to add ImageView...
Please give me any clue at all
Thanks!!
extend the adapter. Override getView() and change the view that is returned to what you are after.
Cheers,
if you want to implement a more generic adapter without overriding the arrayadapter you can look at the post at http://blog.app-solut.com/2011/03/using-custom-layouts-for-spinner-or-listview-entries-in-android/ its using the baseadapter which can be modified as desired.

Categories

Resources