gridlayout or table layout - android

I am trying to create a candy crush like game. Correct me if I'm wrong, but the way I see the basic logic of candy crush, several rows and columns were created and objects (e.g. candies) are placed on each box, forming a pattern will erase previous objects and will be replaced by a new one. My question is, shall I use a grid or table layout? Shall I create an array for objects (e.g. food, crackers) where it will be placed? are buttons in android shall be used to swap it with different objects?
Thanks for the help. Android newbie here. :)

Definitely check out the official docs for your use-case.
GridLayout: http://developer.android.com/reference/android/widget/GridLayout.html
TableLayout: http://developer.android.com/reference/android/widget/TableLayout.html
I'm not familiar with the candy crush game but from your description I would use a Table Layout as it looks like every item you'll have will stay in its' assigned cell. GridLayouts are very helpful if you have Views that extend past their cell boundaries. Just in case you should decide to use a GridLayout here's a wonderful article on them: http://android-developers.blogspot.com/2011/11/new-layout-widgets-space-and-gridlayout.html
"Shall I create an array for objects (e.g. food, crackers) where it will be placed?" - That sounds like a fine thing to do to me!
"are buttons in android shall be used to swap it with different objects?" - Buttons are simply a type of View, replacing a Button with some other View inside a TableLayout should be pretty straight forward.
I know this was asked forever ago but maybe this could help someone? GL!

Related

Dynamically Android Activity Layout using SQL Database

I am asked to build a survey application on Android that may contain questions from the users, the question might be changed after some time and their answers as well.
the nature of answer might get in form of radio, checkboxes, drop-down or plain text.
I want to do this through SQL database. it will get the question from Master table and answer Option(s) from detail table and dynamically designs the activity layout accordingly.
Is that possible or is there any other way to do the same.
This can be done in many ways. You can create custom views for each type of layout and inflate the required view based on the type question you fetch from the Database.
You can also just use simple XML layouts instead of the custom views. The benefit of using custom views will be that you can predefine events and other things within the view which makes it easy to reuse.
Here are a couple of tutorials to give you an idea.
http://www.vogella.com/tutorials/AndroidCustomViews/article.html#tutorial_compoundcontrols3
https://www.intertech.com/Blog/android-custom-view-tutorial-part-1-combining-existing-views/
In all honesty you're not going to get any answer on here that provides you with nearly enough detail for how to do this if you're not sure how to start. It's simple to breakdown but a lot more complex than you realise.
However.. essentially you need to:
Create a DB table that contains questions.
Create a DB table that contains potential answers options. If answers
can be used for multiple questions, consider handling this
appropriately.
The question table should contain a column that details the question
type, i.e. radio button, checkbox etc...
The question table should link to another table which provides links
to the potential answers.
The above really needs to be well designed before you code anything. Think it through thoroughly and only use the above as a guide.
Your UI should be dynamic. Use something such as list/recycler view that will allow you to insert rows of data. You can then create XML layouts for checkbox answers, radio answers and insert these as appropriate.
Again, you need to consider, will you display all questions/answers in a list, will they be displayed one by one and moved onto... in a wizard style approach.
As I say, you will not get a fully detailed answer because a lot of this isn't coding, it's thinking and designing.
I hope the above helps you to get a rough and basic understanding of what you need to do and how to approach it. Don't be under any illusion that this is going to be quick to do. Spend quite a bit of time before coding to design this, because, coding it is easy... the design which is the most important part here is what will take the time.
I have done one project that consists only dynamic questions and answers like you.First You have to create Layout programmatically.
RelativeLayout layout = new RelativeLayout();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Add your rule to that layout.
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Then create your view and then set properties like below.
EditText edittext = new EditText()
editext.setId(1);
editext.setInputType(InputType.TYPE_CLASS_TEXT);
editext.setGravity(Gravity.TOP);
Thats' all you created a view programmatically. Then you can add to you child to the parent view.
layout.addView(edittext);
You can create multiple layouts and multiple views by doing this. just think about it. Don't forget to add your child Layout to your parent Layout. Keep coding..

Google Play Music style GridView

I'm looking to create a music app and I'd like to make a GridView similar to what Google Play uses where they inject elements that will span rows and columns like the Soilwork album does in this screenshot:
I've thought about using a ListView and populating rows with custom elements, but I couldn't think of a good way to use that with ViewHolder pattern, or really a way to make that reusable and account for differing number of items in width based on screen size (for example, the grid is only two items wide on phones, and "large" items span both columns and only one row).
I've also thought about using a ScrollView filled with custom ViewGroups, but that seems to run into the same issues I previously mentioned.
I next thought about using GridLayout, but that doesn't accept ListAdapters, and doesn't seem tuned to the kind of usage I'm looking at (nor does it seem to scroll)
I'd greatly appreciate if anyone could give me somewhere to start on this, or could point me to a library that does this. I've already checked out StaggeredGridView but it doesn't seem to accomplish what I'd like. My ideal solution would be a view which lays out items on an even grid like GridView and accepts view from a ListAdapter, also like GridView, but allow for elements to span, using the single cell constraint of GridView as the default behaviour.
Cheers.
EDIT
I have a perfectly functioning GridView as shown below, but I'd like to make items at regular intervals (every nth item) span more than one column and/or row, as shown in the previous screenshot.
Check out Parchment. GridDefinitionView may help you achieve the UI you are looking to build.

Need to make a table: which is better for performances?

