Android: Dynamically vary number of buttons in a custom View - android

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.

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.

Grid or relative layout

I want to have a page in an app have multiple buttons (in grid fashion) I was wondering if it would be better to populate a grid layout with buttons or manually add buttons to a relative layout, or if it even matters. I want each button to do something different so if using a grid layout is better how would I do this. I know you need to have gridview.setOnItemClickListener but that would make each button do the same thing (I think). Any suggestions would be appreciated!
If you have a fixed number of buttons, use a RelayiveLayout or TableLayout. If you have an unknown number of buttons and want to be able to scroll through them, that's the time to use a GridView.

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

Custom buttons arangement in Android

I'm trying to build an Android app where I would like to display some Buttons in various places, as in the demo image attached.
The challenge here is creating the custom buttons and arranging them.
As for the custom Buttons, I guess I could achieve that using CustomViews or a simple button with a Custom Drawable as Background.
Are these the right points to start, any other ideas?
On arranging them, I have no clue how to achieve that.
As Android_Crazy and Closeratio have already said, a RelativeLayout is the most suitable option for custom placement of buttons in general. However, for the exact placement of buttons pictured in your example, a LinearLayout would work just fine.
In a LinearLayout you may place views under or above eachother (with android:orientation = "vertical", relevant for your example) or next to each other (android:orientation = "horizontal"). You can also add margin to your views to alter the horizontal position (layout_marginLeft or layout_marginRight) or the vertical position (layout_marginTop or layout_marginBottom).
As for the buttons' appearance, I always use custom background drawables, usually with a custom xml to add a different drawable for when the button is being pressed or selected.

Which layout can provide free drag and drop insertion?

I'm using Eclipse graphical layout, and i want to Drag and Drop components to the layout and i want them to stay at the exact position that i dragged them.
Relative Layout is the closet thing but it changes the location of the components after couple insertion.
thanks.
Rami.
i want them to stay at the exact position that i dragged them.
No, you don't. Android devices come with screens in many different sized and resolutions, so fixed positioning of components is a recipe for making the application unusable to users with a different screen that you.
The point of layouts is that they can adjust to different screen sizes. Learn to use them properly.
You're looking for AbsoluteLayout, but it is deprecated.
RelativeLayout is the way to go, just pay some attention to the hints drawed on-screen when you place your elements.
You should be careful also to avoid circular references otherwise your items will be scattered randomly over the layout. (Example: Textview A on right of TextView B; TextView B on left of TextView A)
Be careful also when you change your element's id because the IDE does not update automatically all the references and thus your layout gets scattered again. When you update an ID you must update all its references in the XML file by hand.
Actually absolute layout is the answer what u want ,but that is deprecated.Relative layout does what you want.But it arrange its child based on parent position.If you drag a text view ,then other view will depend on this text view.If you have inserted 2 view,then third view will depend on the parent(2 views).But in between if u disturb any of the view ,it may affect other views.So do things systematically
In Java what we can typically use is GroupLayout. You may see what the equivalent is for android.

Categories

Resources