Making UI elements dynamically in android apps in android studio using java - java

I am wondering if it is possible to create UI elements dynamically in android apps without using xml or design? The number of elements and type of elements will be different based on the requirements I get from a json array. So, can I create buttons, textviews, etc in my activityClass file, without actually having them in my xml file??
if yes, then how complicated would it be? can you provide an example please?
Also, are there any libraries for android that I can use that would just dynamically create the fields for me based on types and number of fields as an input?

Yes. You just create the View objects using new, then add them to their parent layouts using .addView(newView). If necessary, add them with the correct LayoutParams object.
I will say that this is MUCH harder to write and debug than xml, so it should be done only if something absolutely has to be manual. Even if you are getting things from a JSON blob, its best to make as much of that just deciding what xml to inflate as possible.

Related

Best way of adding id's to html elements in GWT

I'm rather unexperienced in GWT, and I have large codebase with working project in this technology. My task refers to assigning id's to html elements witch will be used in automatic testing. We can't use some dynamically assigned id's because in automatic test we have to specify exact values of id's. My way for now was to use method ensureDebug(id), written by hand in code for specific elements.
I think that doing it this way mean that code will be more spaghetti-like, with mixed ensureDebug(id) methods usages there and here. I was thinking if there is any way of doing it that will be more manageable and cleaner than current. Is is maybe possible to use AOP? (I have never used AOP, so I don't know if it is any good idea, or possible in GWT) Or maybe other way than using ensureDebug?
You also can set the IDs for HTML elements like
element.setId("myId");
But this is as much spaghetti like as your approach adding the IDs in the code.
Another possibility would be to use an UiBinder and set the id there. With this approach you have all your ui elements of one view, which should have an id, at one place. With bootstrap for example it would look like this:
<b:TextBox ui:field="searchTextBox" b:id="search-text-box"/>
Like this you can access the field in your view-class via searchTextBox and the id search-text-boxis added to the HTML element (which you could also use for styling etc.)
We have faced same issue for our project while adding test automation. As per my knowledge unfortunately GWT doesn't support anything like AOP yet. So we have to follow any of the spaghetti-like approach only from one mentioned above by #mxlse or the one you are already following.
Based on my experience I can recommend you to create separate constant/property at client or server end. Use this file to save all your id's which you can share latter on with test team as well.

How to print the current XML of a dynamically updated View

Say I have some views in an Android Studio app that I change via JAVA code.
Let's use a simple TextView as an example. It has it's XML code in its respective layout file.
But while code runs, I use its .setText() method to set a new text.
How do I print its new XML layout after the changes to see how it is formed?
This is very interesting for studying more complex elements and declaring them complete via XML instead of having to do lots of initialization via code.
Don't stick to the example given. The expected solution should show the entire XML for any View.
Android uses XML layouts to help you express your views without writing code, but there is no easy way to get back to XML. You would have to write this yourself by writing code to crawl a view hierarchy, check every possible setting in every view, and outputting XML that would match those settings. I suspect in some cases it might be very difficult or even impossible if a view doesn't have anaccessor methods for each property that you can set in XML.
In short, the general understanding is that inflating a view from a resource is the only time a dev needs to worry about XML. In fact, the XML is not even really present in the APK (it's precompiled to another binary format.)

Downloading and parsing JSON files to update ArrayAdapters on demand

Well I have an app set up, In way that there are, let's say, 3 ListViews in three different fragments.
Now a server provides 3 JSON files, that contain information, one for each ListView, which I have to parse into objects or lists of objects, and use them to instantiate my custom ArrayAdapters to display information on the list views.
The problem is that Downloading and parsing are operations that should happen outside the GUI thread.
So I am looking for the most effective way to update these list views on demand (when a fragment is inflated or OnResume etc)
What I've tried already was to download the needed json file using an IntentService then send the downloaded buffer to the GUI thread using a ResponseHandler and Intents, and then passing that to a manager class that was responsible for parsing the buffers into objects and populating the views. To do that, I had to keep the Views and the ArrayAdapters as properties to that class and provide on demand update methods.
Now my questions are:
1) Is it possible to have the service download the JSON files, parse them into collections and somehow send ArrayList objects to the GUI thread?
2) If I have to keep the adapters and update them in the GUI thread, what is the most effective (and maybe elegant way) to have a manager class keep and update the ListViews and their ArrayAdapters easily?(Do I have to get a new instance of the ArrayAdapter each time the information is updated, or can I update the inner ArrayList only?)
I think I won't include any code, since this is about the project structure and the preferred ways to do things.If it's needed for some parts though I can always edit the question later.