In my app I download from the net some data I need to arrange in a table way.
I already know dimensions: about 26 rows and 6 columns, anyway data can change across subsequent calls so I have to create this table in a dynamic way.
As it is a table I though using a TableLayout (never used before), on the other hand I could simply use a list view, using for the single item a 6 elements linear layout with horizontal orientation.
ListView intrigues me because it is an adapter view (I think I'll store this data in a sqllite db, later) so populating it is quite simple and straightforward, anyway I don't know if it is the best for my problem.
What is your opinion?TableLayout, ListView or other?
When it comes to performance, I think you should use ListView because ListView support Recycling of view and you can check this and this post for more detail regarding Recycling of view.
Hope it helps :)
A ListView is a collection of layouts that are automatically displayed one below the other. The developer adds new items through a piece of code in the Activity. An example is the address book where you have lots of contacts one after the other.
A TableView on the other hand is defined in the layout file and does not only need to contain elements below or above each other but can also arrange elements on the left or on the right. A TableView is exactly what it says: A table with rows and columns.
here's a similar question that might answer what you want to know

Custom view in android

I need some valuable advice from you guys...
I have a UI where I have to drag objects from one view to another.
I have a rough sketch of this UI below
I think I have to create a custom view for this. I have to drag a square and a ball from bag 1 and bag2 [both bags are scrollable, can contain 10 to 60 items each] and drop it to the closet one by one. and later I have to find the number of items in the closet. The items in the closet must be arranged in a well maintained fashion [may be ...like 6 in a row].
Where should I start?
How many custom views should I use?
Is there a simple and effective UI solution for this?
Happy coding..!
I would suggest a design with three different GridView objects laid out inside a ViewGroup that supports dragging objects from one GridView to another. The objects would be custom ImageView subclasses so you'd have a place in code to support a drag-and-drop protocol of some sort and because you'd likely want to associate some data that is specific to your application with the objects being dragged.
As for the drag-and-drop protocol, you could consider an adaptation of the Android Launcher drag-and-drop framework or the current drag-drop classes described on the Android developers' website. I don't have much to say about the current drag-drop classes. I have not tried them yet, but understanding them is on my to-do list. However, I have done an adaptation of the Launcher code, and I have written it up on my blog and posted demo apps and source code there. See Drag-Drop for an Android GridView.
With the framework that originated in the Android Launcher, you have a good set of classes and interfaces to work with. Some of the objects include: DragLayer, DragSource, DropTarget, DragController, DragView. The DragLayer is a custom ViewGroup within which all drag-drop operations occur. It delegates handling of all the touch events to a DragController, which is the object that does the actual moving of objects around on the screen. As it does so, it interacts with DropTarget objects to give the user visual feedback that something is being dragged and that a place to drop something is available. A DropTarget is an object where something can be dropped. A DragSource is the interface for objects that can be dragged within the DragLayer. The Launcher framework is a good one because it gives you a way to think about dragging and dropping and how you want to divide up the responsibilities defined by the framework.
The reason I suggest GridViews for your problem is it sounds like you are thinking that way already. The closet has "maybe 6 in a row" so that could be a GridView with one or more rows depending on how screen space you have. A GridView would also work for the container that holds the squares and circles. If that is a good fit, you could study the code in my drag-drop tutorial and see if that makes it easy for you to move objects from one GridView to another.
In my demo program, I ended up with a custom subclass of ImageView that I called an ImageCell. These are views that are on the grid. An ImageCell allows objects to be dragged from them and onto them. For awhile as I worked on the tutorial, I had a custom GridView class too but ended up with the standard GridView. It sounds like you'd want a custom GridView because it sounds like being in the closet is different than being in the other sections. Having it gives you a convenient place for the methods you have not thought of yet.
I hope some of these suggestions prove useful.
Depends on the Android version you are targeting. For 11 and above you can use the built in drag and drop functionality, otherwise you are pretty much on your own. I would normally advice you to implement some sort of a long press action that might even allow you to select multiple items and move them in batches, which would be a really simple thing to implement.
If you really need DnD you should check out this example, it should give you some idea on how to make your own implementation.
I think that bag1, bag2 and closet can be three instance of the same configurable custom view (let's name it CustomBagView).
CustomBagViews should be responsible for displaying items (using a gridview of imageview for example).
I think those will not need to handle drag & drop directly but they should support
removing and adding elements
provide a setOnItemTouched(Interface_class) callback setter.
Then you will need to code a Container custom view (let's name it BagContainerView) that will contain the three bags and handle the dragging & dropping from the bags.
You will provide a handleDrag callback to each bag using CustomBagView.setOnItemTouched, then track the finger motion in it.
When the dragging finishes, you must find where it ends, locate the right 'customBagView' and ask it to add the item to its list.

Android DB List Adapters: multiple columns, aligned?

Ok I can't find a sample anywhere.
I've done the notepad tutorial on Google's Android site, but I would like to know how to add more fields to the list, in columns. At the moment I can add the columns no problem, but they're not aligned like you would a normal table on the layout:
john smith
heinrich cilliers
will peck
I would like the first names and last names aligned proportionately, as you would in an html table.
It works if I use a constant value for the layout_width parameter (100dip etc), but I would prefer to use a relative percentage. However it's becoming clear that each row is on it's own, and does not know how to alighn itself with the row above.
Any pointers?
UPDATE: I've reached an experienced Android developer which advised me to use WeightSum, which brings me closer, but vertical alignment is stil not happening:
I think therefor you have to extend the ListAdapter.
http://developer.android.com/reference/android/app/ListActivity.html

Categories

Resources