I have ListView. I would like to fill each row of ListView with a background from left to x% ...
So what I want is:
Item - 50% of background is colored
Item - 20% of background is colored
Item - 90% of background is colored
Is it possible to this?
Thanks for the answer!
As far as I know this is not possible out of the box. But I think you can archive is with a little trick.
You have to define a custom view for you rows. This view has a RelativeLayout as root element on which you can set the background color you want to have.
You then add another view element to the root element which covers a part of the root element. The background color of this element should be set to black and should be aligned to the right. The RelativeLayout provides many attributed you can use to arrange the elements.
Furthermore you have to write a custom adapter which handles the creation of the single list items views. When you override the getView() method where the view elements for each single row are created and inflated to the ListView, you calculate and set the width of the "cover"-element in such a ways that it covers a part of the root element.
The difficult part will be to calculate the width of the cover-element so that the rest of the root element which is not covered and still visible correspond to the percentage you want it to have.
Here you find a short tutorial about how to implement a custom Adapter with a custom view for the list items.
It is possible by the concept of custom layouts. This is a good article for to learn more. Also, I think you need to go through the NotePad tutorial in Android official site.
Related
I would like to create a grid layout in which each item is taking as much space as possible (minus padding), but only as long as there are columns available (after that the next item would be inserted in the next row while keeping the size). Additionally, each item must be a square and is added dynamically.
Example layout with 10 items would be as follows:
I have tried to achieve this by setting weights, ratio constraints, overriding onMeasure - but I just can't get it to work. I would be happy with either a programmatic or an XML-based solution (as long as each item can be added programmatically). I would prefer the solution to be in Kotlin, but I would be happy with a Java-based one as well.
It's probably worth saying that each item in the grid layout is a layout (RelativeLayout as of now) to make inflating it and setting a layered background drawable programmatically easy.
I think you might be able achieve what you want with a different Layout
Have a look at https://github.com/google/flexbox-layout it has lots of methods to control how the cells grown or shrink and includes automatic or manual wrapping of cells.
Take a look at RecyclerView. You would need to pass GridLayoutManager. This tutorial may or may not help you. For square items, I suggest using CardView but it's not necessary. If you are targeting tablets as well as smartphones, check this out. And for dynamically adding new items, you should notify recyclerView's adapter. See this link. You can also extend RecyclerView or GridLayoutManager for more control over items.
Right now I'm stuck how to manage to build a specific Activity in my app. I've added an image so I can explain my problem:
So first of all: all the content will be loaded from an API. "Static text" in my image means that I can define these parts in my activity.xml and don't have to do that in my Activity.java because these parts will be always the same for the screen (meaning the size of the elements, the content will be loaded from my API).
The green box should be horizontal scrollable or not depending how many boxes have to be shown here (1 to 3 possible).
The blue box will be generated in my Activity (in the end it should look like a table) and I want to define the layout of a single row in a separate xml (e.g. table_row.xml) so I could change it easily. This table can have up to 100 rows depending on how many are returned by the API.
So my problem right now is: Obviously this whole layout has to be scrollable so my first idea was to use ScrollView and a LinearLayout as child. But I read here on stackoverflow that the performance will be really poor if you use LinearLayout and add Views to it. So everyone recommended using a ListView for this part (meaning the blue box for my Activity). But that would mean only my blue box will be scrollable as you should not use a ListView in a ScrollView.
So my question is: How can I make this whole screen scrollable with a table dynamic in size without losing performance?
Put the first three layouts as ListView Header and make your blue box layout as the list view. By this you'll be able to scroll the complete View i.e. Blue Box, however the first three layouts will be static and won't scroll.
I have an Activity which is displaying an assortment of details about a particular widget. There is an image, a TextView for a description, some common stats in TextViews, and then a
variable sized list of categorised items, from 1 to hundreds.
Quick wireframe: http://i.imgur.com/Z4kY6Ky.png
My first assumption was to use a ScrollLayout containing all of these elements: ImageView, TextViews, ExtendableListView, which works only if I specify a height for the ExtendableListView. Since the height is variable this is not a solution. I'm also now aware it's not recommended to use ListViews inside ScrollLayouts.
So I'm stuck. Can anyone point me toward the best way to achieve this kind of layout?
Just put expandable list view as main component, and then put the header into the list. addHeaderView()
I have a working example of a grid that allows items to be reordered using long touches to active a drag-n-drop. All is working well if the items are simple Views e.g. TextView or ImageView but if the items are LinearLayouts only the layout itself is displayed.
I've been using Tom Quinsn's grid (thanks Tom!!!) from this posting:
Android Gridview drag and drop example
I can get LinearLayouts to work if I derive my own LinearLayout class and override onLayout(), but this forces me to hardcode the positions of the child controls in the layout within this function.
Ideally I would like to be able to define the item layout within an XML file and inflate them before adding them to the Control that handles the grid. I'm guessing that for some reason the framework is not calling the layout function for the children contained within the DraggableGridView view as defined in Tom's code but I can't understand why that is.
I am developing my own Drag and Drop app with good help from this link. You may compare the code with the one from Tom Quesinsn. It also gets the different children of a GridView and add them as "drop targets" that accepts drops on them and copy the Image of the View you are dragging.
This is a known problem with the DraggableGridView that -- unfortunately -- I haven't gotten around to fixing. When I wrote DGV, I didn't entirely grasp how views were laid out. You might try having DGV measure each child before laying it out. Adding something like:
getChildAt(i).measure(MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.EXACTLY));
before the layout on this line:
getChildAt(i).layout(xy.x, xy.y, xy.x + childSize, xy.y + childSize);
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.