View or Fragment library to compose UI for common data types

Problem
Reusables#common.com
£1,000,000
www.inventing-a-wheel.ee
Action Shortcut: C
I need to let the user enter an email, pick a date, edit a webpage, choose an image etc - there are a number of common presentation tasks that are not directly subserved by the Android SDK widget library and so I thought there may be a dedicated repository or framework available that specializes in such UI concerns.
[ While Android provides widgets that can be turned into specific editors for email, for instance, the question here regards finding a library of ready-made components that specifically target such common data types i.e. UI elements which are specifically geared towards presenting such data, out-of-the-box, as well as providing validation. ]
Existing Sources?
So I searched the web with a combination of terms like Android, widget, library, view, toolkit, fragment, repository, but came up with false positives only.
Could you refer me to any collective efforts that provide a list of classes / XML layouts implementing common data-specific input / editing / configuration elements?
So I could do something like this in ActivityA1, ActivityA3, ActivityB1, ActivityN9:
new EmailEditText()
new UrlTextView()
new IPView() ...
Matching Data to Views?
What I'm really looking for is a set of widgets that would be resolved according to the data that needs to be presented, perhaps similarly to implicit intent resolution ?
So from my domain model I would provide some data with a specific data type, which should be displayed somehow appropriately. But I do not explicitly set what View will present this data, instead there is a matcher inbetween that filters a list of available specific-purpose views and selects the most appropriate one for the given data type.
Anything like that out there already?
Unfortunately, Android doesn't have exactly what you want, but don't worry - there are definitely things to make your life easier.
For example, you can set the inputType on an EditText to change what the keyboard looks like (eg. for e-mail you have "#" and ".com" buttons, for a phone number you have numbers) and use InputFilter to control data entry.
You can even use EditText.setTransformationMethod() to change the way the data is presented (like adding spaces in a phone number).
And if you want to pick an image, you can send off an intent to request it: http://android-er.blogspot.se/2011/02/select-image-using-android-build-in.html
It shouldn't be too hard building your own set of widgets that does just what you want by combining these approaches.
Update: There is now a library available that makes form validation easier: https://github.com/vekexasia/android-form-edittext
There is a dateslider componenet to implement mixed date-time inputelements.
However accourding to my experiences the current android development tools adt (at least version 17 but probably until version 19) are currently not able to handle jar-files with resources properly. you have to include the library sources to build a project with widgets.

Expandable ListView with custom objects

I'm fairly new to android and am slowly adjusting to how android works.
I am in the process of creating an android application which uses a .net web service to pull back data from a sql database. I have two methods which return a json response with a list of custom objects.
I am parsing these into arrays of matching objects on the android client. I am looking to implement a multiple tier grid displaying information from these two arrays. The items in the first array will have child items contained within the second array.(one to many relationship)
I am guessing I will need to create two custom array adapters? (which will allow me to have custom layouts for each tier).
I have had a look around and struggled to find a comprehensive example of using the expandable list view. What data source will this expect? (some kind of hash table I would imagine?)
Does my approach above sound reasonable?
Any advice would be appreciated.
Thanks,
ExpandableListView expects a class which implements the interface ExpandableListAdapter as the data source. There are several implementations included in the Android SDK. The SimpleExpandableListAdapter would probably get you up and running the fastest, it uses Lists and Maps for it's data. It won't give you the ability to use different layouts for each group within the list but it will let you have a different layout for groups and children.
If SimpleExpandableListAdapter isn't enough then you are going to want to write your own adapter. I'd suggest extending BaseExpandableAdapter (this Adapter implements ExpandableListAdapter and takes care of some of the housekeeping aspects for you, it leaves the rest of the implementation to you). There is a simple example that shows how to do this in the API Demos ExpandableList1.java example.
Your implementation will likely be more complex than the example, but it should give you some idea how to do it.

Categories