How to display different (live) elements in a grid layout? - android

What I'd like to do is create a grid layout that has multiple live components. I'm trying to implement this layout, and am struggling to figure out the best way of doing it.
The 4 main elements of the grid layout:
Imageview (the green bar). It changes colours to green/ red/ or yellow, depending on the delay.
Textview (the title). The title changes based on an update call. It's the recommended title.
Textview (Delay). Will be a string with the delay status (either No delay or 30 Minutes)
Imageview (background for delay text). Will never change.
After reading about this on StackOverflow, an embedded grid layout seems to be the best choice, but I haven't seen a good way of incorporating different elements (that are set every new "Update"/ pageload) into a Grid Layout without having multiple views set for each element.
Here is the picture I want to implement (http://imgur.com/8TrFvl8)

Perhaps consider using a recycler view instead

Related

Ideas how to build a screen with 100 Views added programatically

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.

How to overlap views?

I am making a calendar. Each day is a textview with a number denoting the day of the month.
I'd like to overlay another view which signals that there is an event on that date. See below image or think about how the google calendar app looks. How do I do this?
One way is to make the two textview inside a Relative Layout or a Frame Layout. You can look at this for reference.
You could use a TableLayout to store the TextViews in rows and colums.
A possible way of marking a special day would be changing the background resource of the specific cell with one that has a marking on it, that does not overlap with the number.
If you want more fancy stuff, you could write your custom View subclassing TextView, which handles its onDraw calls and everything else.
As far as I know you can use a RelativeLayout to overlap views.
You can try setting left drawable (android:drawableLeft="") to the TextView. OR, you can use some transparent background images with indicators drawn in top-left.

Android: Dynamically vary number of buttons in a custom View

I am learning Android by implementing a clone of Mastermind. I want to break up the screen (or View) into three parts: the board with the users guesses so far and feedback, a series of control buttons, and a series of buttons to pick the color of the next peg.
My instinct is to do this is a modular way. The layout files uses nested LinearLayouts (I know not the most efficient thing to do, but this is an educational experience.)
The "board" is a custom View where I do a lot of drawing with a Canvas. The buttons on the bottom are declared in the layout file. Notice the orange strip to the right?
Right now that is another custom View. I want to add a variable number of buttons to that custom View based on the number of colors the player can choose from. A button press would select the color for the next peg in the player's guess. (There are 3 versions of the game, easy, medium, and hard each with a different number of colors.)
So, how do I add a variable number of buttons to the custom View I am creating? Or am I approaching this in the wrong way? Should I use a prebuilt layout? If so, which one and how could I dynamically change the number of buttons in the layout?
Thanks for any help. Cheers!
You can do this in two ways:
Using a predefined layout and setting initially the property
"visibility" of all the buttons to "gone", then programatically you
can set the "visibility" of the buttons you need to "visible". The
"gone" property makes the button invisible and also not consumes
space in the layout.
Adding dinamically buttons to the main layout, first you will have to
create or "inflate" them.
The second options is more powerful, but also more difficult if you are learning.

How to set the background of the body of ListView (content part in the middle but not the header or foot)?

Here I want to separately set the background of header part and the body part of a ListView, like a rectangle with white background color and light blue bound for each.
But I cannot find the way to set the background for the BODY part of ListView.
When I call the listview.setBackgroundResource(bg), the parameter background wraps the whole of listview, I cannot just set the background of the BODY part.
This is a classic example of the power of Android's ListView. you have a case where different list items have different views (in your case the only different is the background).
I recommend you to view Google I/O 2010 video about listviews mainly you need to focus around getViewType method

Implementing Android Action Bar

I am working on creating an Action Bar like the one from the new Android UI Patterns and I am running into a bit of trouble. I have a ViewSwitcher with two layouts in it. When the user taps the search button I animate between the two layouts. The problem is that the layouts are different sizes and I can't figure out how to make them take up the same amount of space. Here's what I mean. p.s. forgive the bad art, they are just place holders ;)
The red box is right up against the ViewSwitcher and there is a gap between that and the action bar for this layout:
but not this one:
What I want is to tell the layouts in the ViewSwitcher to be the same size. How can I do this?
You can try setting android:minHeight on the smaller one to be whatever the height of the larger one is. Or maybe setting it on the ViewSwitcher itself.
Idea:
The ViewSwitcher only lets you add two Views and in your case they are two ViewGroups. You can call View#measure on each of the views you're adding to the ViewSwitcher. It looks like you should use ViewGroup.LayoutParams.WRAP_CONTENT for each parameter to measure. Then you can call View.getMeasuredHeight on each view. What ever is the larger set the smaller view using View#setMinHeight.

Categories

Resources