Android : Creating GUI programmatically at run time in java - java

I have to create number of buttons and textbox, etc. depending upon a certain number. For ex: if the number = 5, I need to create 5 buttons, and if its value is 10, I need to create 10 buttons.
To achieve such a functionality, the conventional xml GUI won't make it up. I need to develop the GUI dynamically. How can I do that?

Take a look at RecyclerView. You need to apply the DataSet (what u receive dynamclly) and then let the Adapter handle all of the Binding.
Take a look at this tutorial, there are many more.
Notice that you will need to create TWO Viewholder (button and a textview) and override the getItemViewType

Add a Linear/Relative layout in xml and on run time according to given number add view(buttons and text views) in this layout.
See this tuts:
https://androiddesk.wordpress.com/2012/08/05/creating-dynamic-views-in-android/
http://www.javacodegeeks.com/2012/09/android-dynamic-and-xml-layout.html

Related

Dynamic amount of entries in GUI using java

I'm working on creating a basic user interface and I wanted to try and create a portion that is in a scrollTaskPane and is capable of holding multiple entries. As I'm going about creating it I can obviously test it with a simple amount of entries but I'm confused how I can go about later allowing for it to take input to create entries in the scrollTaskPane of maybe 1 entry one time, and then later needing to allow for input of 20 entries. I only know how to use absolute positioning and am trying to figure out the best way to go about it. I also need to later be able to select each entry.
For the entries that will eventually be called and displayed in my interface, I'm planning to store them in a simple text file and use a semicolon as a delimiter between the task "Type" "Name" "Description"(which will be accessible through a button) and "Due Date". Or I may try to learn to use a database for the information. But I haven't decided yet and don't know anything about connecting a database with a java program.
This is the current look (the scrollTaskPane in the middle). And my goal is to put in entries that are each rectangle boxes going across the scrollTaskPane with a checkbox on the end of them. Should I use some sort of grid layout? Or something else? I'm a beginner at user interfaces, so any help is appreciated!
You can make a custom layout, and then keep adding those layout. So extend a layout class, add TextField and a check box in the layout. Initialize the layout with your values, add then add to the ScrollTaskPane.

How to create Options for activity

i have a couple of questions about activity's
i have a activity which i want to create some options for it. for eg. i want the user to be able to change the font size of text views.
i am a noob in java and eclipse. first i thought i can change the xml values via java but then i found out that i can't and they are read only.
so what is the best solution for creating options which are visual like changing colors and font sizes and picture through entire project?
for eg. i have 10 activities and inside each activity i have some text views. i want to change all of the font sizes. in xml you can create a dimen and all of the text views with android:fontSize:"#dimen/example will have the same size. but in java it takes more code and time.
what should i do ? couple of examples would be nice
thanks in advance
so what is the best solution for creating options which are visual
like changing colors and font sizes and picture through entire
project?
Have a look at the setTextSize() method of the TextView class. This will let you set the size of the text programatically.
setTextColor() will let you change the color.
setTypeFace() will let you change the font.
Having said that about TextView, it applies to all the subclasses of TextView like Button, EditText, etc.
i have a activity which i want to create some options for it.
I suggest you create a singleton instance that encapsulates all the information to change the visuals of your activities like text size, typeface, etc. This instance should then be shared among all of your Activitys.
couple of examples would be nice
No, an SSCCE would be nice. We need to see what you have tried. Code or you didn't do prior research.

Arrange nested layouts in Android

I want to create a layout that looks like this:
So a few different categories and in each of that, 4 elements should be positioned.
I have tried to make a ScrollView first (because it should be possible to scroll) and then having vertical and horizontal LinearLayouts nested into each other. But it doesn't work.
I don't want to use the GridLayout because this requires API 14 and higher, and my app should work for the API 10 or lower.
Can anyone help?
It is an example of a type of custom ListView.So instead of what you are doing now, try creating a custom adapter for adding labels and buttons to each of your row items within the list as per your needs you have given in your diagram link.
Learn more about it here

GWT RadioButton Grouping

