Dynamically Android Activity Layout using SQL Database - android

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..

Related

Is it better to create multiple layout files for one activity or change a layout programmatically?

I'm currently creating a Quiz section for an app. This is what it looks like for the most part:
My problem is that the number of answers for each question can vary from 2-4. I can see two solutions for this:
Create multiple layouts with the correct number of answers. When the user moves to the next question, I change the layout according to how many answers there are for that question.
Have one layout for the activity. Determine how many answers there are for a question and alter the layout programmatically.
Which of the two is the better route, or is there an even better alternative?
The best solution for your problem is to use a ListView or a RecyclerView because these components are dynamic and can display from 0 to n views perfectly.
ListView: https://developer.android.com/reference/android/widget/ListView.html
RecyclerView: https://developer.android.com/reference/android/support/v7/recyclerview/package-summary.html
After use it, you are able to control when your user click.
Use a recyclerview Creating android recyclerview
Definitely the 2nd approach is better,
Why? Because in 1st approach you will be inflating the entire layout for each question. With 2nd approach you can just change the content of TextView and the adapter of RecylerView that publishes your answer.

Listview filling from database

good evening , I need help about filling a list view from a MySQL Database,
i don't know how to do it becaus i'm beginner in android
i wanna icon and 3 text views the first is title bold and the second is the RIB and the last one is in the bottom and right of the list view
Your question is a bit too generic and not clearly defined. That's probably why it got down voted. I'd say your best bet is to start with the documentation. Start by reading about ListView and possibly Adapter and try it yourself. If you still have problems, ask a specific question about something you don't understand or can't get working. The best specific answer I can give is that you will need to define a custom adapter to create the item view you want. You will most likely want a layout xml for that item view.

What is the best way to change between different types in a android quiz?

For a school project a need to build a android quiz application with multiple question types. These question types are: Multiple Choice, Textbox, Radio button and a minigame in which you need to sort images in the right order in a draggable gridview.
I think to use multiple activities for this isn't needed and that i just can switch the overall layout between multiple xml layout files for each of these question types if i first check what the type of the question is. Most other posts on stackoverflow are only related to quizzes with one type of question, without minigames in between the questions so i wanted to know what the best method is to switch between these different question types and layouts. At least i know i need a switch/case to check the different question types (for my prototype located in a array) and switch to right type if you press the "Next question" Button that is located under the answers (under the gridview in case of the minigame).
I already searched on the internet for it and i found the following options but i don't know what's best in my case. I also wanted to make the app compatible with android version 2.3.3. Some examples could also help me very much!!
(Adapter)Viewflipper
An Adapter that changes a inner layout
setContentView() of the main activity
Including a layout in another layout and change it
Make some views/layouts visible and invisible.
Make a new activity for each question (if there is no other option)
You can do it with a ViewFlipper. In the ViewFlipper XML, just list each layout for each question type and just have the user scroll through the flipper to get to the type they want. You can probably do this by setting a OnTouchListener and calling showNext() as they scroll.
Setting the correct data for each view is solely up to you; you would just have to match the correct data based on what view they are in to populate it correctly. That's it :)
It depends on how you want to structure your quizes are the questions supposed to be mixed together? Are they all seperate quizes.
If they are separate the easiest approach would probably to implement a view pager. With the view pager it will create a new fragment for each page. From here you have a two options, you can either create a new fragment for each quiz type. Or you can create one quiz fragment and pass an argument to that fragment to select the correct layout. Either way you'll likely want a new layout for each quiz type. As for your minigame you can just include that as a page in your view pager in between two quiz views.
All of the above will work on 2.3 and above if you use the support library. I would recommend checking out some tutorials on viewPager they are fairly straightforward to use.

gridlayout or table layout

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!

android database value retrieving

I have a database with three rows and columns.I have managed to insert the data into the table.Now i am trying to retrieve the values in a tabular format in my xml view.
How to go about this.
Any help is appreciated.
Thanks.
What you're asking for is a fair amount of work, but saying that, it's relatively straight forward.
The problem is, your request is so vague that people are reluctant to help.
One possible solution (although it might not fit what you need) would be:
1) Create a ListActivity
2) Create a custom SimpleCursorAdapter.
3) Create a custom xml view for each row.
4) override SimpleCursorAdapter's getView() to inflate said xml view
5) Override SimpleCursorAdapter's bindView() method to assign values from the cursor to the row.
The questions in my mind which stopped me from trying to code this for you are:
1) Might you want to change the number of columns dynamically?
2) Might you want more flexibility as to how the column widths are decided upon? etc..
A slightly more complex setup would use TableLayout and TableRow.

Categories

Resources