Placing divider at a certain position in ListView - android

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)

Related

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
}
....

Align spinner values to right instead of left

In spinner xml, I'm using:
android:entries="#array/cars"
where cars is list of numeric items.
The spinner automatically align values to left, and I can't find in xml/code how to change it.
You have to use a custom view here to give alignment to the Spinner values. Create a CustomView like this answer has created and add android:gravity to the TextView as right.
UPDATE:
Set the CustomView to your adapter using
adapter.setDropDownViewResource(android.R.layout.your_custom_created_view);
You can use TextAlignment on properties and select TextEnd,
or android:textAlignment="textEnd" on XML simple
You can try with spinner item text alignment right end :
view.textAlignment = View.TEXT_ALIGNMENT_TEXT_END

Dynamic Group Indicator for ExpandableList View

How to set group indicator for ExpandableList View dynamically ?
I had group_indicator.xml in drawable folder.
But no idea of how to set it. Can anyone help please...
First set android:groupIndicator="#android:color/transparent" to your expandable list view in xml. Then you can set any indicator that you want to set. Suppose you have an image named "pin" in your drawable folder, then you need write following lines.
ExpandableListView explist;
Drawable d = getResources().getDrawable(R.drawable.pin);
explist.setGroupIndicator(d);
Thanks...
Suppose you have created ExpandableListView through code:
ExpandableListView ev = new ExpandableListView(this);
ev.setGroupIndicator(getResources().getDrawable(R.drawable.group_indicator));

Set the XML file for Layout design according IPhone screen widgets

I want this type of layout how i can do this
all the titles should be clickable
please help me
I used Button and TextView but it seems very dull
Inflate a LinearLayout with a TextView and an image (the right arrow). That LinearLayout must have clickable="true" and onClick="methodYouLike"
To identify what is being clicked you can add a tag to each, with an id, like android:tag="1", android:tag="2" ...
On the Activity onClick receives a View, só you can get that view, an obtain the tag, and do what you need.
If you are not content with the standard UI you can always write your own custom widgets. See here for some more information.
Aso if you need not only 4 items but many, you can use ListView You can provide a layout resource id and it will be atomatically inflated into each row. Datasource could be as DB so array and so on.

Android: multiple icon in ListView

In my List View, I want to display multiple Icon. Icon1 for list row 1, Icon 2 for list row 2, Icon1 for list row 3.
Override getView in your ListAdapter and then you can provide whatever layout you want for each row.
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
You want to use a different icon in each row correct? Well you can use an Integer array of resource IDs of all your pictures and put those in order for your row items. The typical way people do this is hard code the list and specify the R.drawable.iconName that is in your resources. like:
int[] mapPic = {
R.drawable.icon1,R.drawable.icon2,
R.drawable.icon3,R.drawable.icon4};
To me this is a crude solution, hard-coding is a big no-no. A more elegant way is to put the array into your xml file like string.xml or one you create for all your arrays. In your string.xml put in:
<array name="arrayIcon">
<item>#drawable/icon1</item>
<item>#drawable/icon2</item>
<item>#drawable/icon3</item>
<item>#drawable/icon4</item>
</array>
Then in your code when you first create your view before the setting content, add in:
TypedArray mapPic = getResources().obtainTypedArray(R.array.arrayIcon);
You can the access the object which is actually the drawable by:
Drawable drawable = typeArray.getDrawable(RowID);
In my code to set it in my view I had to grab the ViewWrapper and use getIcon() to set it.
ViewWrapper wrapper.getIcon().setImageDrawable(drawable);
And that is how you add your image resources you want to your list by using an array in the xml resources. Hope this helps.

Categories

Resources