In my application, I have URN-identified data coming in from the server. I'm in the process of abstracting as far as possible so there is very little to no logical code in my views, and I'm using a generic presenter that wraps those views. All widgets have URNs, making it super easy to map incoming data to a specific widget (until now, a 1 to 1 relationship). This has worked well for pretty much every widget, and now I've reached a point where I'm tripped up.
Assume I have (just for simplicity's sake) two RadioButton elements on a view. These buttons belong to a "group" (just by setting their name values to the same thing), but obviously they're 2 distinct elements. I can't map my URN-identified data to a single widget as in every other case because, in this case, it is two widgets.
Here's an example of what I mean:
Utility Company is a ListBox, so just one widget there. I map each item in the list to a specific Enum value.
Utility Rate is a TextBox, so again just one widget to map.
For Energy Usage, they can select to use either an average for the year or input 12 monthly values. I'm stuck here. I can't map to just one of the RadioButton elements, because then I'd need some extra logic in the view to handle the behavior appropriately.
Am I stuck mapping to just one widget and sticking (unwanted) logic in my view to determine what the state of all of the elements should be based on the value that came in for the one widget that is mapped?
How should I handle this case?
Edit (Solution):
Following the concepts of jusio's answer, I came up with a workable solution. Because I didn't want to go sticking special case handling through my logic to take care of a non-widget, I created a RadioButtonSet faux widget (public class RadioButtonSet <T extends Enum<?> & HasDisplayText> extends Widget implements HasValueChangeHandlers<T>, HasValue<T>), into which I manually pass the radios I intend to group. Having done that, I can get or set its value and have it fire appropriate events when the user changes the selection. Then mapping the collection of radios is no different than doing so for a listbox. Thanks jusio.
I believe in your case you shouldn't treat radio buttons as two separate widgets, basically in your case you can treat the radio button group as combo box, because behavior is almost the same (the only problem is that you have additional master detail). So basically what you will have to do is to wrap real BO objects into some kind of RadioButtonGroupModel, and give it to view, view can take this model and generate radio buttons (with some editors or whatever else). I remember running into this problem when i was extending databinding FW for JFace, and this was the best way I could find to solve this problem.
If I understood correctly the problem, there are 2 possible solutions:
Give each RadioButton a unique URN (ex: oldURN_1 , oldURN_2)
When you send data for a URN, disable the other one
Keep the same Name for each RadioButton but add a number variable in the data the server sends indicating which radioButton it is supposed to use (ex: 0 for Average and 1 for Monthly)

Question about creating GUIS "dynamically" on Android

I'm trying to make an android app (I'm new in the Android programming world), and I'm having problems creating the GUI.
The point is that I get information of some data that is divided in days... The problem is that I don't know, until I retrieve the information, how many days the GUI should display.
http://img574.imageshack.us/img574/3787/mainscreen.jpg
The grey part will be a TextView, and, also, the black part will be another TextView with multiple lines.
So, the point is, how can I do to have multiple TextView's without knowing before the exact number? I suppose that I can't declare them in the layout.xml
The only solution that I've been thinking about is to create in the layout 7 pairs of TextView and, when I know the exact number, just use what I have to, and don't use the others... (It's a bad solution)
What do you suggest?
Thank you for your answers!
You should create a ListView, which inflates TextView for the items you have.
You can use this example of how to create sectioned ListView, which will look exactly like you want.
I'm not familiar with Android, so other people may offer better, more specific advice.
In the environments I'm familiar with, the problem of displaying an unknown number of items is solved by using not a series of display elements for each data item but a list control. The list component will display as many items as you give it, and can usually be modified to have different appearance for different data, so you're flexible in making it as pretty as you want.
Well you can consider this one also,
Create a table layout in XML, give it a id: TableLayout table=(TableLayout)findViewById(r.id. ....)
Create dynamic TextViews using: TextView day=new TextView(this); day.setText(day name);
Now add this text view to your table layout : table.addView(day);
Run the code for textView creation and addition to table in loop
Hope this helps...............

Categories