Android UI - How to create Dynamic UI on XML - android

I have a weird problem(I assume, not common in Android). I'am working on a product which has its UI "elements" defined by a individual(admin) using our website. Positioning, characteristic and order of the UI elements is decided by the admin. However, type of UI elements is pre selected. Some UI elements are - checkbox, editText,spinner etc.
Although we handle this programatically, I was wondering if same can be achieved by pre-defined XML(since we know the UI elements we support) and dynamically modify the position and characteristic of the UI elements.
I wanted to do this because it is becoming very painful to manage the UI using the java code.
Any suggestions is appreciated,
SKU

The question is puzzling. You say that your admin defines the UI elements, "however", the type of the UI elements is pre-selected? Preselected by who? Exactly what in XML is pre-defined, and what isn't?
If a widget class is defined in XML (a widget is something like , , etc.), you can modify some aspects of the widget's appearance by inflating its layout (which is done automatically for the main layout), finding the widget within the layout tree, and changing the widget's characteristics.
You can't change the widget's type. You'd have to delete the widget from the layout and add in a new one that fits into roughly the same space.

Related

creating a dynamic constraint layout

I'm devising a multi file processing system, and they have an interface like this:
The lime arrows show the constraint setup, and the orange box holding the widget is a constrain layout
So, I'd like to make this constraint layout dynamic say I'm only processing 1 file then only a button and its corresponding progress bar should appear, if two files are being processed then 2 buttons and 2 corresponding progress bar should appear and the (orange) constraint layout should resize as well.
For this type of appearing of a group of widgets (the button and it's corresponding progress bar) it seems sensible to save them in a layout and reuse them using <include>, but then the sizes look dynamic that I think the whole UI thing could be just done in java.
So, I'm confused to how to go about making this sort of UI design. The questions that arise would be:
1) If I save the button and it's corresponding progress bar as a layout, how do I reuse it, like I'd have to instantiate it every time no. of file processed would change, setup their constraint, provide different ID to each button and progress bar. (If not possible to answer all these, please tell me if it's at least possible using xml layout to do this)
2) Just use plain java code for all the UI designs that relate to the constraint layout. The code and UI would not be separate.(I think this is how to handle this sort of thing, but, I'm not very knowledged)
So, what would be the appropriate way to go about doing this.

Android: Widget, AppWidget, Fragment, Activity and App - What's the difference?

I've got a lot of experience with Java and C#, but I'm new to Android. I mainly use C# because I am enamored of the Control hierarchy. I love the plug-and-play of the ontology. I'm trying to understand the ontology in this new paradigm and I may have been given some false information.
With respect to Apps, that should be the largest component. Within the App, there may be several Activities. An activity can display a number of Fragments. AppWidgets appear to be a special case as they exist as a component of the App, but are shown on their own. And I was told that you can extend Buttons or ProgressBar to create your own components which again appear to be called Widgets.
As I said, I may have this completely wrong. Ideally I would like to create my own widgets which I can put on a Fragment, an AppWidget or an Activity; any of which I might compose into an App. All the online sources I've found only discuss Widget in the sense of an AppWidget? Was I given incorrect information? Can anyone clarify the ontology?
Thanks
"Widget" is a bit of an overloaded term. You will probably have better luck if you search for tutorials on "custom Views" instead. I'll include a brief rundown of various terms and what they mean at the bottom.
A custom View is pretty much anything that extends the View class (or any of its subclasses) and isn't part of the framework. Custom views can be used wherever typical Views are expected, e.g. in layout files or directly constructed in Java. One thing to note: only certain Views can be used in an AppWidget because they are running in another process outside of your app. This means your custom Views cannot be used in AppWidgets. In my experience this tends not to matter too much.
App: An application. Contains components, which are defined in the manifest within the <application> tag.
Activity: One of the four application components. Nearly always has an associated UI, composed of a hierarchy of Views.
Fragment: A framework class that helps modularize your application's code and UI. Fragments can be attached to an Activity and can contribute some UI to the View hierarchy of the Activity. They are entirely optional; you don't have to use Fragments in your app, and you can attach a Fragment without it contributing any UI to the Activity.
View: A UI component, such as text (TextView) or images (ImageView). These are also referred to as "widgets", and you may notice the framework classes are found in the android.widget package. Some views contain other views, so that you can build a UI hierarchy; these will extend ViewGroup and are referred to as "view groups" or "layouts" more or less interchangeably.
AppWidget: Something the user can add to his or her homescreen. This is provided by the app, but is not one of the 4 application components mentioned previously (it is managed by an application component, namely a special subclass of BroadcastReceiver). Most people colloquially refer to these as "widgets" because it's shorter to say and launchers used that terminology as well, thus conditioning users to it.

Obtaining widget UI references

After doing some coding for widgets, I've been wondering if I'm correct in assuming that the only way you can modify UI elements is through RemoteViews and there is no way to obtain a reference to any elements on the widget (TextView, ImageView, etc.). Or is there a way to store references to these specific elements of my layout? Perhaps through intents or constructors? Thanks for any input.
After doing some coding for widgets
Given the context of the rest of your question, I am interpreting "widgets" to mean "app widgets".
I've been wondering if I'm correct in assuming that the only way you can modify UI elements is through RemoteViews
Generally speaking, yes.
there is no way to obtain a reference to any elements on the widget (TextView, ImageView, etc.)
Correct. The app widgets are rendered by the home screen, and so the home screen's process is the one that has the View objects, not yours. The exception would be if you write your own home screen.

Dynamically change the content/layout/flow controlled via server

I am new to android development, and we have a very specific requirement.
We need to change the content/layout/flow of the app on the fly. For e.g. we have a layout which consists of some images, textarea and textboxes. There might be a request coming to change the textarea to a textbox.
We thought about this and are thinking to provide the apk with a json/xml which will contain all these changes.
My question is will it be possible to re-draw the objects again dynamically and change the content?
Yes this is possible. You can dynamically design what has to be displayed in your Activity UI screen. If you feel there are only 2 or 3 different UI screens that would be repeatedly used, then you can have XMLs for these screens and you can just change their labels in OnCreate() of Activity class before rendering. LayoutInflater class would be helpful here.
When you design a Android Application with Activities and Fragments your XML layout definition is always static. If you want a true dynamic layout structure you should use a Web View with a HTML content pointing some URL.
As Rahul says, another approach is to manage the "default cases". For me that is the standard way to design an Android Application.
The dynamic content (values) can be done with a simple http call to server you can get values for your views.
The navigation could be handled by switching Intents, but, definitively you have to associate these intents to UI elements like buttons in the most cases, and ¿How you can do that if your layout is changing over time?.
I think, that the WebView could be a very easy solution for your problem.

Does Android UI development lend itself well to a particiular design pattern?

Does the Android platform lend itself well to a particular style of UI programming like MVC or MVP? Most of my UI experience is with spaghetti code on a very old embedded device or in GWT with MVP so I do not know where to start.
The MVC Pattern is more or less pre build into android.
You have three layers consisting of:
The Model Your data classes, Content providers etc. wrapping all your data.
The Controllers Treat all your activities as controller classes. Don't do anything in them that looks like business logic or data persitance. Just react to events from the model or the user and forward them to the correct layer.
The View Often the Activities are called the view because there it is the java code that is closest to the views. But in my opinion the view layer in Android is mostly defined in xml. You define your buttons, images, state changes etc in xml and then connect it with your application through your Activities.
There are some simple rules to follow to have a basic separation of this layers.
Define as much of your UI in xml only
instantiate Views yourself if there
is no other way to achieve something,
don't change the graphical state of
views from code, for example don't
change the background of a button if
the button is deactivated, or the
color of a font if a button was
clicked, do all this through stateful
drawables, and selectors in xml.
Don't do any data saving or logic in
your activity classes. Call to extra
model classes for this purpose. This
will make your activities clean and
short
If you want to change your data think
about going through a full
controller changes model -> model
informs controller about changes
-> controller changes UI cycle instead of having the controller
change the model and the UI
directly because other observers
of the modes may not be notified.
I do not know if the Android lends itself well to a specific design pattern when it comes to UI development per se, you can certainly use a particular pattern if it helps.
When in doubt you can check out the standard User Interface Guidelines and see what the guidelines are for particular interactions.

Categories

Resources