How to print the current XML of a dynamically updated View - java

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

Related

Making UI elements dynamically in android apps in android studio using 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.

Should I use SceneBuilder or implement layout in code?

I'm quite new to JavaFX and Java in general, but I've experienced something weird. At first, I took a Java course and there we were creating layout for application using SceneBuilder. I've finished the course and I'm trying to write some simple applications on my own. But whenever I look for some solutions for problems on the Internet, everywhere layout is implemented in code, that means: using something like this:
Label secondLabel = new Label("I'm a Label on new Window");
StackPane secondaryLayout = new StackPane();
secondaryLayout.getChildren().add(secondLabel);
So my question is: how I should create layout? Should I stick to the SceneBuilder or code it in Java as apparently everyone does?
Use the right tool for the right job
There are benefits for using both approaches and you cannot tell in general which one is better without analyzing the scene you're trying to create
Pros of FXML
Fast to design
WYSYWYG Editor(SceneBuilder)
Scenes can be modified without the need to recompile the app
Nice seperation of view and controller with injection
In most cases fxml structure resembles the object structure closely making it easy to read.
Cons (compared to java code)
loading a fxml is slower than java code
no loops possible. When you need to insert something 10 times into a scene you need at least 10 tags; you have to insert the thing 100 times? Thats at least 100 tags. A single tag may not even suffice, e.g. for a GridPane there is no way to insert multiple nodes to multiple columns per <fx:include> tag.
Storing nodes into a collection requires assiging different fx:ids to all the elements and then listing all of them at a different location.
No compile-time checks. You want to add a Integer to the child list of a parent? You can write a fxml that attemts to do that without getting an error - that is until you attempt to load it. The java compiler would immediately complain about something like that.
There is just no way to invoke some methods from fxml.
Loading fxmls relies on reflection
You can of course use java code in the controller class which allows you to combine fxml+java code to a certain level. E.g. repetetive structures could be created in the initialize() method.
Scene builder is amazing when you are building project with JavaFX+FXML,i cant recommend it enough.
When you use fxml your classes become so much cleaner, and you can focus on further extending functionality of your containers/controls.You are still gonna be using javafx classes and your controllers might become quite big. Its good to know the concepts of how to structure layout and when to use what kind of container, so when you just started to learn basics, implement your layout in code, and then move to using SceneBuilder and FXML.
Once you implement more complex layouts in your code, you can translate these skills into other languages/platforms.
There are also cases where you cant do what you want via SceneBuilder, for instance some dynamic layout thats beign created at runtime and would be too tricky and messy to include into separated custom components/fxml files. Then you write it down in your code with complex logic on top of it.
Implement it using SceneBuilder/FXML directly. JavaFX was designed to use XML UI definition files instead of programming your UI in Java code. It is a separation like in Android.

What is the difference between writing onClick() in the XML file to writing listener with onClick function in the Java file?

In Android you can write in button for example the onClick() function and linking the function to it. But most of the time I see people not doing it but activating onClickListener in the Java file to the button.
Why? What are the differences between the two? Are there cases when you are not allowed to use one of them?
Short Explanation:
onClick via xml is good for fixed implementation in your code, but using OnClickListener is better for complex app structure with multiple UI elements or complex UI flow, but they both do the exact same thing.
Detailed:
when you set onClick via layout, LayoutInflator initiated by setContentView
pass this attribute to respected view constructor & managed by the views code.
But by using OnClickListener you can alter the behaviour of the control of click event management when it comes to complex app structure.
Below is snap of the internal implementation of onClick attribute
case R.styleable.View_onClick:
if (context.isRestricted()) {
throw new IllegalStateException("The android:onClick attribute cannot "
+ "be used within a restricted context");
}
final String handlerName = a.getString(attr);
if (handlerName != null) {
setOnClickListener(new DeclaredOnClickListener(this, handlerName));
}
internal implementation itself uses setOnClickListener, so both are same,
as i mentioned above, using xml onClick option is good only when app is simple, otherwise use Listener.
Functionally you are able to use either and they will both do the same thing. There is no restriction on which one you use, however many developers will favor using the Java code because it's a bit more flexible and allows you to add more functionality if you need to. Sometimes you might see people adding a very basic method call in the XML file which then links to a Java method doing everything they need.
All in all it's just a matter of preference and coding style.

Java and Database Interaction. How to use it in a clean code manner?

I am searching for new ideas of how to make my life a little bit easier. I have some Java classes, that use a set of SQL templates, that are saved in a XML File. For example, i have a function that is called getTracksForArtist(Artist artist) and returns a list of track objects from the database. For the lookup, i have a few xml templates, that vary for sorting/grouping issues, that are loaded, when i call the related methods. In the getTracksForArtist(Artist artist) method I call the queryMap.getTemplate("getTracks")` wich retreives the template, that can be modified for the according method. It works, but i think it is really ugly. For one: I would like to not get the template with a String. I also would like to see, if there are templates that are never used, wich is important, as the template XML file became quite big.
I had the idea, to replace the xml file with a enum or some class representative, that has the sql stored inside and can be called by getTemplate(GetArtists) for example. But the problem here is, that multiline Strings are a real pain in java. There are some plugins for eclipse, that promise to solve this issue, but they do not work as needed and i do not really like to solve a design problem with a plugin.
How do you organize your java/database interaction?

Get view from Widget reference

In Eclipse, can I get the View ID given a reference to one of its widgets? I don't want to enumerate all views and traverse their hierarchies.
Use Control.getParent() API to go up the widget hierarchy starting with your widget. At some point, you will find the view part. Then you can get the info you need. I'd do this in the debugger first to make it easier to identify the class that the view part will have in the controls tree. It should be pretty obvious when you see it.
Ended up using SWTbot and some manual bookkeeping for the purpose.

Categories