ListView row marker ala GMail - android

I am interested in creating a ListView where each row is marked the way it is done in GMail for 3.0+. This creates a nice separation of the left and right ListFragment.
Other examples include also Google Calendar on 2.3.4 for instance where a color marker is on the left of the ListView.
See the grey vertical divider between the two lists. How does one achive something like this? A bonus would be also the alternating width, but I guess that is only a smaller layout change.
I know I could probably do something like inserting an ImageView in there and then fill it with the color I would like but it seems to me that this is an ugly hack.
Another question would be also if there is a generalized way to combine the two ListView fragments somehow the way the GMail or Mail applications do it.

If you want speed, then the option I would go for is to use a custom View class (e.g. extend RelativeLayout) for the row container View and override the dispatchDraw(Canvas canvas) method.
The dispatchDraw method is called after the View has drawn its own contents and before it draws its children - the children are drawn when you call super.dispatchDraw.
Use this to do something like
private boolean mDrawMarker = false;
public void setShouldDrawMarker(boolean drawMarker) {
mDrawMarker = drawMarker;
}
public boolean getShouldDrawMarker() {
return mDrawMarker;
}
#Override
public void dispatchDraw(Canvas canvas) {
// draw the children of our view
super.dispatchDraw(canvas);
// draw our marker on top of the children if needed
if (mDrawMarker) {
// e.g. canvas.drawRect(...) or canvas.drawBitmap(...)
}
}
This way you avoid adding any extra views to the hierarchy which means you won't incur any penalty in the layout or measuring phases. Remember to re-use Paint and Rect objects if drawing a rectangle rather than creating a new one each time. Similarly if you use a bitmap you should share the same Bitmap instance across all instances of your View rather than loading a new one from your resources each time (this does not mean putting them in static fields)
For the indentation of the items, since in this case the lists don't seem to be overlapping you could (off the top of my head):
Set a left margin on the row container (not totally sure this will work)
Wrap the row container in a LinearLayout and set the left padding on this (if the above doesn't work)
Use a custom view class (if setting the left margin doesn't work)
Go with #commonsware suggestion and use two Views - one on the left with the grey background color and another to the right of that with the marker color - then just set the view on the left to visible/gone if you want indentation/no-indentation
As for the overlapping of the Views in the second example, I'll defer to #commonsware answer.

Please tell me if I have understood the problem correctly.... You would like to mark certain rows as selected, and selected rows appear to be indented visually (with a different colour and margin)?
Here are 2 techniques:
1 - Using a StateListDrawable for the row background:
If so I would create a list row layout file and set the "background" property to a StateListDrawable (can be XML). This will allow the row to switch visual states for selected and not selected.
The "drawable" attribute of the StateListDrawable would be a 9-patch PNG, one for the un-selected state that includes no margin, and one for the selected... the selected one would define a margin within the PNG itself, by specifying the content-area/bottom black line to not extend fully to the left hand side of the PNG, leaving a region which is your unscaled margin.
For the benefit of people finding this, Radley Marx just posted an excellent post on 9 patch: http://radleymarx.com/blog/simple-guide-to-9-patch/
With ListViews it's sometimes the case you want to turn off the "listSelector" (which is a separate entity either rendered above the list, or behind) and instead use the "duplicateParentState" attribute to allow the row itself to display the selection (no list selector needed). This can provide a bit more creative freedom, especially when you want to have variable-width margins on certain rows, or several types of row that all look different. Totally depends on the design though.
2 - Using a margin for each row:
If you decided you needed multiple types of colour indicator and so on, you may have to use a different approach, providing a margin attribute (which probably won't just work straight away)... This relates to how LayoutParams are used by the layout system. I'm trying to remember the exact details, but I think this is due to the different types of LayoutParam subclass, and the properties of a MarginLayoutParam (or subclass of that) e.g. marginLeft may be ignored by the layout code of the ListView. You should be using an instance of AbsListView.LayoutParams, which includes no options for margins. One way is to nest your row inside a container View (subclass) which does allow for margins in its LayoutParams*. I'm certain I didn't end up doing this extraneous-nesting but I'll have to dig into some code to remember the better solution.
You mention putting an ImageView and filling it with colour. There are a couple of alternatives you could look at... The most performant would probably be to define your own ListRow class and use onDraw() to actually draw the row contents yourself, canvas.draw_xyz() to paint the little colour tab, and draw text etc for the rest, rather than build the row in a composite layout. The second method using layouts would be to have a lighter-weight <View layout_height="match_parent" layout_width="4dip" background="#ffff0000" /> for example.
*A golden rule in Android layout: Complex UI hierarchy is death to performance, especially with things like ListView. It's often possible to avoid this by using other things: RelativeLayout, drawableTop(etc), 9-patch images, rather than adding more Views.
If I have misunderstood and the above is just too basic please could you provide some more detail, maybe a diagram indicating the exact part you need to reproduce.

How does one achive something like this?
Off the cuff, I'd use a View with the desired background color, visible when you want it, invisible when you don't.
A bonus would be also the alternating width, but I guess that is only a smaller layout change.
I do not know what you mean by "alternating width". I think there are two Views with the desired background color in a horizontal LinearLayout, with only one visible.
I know I could probably do something like inserting an ImageView in there and then fill it with the color I would like but it seems to me that this is an ugly hack.
Well, ImageView is a bit heavier than is needed. Otherwise, I fail to see why this is a hack. Think of them as icons that simply happen to be tall, thin, gray, and not always needed.
Another question would be also if there is a generalized way to combine the two ListView fragments somehow the way the GMail or Mail applications do it.
Use a RelativeLayout, so the right-hand fragment can float over the left-hand fragment.

Related

Embed elements of a RecyclerView inside CardViews

I have a RecyclerView that display the list of modifications per day for a document. Items are TextView with values like "Line 3 updated to ...", "Line 5 removed", etc. The ViewHolder handle 2 types of views :
Headers : which are the days. These are simple TextView too that are in bold, larger, etc.
Logs: that's what I was talking about : "Line 3 updated ...", etc.
What I would like is that each "day", with its corresponding logs are embedded inside a CardView. But a document can have huge number of modifications per day (>100). So programmatically creating a layout with a CardView as the root, calling 100 times addView() on it to add each logs and then passing this layout to the RecyclerView does not seems a good idea to me.
Is their any way to delimit between a "positionStart" and a "positionEnd" views that will be embedded inside a CardView? It seems to me that this isn't possible or by adding each TextView programmatically inside a CardView but it will then slow down the binding of the views and break the ViewHolder pattern. Am I right or is their a solution I didn't think about ?
You have 3 options to achieve this "grouped in a card" behavior.
(as mentioned) you create the layout yourself and put the whole thing into the recyclerview. This is probably the worst solution since it negates the idea of a recyclerview in the first place.
You just wrap each of your items in a CardView (and set the corner radius to 0dp).
On < 21 devices (I think) there will be some additional padding and every item will appear as its own card, but on higher API versions those cards will lie next to each other and just have some "seam" between them. The shadow on the corner is also a bit buggy, but this is probably the easiest and cheapest solution.
Alternatively you can also create a custom view that fixes the errors mentioned above (margins between and shadow) and use your own to wrap the views. (I believe this is what the Inbox app does if I recall correctly, which also features lists in cards.)
You use an ItemDecoration. For this approach you need a kind of stable setup of your dataset, but if its just the headers and logs, you can draw a shadow above the header, draw borders to the left and right of every item, and draw a shadow beneath the last log. This will also require some setup, and if you introduce further view types you will also have to modify this code (it's highly dependent on your data set)
The 1. method is probably the worst idea. It will work for small lists.
The 2. method can work, but you either will have to create your own custom view or live with a "bugged" version on lower api levels.
The 3. method is something I tried once for fun, and will work, but you will have some additional dependency between your data, your adapter, and your decoration. You can see an example of this decoration here on GitHub. It just draws a shadow around all of the items.

Drawing a tournament bracket in android?

I have an app that will take a list of players and set them up so that you can go through and create a bracket dynamically. The number of players will change and the names as well.
Instead of showing a whole bracket, my point will be to go in columns. I.e. instead of the left side of the arrow, it show the right side:
My questions is how do I best draw this?
My initial thought is to do multiple layouts where I hard code textviews with custom drawables and lines for each column of 16/8/4/2 with just one border, but then I have to constantly change the layout setup.
My second one would be to dynamically draw it on a canvas based on this answer here, but it didn't give a lot of ideas of implementations.
My Questions: If I choose method 1, will I be able to dynamically change the layout of my activity?
If I choose method 2, how could I go about this? I'm still pretty new to android.
As for question 1, yes you can absolutely change the layout dynamically. You can create several layout files and do one of two things: The first, is you can use your activity's setContentView(int) method to completely reset the activity's layout to a new view. The second would be to identify the parent view of the bracket portion, cast it to a ViewGroup and use addView(View) and removeView(View).
As for question 2, you could go about using a canvas, but it would be quite difficult to use only a canvas, but possibly a combination of a canvas and a set of text views could work. While this isn't directly answering your question, this tutorial is one I have used in the past, and gives you an excellent sense of how to work with the Canvas class in android.

iOS: Relative Positioning like Android's layouts

Is there any way position views relative to each other like you can with Android layouts?
Example: You have two UILabels that are dynamically set to strings of variable length, one above the other, and you want the bottom label to appear directly below the last line of the top label, regardless of how many lines the top label ends up having.
Another example: Same situation as above, but one of the labels is sometimes hidden. You want the resulting label(s) to be centered vertically in the parent, regardless of whether it's one label or two labels.
Android's Linear Layout and Relative Layout make this very easy to do, but I can't figure out how to do this is iOS. Can it be done?
You will be able to do this using AutoLayout in iOS6. For an application that should run in iOS before 6.0, you have to do it by yourself, but this is not very complicated.
Actually I have implemented a class to do this (that's a long time ago, I hope it still works, but there is no reason not to). This OHStackView class is a subclass of UIView that automatically layout its subviews horizontally or vertically to stack or align them.
You can ask OHStackView to stack its subviews horizontally or vertically (one above the other, etc), or align their top/bottom/left/right borders or their centers, and even specify a padding between each subviews. Each time one of the subview changes its frame or size, OHStackView will automatically relayout all depending views to realign everything.
(E.g with your two UILabels, a simple call to sizeToFit on your labels to make them adjust their size to their content will relayout everything around automagically)
There is an example project provided so feel free to test it.
Note: IIRC, my subclass does not take the "hidden" property of the subviews into account. But you can easily add support to this behavior by adding a condition like if (v.hidden) continue; in the for loop of its layoutSubview implementation to only take non-hidden views into account in the layout algorithm.
HTH
I've been searching if it's possible for a long time. As far as i can tell, it's not possible for now. I don't remember where i read this,but it will be possible with ios6. Instead of using Android's Linear Layout,you can use sizeWithFont method to detect size of your UILabel,then you can set their frame to position them.First,you need to set their text of course to find their size according to their font family. Another thing you can use is sizeToFit method. Unfortunately,i don't know how to use it. You can give a shot,though. At that time,i found this.Maybe ,you can use it https://github.com/scalessec/CSLinearLayoutView

Is there any way to have UI elements slightly overlap (one on top of one another) without using absolute layouts?

Lets say I have a list on screen, which I always want to be usable. I also want a small image or textview to slightly overlap the listview. Is anything this possible without using absolute layout parameters?
I've never seen it in any android app or tutorial, but there are things like this in many iPhone apps. It adds a nice touch.
(Also, I don't have any code to show because I'm not sure where to start)
Relative Layouts also allow things to overlap. Views declared later in the xml will be on top. I believe that aligning view edges and use of margins should allow you to achieve this affect without great difficulty.
You could use RelativeLayout and set for example android:layout_marginTop="-50dip" android:layout_below="#id/my_list".
As well as RelativeLayouts, you can also use FrameLayouts to stack objects. Other than the z-order (last object declared = highest z-order), the child objects don't depend on the positioning of other objects in the group, so you can just set margins or gravity to position them.
So in your instance, just declare a TextView after your ListView, and position it wherever you want. It won't interfere with the ListView's positioning, and it will sit on top.

grouping texts plus images + round edges in android

i was requested to make in android a view that groups several items like checkboxes or text views in vertical rows, separated by transparent dividers while the background is with a certain alpha level and the edges are round.
I thought of two solutions and i hope for some feedback on good\bad or other solutions if you got'em.
just use regualr linear layout but have a single style A that uses a 9 patch as background, includes padding,margins and whatever i need to make it look like what i want. i then create another style A.up and A.down that represents the upper most and lower most items that will use a different 9-path with round corners.
inherit from linear layout, in the onMeasure and layoutChildren add to all the children some kind of space between them, i can create new attribute for it that can be customized in a style. i can override the dispatchDraw to paint the background for each view before it draws so i can paint my round borders, my only demand will be that each View added to this layout will have to be with transparent background.
So what do you think ?
Eventually i decided to use a List with customized divider.
It looks good, however a list got a very nasty bug when it comes down to items with states like buttons and clickable textViews,
you get no focus for the item and don't see the ornage bar
you don't seem to get the evnets flowing to the children of the View in the list.
I'm notsure how to resolve that one, i've seen numerous mails about it in the developres mailing list and here, most saying don't put statefull objects in a list.
So it mist not be the solution for me.
Nest thing i'll try is extending the normal layouts to have a bar in their bottom and use regualr linear layout with round corners drawable.

Categories

Resources