ListView individual item styling - android

I am developing an android app, and in one fragment, I already have a listview and have a set of items that will be added to this listview via an arrayadapter. However, I was requested to dynamically color some list items based on a particular property in the original database, specifically bold only those elements to highlight them. Is there any possible way to acheve this with the arrayAdapter class, or if not, considering this is a simplistic use case, what can I do to achieve this

It's very easy to handle this problem you just have to put a condition in your adapter and if that condition is true you just have to customize your item as it's asked from you .
if(condition)
{
view.setBackgroundColor(activity.getResources().getColor(R.color.White));
}
And if you want to set a TextView as Bold programmatically in your adapter check your condition and set it like below :
textView.setTypeface(null, Typeface.BOLD);

you can do it by your ArrayAdapter class,
just put you condition in adapter getView Method, and set your text view as a bold
if(conditionPart){
textView.setTypeface(null, Typeface.BOLD); // For bold
textView.setBackgroundColor(context.getResources().getColor(R.color.Black)); // For Black Color Background
}else{
textView.setTypeface(null, Typeface.NORMAL);// For Noral
textView.setBackgroundColor(context.getResources().getColor(R.color.Gray)); // For Gray Color Background
}

Refer to this tutorial:
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
Extend ArrayAdapter class and override getView() method
Apply conditional statement as mentioned above

You can create your custom Adapter where you can style your text as you want here is sample for custom adapter:
Custom Adapter for List View

Related

Changing the layout/color of a listView

I have a listview which contains 2 textViews and an ImageView. Whenever is pressed I want to the selected row to change a bit it's layout. For example change the color of the texts change the imageView add another one etc..I have added a custom list selector for the background to change. My question is which is the correct listener in which I should add:
TextView title;
TextView descr;
title = (TextView) arg1.findViewById(R.id.title);
descr = (TextView) arg1.findViewById(R.id.descr);
title.setTextColor(Color.parseColor("#189dd9"));
descr.setTextColor(Color.WHITE);
I added it to OnItemClickListener and it works correct after the item is clicked not while it is selected.
Inside adapter you can keep a track of which item is selected.
int selectedItemIndex;// ArrayList<Integer> selectedItems=new ArrayList<Integer>();
Now in getView just write if
if(position==selectedItemIndex){
//Inflate different layoout
}else{
//normal flow
}
To set selected items you can set onItemClick Listener and set this variable in adapter or you can add an onClick listener inside getView itself and in it set this variable.
In case you want to have same layout just write different code inside if/else
I did using multiple selectors, according to this link I'm reposting the link just in case someone finds this thread first. cheers!

textview appears faded android

I am using a custom adapter to populate a listview.The list item is made of 2 textviews and the result is the following:
Any reason why the textview appears faded?
you might not have set the textcolor and tht could be a reason. set the textcolor to the textviews to black and it would be done.
Maybe because you have made your textViews in listView row as enabled:"false" .
If so give your textView textColor = "#000000" and otherwise also give textColor

How to customize a list in android?

i'am using a list in my main activity
and i don't want to use android list , i want to create my own list ,already i created my custom Adapter that enables me to use images and text in any row of list but i want to disable the divider of the list and the hover mode
it's mean that i don't want to have something like this pic
it is not my project screenshot
thanks in advance,
related with Avoid ImageView hover state inside Listview if List Item is pressed
you can use :
set empty background
android:cacheColorHint="#android:color/transparent"
or
android:cacheColorHint = "#00000000"
to remove divider between items in the list use :
myListview.setDivider(null);
or XML:
android:divider="#null"
android:dividerHeight="0dp"
in your adapter, override the method isEnabled(int position) and have it return false for this item.
Since you already have your own CustomListAdapter, do the following in your onCreate() method:
ListView list=(ListView)findViewById(R.id.list); //or whatever the id of your listview is
// Getting adapter by passing xml data ArrayList
YourCustomAdapter adapter = new YourCustomAdapter(this, listDataToBeDisplayed);
list.setAdapter(adapter);
Try code below to disable the highlight, you may change the text color as well
ListView.setSelector(new ColorDrawable(Color.TRANSPARENT));

How to Apply different colors to list view items in Android

I have to show list view as like below image
Here I have the view in the xml as like this
<View
android:id="#id/margin"
android:background="#6DAAE9"
android:layout_width="20.0dip"
android:layout_height="fill_parent" />
I Have to change this background (#6DAAE9) dynamically when list is loaded and I have to fix these colors to corresponding item even the list items are increased.
Can any body help me. Thanks in advance
Basiclly you need to edit your getView method.
you can use
convertView.setBackgroundColor(Color.parseColor("#6DAAE9"));
With your appropriate logic, meaning if you want it to be random set random color or hold an array with the order, etc.
See this listview I added list item as per odd or even row and you can do it checking position in getView() method.And you should put imageview for that and set color rather that set color to row.
if(position==0){
//here set color for imageview which position is 0
}else if(position==1){
//here set color for imageview which position is 1
}
....

Placing divider at a certain position in ListView

I'm using a ListActivity with an ArrayAdapter. I have an array of Strings, and I want to put a divider after the 5th String in the list. Is there any way to do this? Sorry if there are duplicates, just did not find what I was looking for. Thanks in advance.
There is a hack.
Add a divider view at last in your list_item's layout file and give it an Id. Now in your custom adapter class,you can set that view's visibility depending on the position whether you want it to be shown or not,in getView() method.
A property of Listview will help you:
android:divider = (color or drawable)
And also
android:dividerHeight = (integer in dp)

Categories

Resources