Background drawable shape margin - android

I have an xml drawable shape with rounded corners which is background of listview(with margin 10dp).
Now I added header/footer to the listview.
But how can I make background not affect header/footer? I want it to start from the first(not header) element in array and finish at the last(not footer). The size of items in listview is known.
Thanks!

If you can provide the code of your listView item XML and the listView adapter, i can give you a detailed answer.
basically this is what you have to do
1) in your listView item XML, add the header and footer. set id's and set the visibility property to gone
2) in your adapter, in the getView (int position, View convertView, ViewGroup parent) method; check the position. if the position is 0, set the header visibility to View.VISIBLE
and if the position size is equal to the getCount(), set View.VISIBLE to the footer.

Related

Custom divider in RecyclerView

I have a custom layout for my recycler view with an image view and a text view(in a horizontal linear layout). I needed a custom behaviour for my divider - divider should show only under the textview area and not the image view area. For example, whatsapp's chat list view - link
When I use the default DividerItemDecoration, it creates a divider across the view.
How do I achieve a custom Divider which spans only certain views?
I solved this here - https://gist.github.com/shiladitya-bits/1444002568de9f02ce3d
View avoidView = child.findViewById(avoidResourceId);
if(avoidView != null){
width = avoidView.getWidth();
}
mDivider.setBounds(left+width, top, right, bottom);

ExpandableListView, how to set the child height?

I'm trying to set the ExpandableListView item (ViewGroup) height. Can it be set via xml (I don't need the divider height):
<ExpandableListView
android:id="#+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
or it should be used this method, by manipulating the convertView:
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) { ... }
Thanks in advance.
ListView child views can be different heights, so we don't set a one-size-fits all height in the ListView's xml. Rather you can set a specific height in each of the different convertView xml's that you are inflating.
Are you trying to set the height of the group row item in the listview? Also the ViewGroup would be your ExpandableListView and all of its groups and group children row items.
Or are you asking to set the height of the child items in the expandable list view?
The group item and its children in the listview are the same as any other listview row item.
You can just simply provide fixed height in row layout XML:
android:layout_height="25dp"
Or you could use ViewGroup.LayoutParams
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
Question is a bit old, but someone may be useful. As others said, height is not strict value (it changes according to its content). If you want to generally make more space between items (whether it is group or child), you can set padding to corresponding layouts.
This can be done either in XML layout or programmatically in list adapter.

Android Listview : Center items in layout when fewer items

I have a listview that can have items from 1 to 100, dynamically varying.
However, when I have fewer items, I want the items to appear vertically centered in
the listview layout. By default, Android draws the items form the top. I have attached a picture to illustrate what I need.
Is there any way I can achieve this, either by code or in XML layout ?
Thanks.
Edit: Based on the answers I have received now, I am adding more clarity to my question as the answers are off mark.
The positioning of the Listview in the main layout or positioning of individual item layouts is not an ISSUE here. The listview is positioned properly as shown in the shaded background. The question to which I need answer is:
Assume there are 10 items, the entire shared area of Listview will be used by items,
possibly Items 1 to 5 are shown.
Assume there are 3 items only. The listview is still shown but Items 1,2 and 3 are rendered at the top of the listview (shaded area). However, in this case, I want the items to be drawn at centre of the listview's layout (as shown in the picture).
Anyway this can be done ?
Use this property
mLoginUserNameValueView.setGravity(Gravity.CENTER_HORIZONTAL);
which one you want to center in the List view adapter and which position exactly where you need
e-x you need your 55th position you want center the item means
in getview you will use this line like below.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = null;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.navigationlayout,
null);
mHolder.mLayout = (LinearLayout) convertView
.findViewById(R.id.navigationLayout);
mHolder.mNavigationtext = (TextView) convertView
.findViewById(R.id.navigationText);
convertView.setTag(mHolder);
}
if (\\Your desired position checking )
\\ and set the above line here
}
return convertView;
}

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

List header/section color becomes dark while scrolling

I am using the this code to have section/headers in list.
http://w2davids.wordpress.com/android-sectioned-headers-in-listviews/
I am setting the color with 50% alpha to list header and my window background color is transparent. So while scrolling header color becomes dark. Any idea how to overcome this.
and i have also set android:cacheColorHint="#00000000".
I had the same problem. I set the background color of the header view to the same background colour as the list's parent and it worked.
So I had a LinearLayout with a background colour of 'grey'.
The list is contained within this layout.
The list had android:cacheColorHint="#00000000" set in the XML and no background colour set.
The header view is loaded using LayoutInflater, inside my activity and set before the call to the list's setListAdapter.
The parent View (or main View) of the list header was also a LinearLayout with a background colour of 'grey'.
Hope this works for you.

Categories

Resources