Backgroundcolor on ListView Items - android

my problem:
ring5Views[i]
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View arg1, int position, long arg3) {
arg1.setBackgroundColor(Color.parseColor("#444444"));
}
});
if I click on one Item (child) every second item (%2) will change its bg color.
Why ?
The hole thing I want is a list with items and different colors for each but getChild dont solve the prob because NULL every time. And getItem dont help because it only gives back the value (string)
This is how it should work...
for(int i=0; i<oneList;i++)
{
oneList.getChildAt(i).setBackgroundColor(Color.blue);
}
thanks

Extend the ArrayAdapter and override the getView method. And inside the getView method based on the condition you can change the color
you can see this Example for reference

Related

manage the color of a TextView in a GridView

I need to know how can I change the color of a single TextView in a GridView when I click it.
Different TextViews need different colors.
I tried to change color with the id with the function setonIitemListener, but it doesn't work
You can set up OnItemClickListener which correspond to the item position.
gridView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long arg3)
{
//Selected item is at index "position"
//now you can get arg
TextView tv = v.findViewById(R.id.your_text_view);
tv.setColor(ContextCompact.getColor(mContext, R.color.reddish);
}
});

Highlighting the list item of a list view in android for a specif time

In my android application i'm having a list view and some items in that,now my requirement is when I select a particular item from that list the list item should be highlighted with a red color, and that selection should not be disappear because in my application i should choose an item from the list view and i should click a button(submit).
So in order to know to the user that he has selected an item from the list view it should be highlighted until he clicks submit button.
I have used choice Mode attribute and set the value to single choice and i have changed the color for highlighting everything works fine but i need it not to be disappeared until user clicks the submit button
The variable val consists of some names which i retrieved from database.
Please help me to solve this Thanks in advance.
ArrayAdapter<String> adptr= new ArrayAdapter<String>(this,R.layout.row,R.id.member_name,array);
lv.setAdapter(adptr);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
// TODO Auto-generated method stub
val=lv.getItemAtPosition(pos).toString();
//Toast.makeText(getApplicationContext(), val, 5000).show();
}
});
use OnItemClickListener
ListView lview = getListView();
int pos =0;
lview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// set the background of v here
if (position == pos)
// reset the background color
// and pos = position;
else
get the previous listitem view and reset it's background to original
}
});
Actually I had the same problem sometime back. I was working on non touch andorid ui application for google tv.
The solution is like this.
1) create a custom ArrayAdapter (extending ArrayAdapter)
2) onItemClick get the position of the item.
3) send that position to your adapter by some public method say setCurrentPosition(int pos)
4) in getView() of the adapter check for the position and set the background to red for that view.
Hope this will work as it worked for me.

Custom adapter setOnItemClickListener parent.getItemAtPosition() always null

I have a custom adapter giving custom views to a listview (a few fields and an image). I've changed something, and for some reason my clickListener method parent.getItemAtPosition(position) is always returning null. Not sure how the change I made has made this effect. When stepping through it, it fires correctly, but for some reason the value is always null. Is there anything you can see?
ListView airportResults = (ListView) findViewById( R.id.listAirportResults );
List<SearchResultsItem> items = getSearchResults(searchTerm);
ResultsAdapter = new SearchResultsAdapter(this, items);
airportResults.setAdapter(ResultsAdapter);
My getSearchResults(searchTerm) returns the items in the listview with the following type:
List<SearchResultsItem>
Then the onclick listener:
airportResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long duration) {
String sClickedItem = (String) parent.getItemAtPosition(position);
//do something with sClickedItem
}
});
From the above code, the position parameter is correct (I tap on the first item, position=0; I tap on the 4th item, position=3 etc). The parent and view objects all have values. However, the sClickedItem is always null.
Many thanks,
J
Solved. It was my implementation of my custom adapter.
airportResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long duration) {
TextView textViewIata = (TextView) view.findViewById(R.id.list_searchresults_iata);
String clickedIataValue = (String) textViewIata.getText();
As I wasn't using standard objects, I had to make my own implementation. Simply: my view was a cutsom XML layout with various textviews in it, so I just had to view.findViewById() (because the view is the currently selected item in the listview) and getText() that way instead.
Thanks for making me realise this
J

Android ListView additional items markered error

I have list view with 78 items, in my activity file I descripe onClick Function realization
lv.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.d("Click from activity", "win");
if(!checkedContacts.contains(clearContacts.get(arg2))){
checkedContacts.add(clearContacts.get(arg2));
arg1.setBackgroundColor(R.color.selectedItem);
} else {
checkedContacts.remove(clearContacts.get(arg2));
arg1.setBackgroundColor(Color.WHITE);
}
}
});
So when I clicked on item it change color. But if I scroll down my listView, other elements every 10 times will have new color. checkedContacts array have right values but, visualization wrong
The problem is that listviews recycle the views that are out of the screen so this way you are not saving the row status. When you get the row out of the screen it will be deleted and when it comes back again it will be refilled in your adapter again.
You may solve this, for example, using your checkedContacts var and make it accesible from your adapter so, if the position given in getViewfunction of your adapter you can check if the row has been checked and then youn can change the background color. If the user rotate the screen you may save this var in onSaveInstanceState in your activity to recover them later.
Hope it helps :)

How to highlight an element in GridView?

I have a GridView with a BaseAdapter. I would highlight an element with its focus or selected or checked state.
I tried to set grid.setSelection(0) to highlight the first element, but nothing happened.
I set my drawable states background.
put this code in GetView() Method of base adaptor
If(items[position] == selectedIndex){
// Change background or something alpha or anything so that it looks like it is selected
}
And in onItemSelected() Method
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position,long id) {
selectedIndex = position;
adapter.notifyDataSetChanged();
}
Feel free to ask if you not get anything

Categories

Resources