Android dev: what is the difference between INSTANTIATING a View and DEFINING it in the XML Layout - android-layout

I'm a newbie in Android development.
I have Eclipse with ADT (sdk version: 17 , Android 4.2).
I don't understand what is the difference between:
DEFINING a View (via visual editor provided by ADT or directly in the XML layout file corresponding to the current activity)
and
INSTANTIATING a View (PASSING THE CONTEXT AS PARAMETER) such as: TextView tv = new TextView(getContext()); (taken from : What is 'Context' on Android? , first asnwer)
and
INSTANTIATING a View (WITHOUT PASSING THE CONTEXT AS PARAMETER) such as: TextView tv = new TextView();
Thank in advance for any advice.
Kind regards

To use your terminology:
When DEFINING a View in XML (or the ADT editor - which just creates the XML for you), it still needs to be inflated by a layout inflater. A layout inflater will INSTANTIATE the View(s) for you. This can be done behind the scenes - such as when you call Activity.setContentView(), or directly using View.inflate(). The inflater effectively just runs through the XML and instantiates all of the Views it contains.
When INSTANTIATING a View, you're giving it the Context so it has a reference to resources - so it can load images, strings, dimensions etc - plus other Android related functionality (which you can probably ignore for now).
You can't INSTANTIATE a View without the context.

Related

Android Studio with Kotlin How Does Control Pass From MainActivity to Another Class

I'm using android studio 4.1.1 and kotlin.
I'm trying to learn programming Android apps using Android Studio and Kotlin. I have a question that a couple of tutorials and searching threads have not answered for me. How does control pass from the MainActivity.kt class to another class, i.e: FirstFragment.kt?
I'm doing fine with other things I'm learning in tutorials, but I cannot figure this out.
Example:
I create a new Basic Activity app in Android Studio with Kotlin. In the java directory, I see these classes: FirstFragment.kt, MainActivity.kt and SecondFragment.kt.
In the res/layout/ directory, I see: activity_main.xml, content_main.xml, fragment_first.xml and fragment_second.xml.
Question 1) When the app loads in an emulator, I see the button and textView from the fragment_first.xml. How does this happen? I cannot see in the code where it says, "load fragment_first.xml".
MainActivity.kt has setContentView(R.layout.activity_main) which loads that layout. activity_main.xml has <include layout="#layout/content_main" />. But from there, I do not know where to find the code that loads the fragment_first.xml layout.
I believe that when fragment_first.xml loads, control passes from MainActivity.kt to FirstFragment.kt, yes? Question 2). Is this because of the onCreate function?
Thanks for helping me to understand this better.
There are multiple ways to load a fragment. Traditionally, you use the FragmentManager to create a Fragment transaction that tells the Activity to place a Fragment into the view hierarchy and start managing its lifecycle.
More recently, they have added the Navigation component, which can be used in place of directly using the FragmentManager.
In the current new project Basic Activity, the layout activity_main.xml includes the layout content_main. In content_main.xml, you'll see that it includes a fragment element that is set to the Fragment (using the name parameter) NavHostFragment, which is a special Fragment that the Navigation component uses to host children fragments that are specified in a navigation XML file. This fragment element also specifies the navigation xml file using the navGraph property. It specifies nav_graph.xml, which you'll find in the res/navigation directory.
If you open nav_graph.xml, you'll see that it has FirstFragment set as the home fragment, so that is what pulls it up as the first fragment that you see.
Basically, the control flow is:
Activity's onCreate() sets content view to activity_main, which includes content_main.
content_main includes a NavHostFragment that is automatically inserted in the view hierarchy so the Activity starts running it.
The NavHostFragment is set in the XML to use nav_graph, which tells it to load FirstFragment first.

Insert a Monogame view inside MvvmCross monodroid Activity

I'm trying to build a Monogame view inside a RelativeLayout from my MvvmCross monodroid Activity view.
An android Activity inherits from Microsoft.Xna.Framework.AndroidGameActivity to be able to run a Monogame inside a RelativeLayout (working).
My MvvmCross Activity inherits from MvxBindingActivityView(working).
So, I need a way to run the game and bind some datas within the same activity.
Thanks in advance for your help.
Loosely speaking, you can translate any Activity to an MvxActivity by inheriting some interfaces and by then cutting and pasting a small amount of code which does the basic loading and assignation of the ViewModel.
e.g. see the #Region and IMvxAndroidView<TViewModel> added to make MvxActivityView.cs from a normal Activity.
e.g. it's the same region and interface used for adapting a specialised Activity like Google's MapActivity into MvxMapActivityView.cs
At this level, the Activity/View has a ViewModel which can be used in C# code, but has no clever xml inflation - it has no clever Binding support.
Code can be written at this level - I've shipped apps without binding - but many users prefer to add DataBinding too...
To add this DataBinding support, you need to add a bit more code which provides BindingInflate, storage of bindings, disposal of bindings, etc.
e.g. a raw MvxActivityView is extended using the IMvxBindingActivity interface and a #region like: MvxBindingActivityView.cs
e.g. MvxMapActivityView is extended using the same region and interface: MvxBindingMapActivityView.cs
So to extend your the custom AndroidGameActivity:
Inherit from AndroidGameActivity to get ViewModelOwningGameActivity<T> and cut and paste the IMvxAndroidView<TViewModel> interface and #region from MvxActivityView<T> to provide the ViewModel methods, fields and properties.
Then assuming you want binding:
Inherit from ViewModelOwningGameActivity<T> to get BindingGameActivity<T> and cut and paste the IMvxBindingActivity and #region from MvxBindingActivityView<T> to get the binding methods
For specialist Activities you may want to add more - e.g. you may could add some custom helper methods for the MapActivity to plot points and lines, or for GameActivity to do whatever games do... but this is up to individual implementations.
Sorry about the cut and paste of code required in adapting Activities - I have tried to keep this to a minimum. However, writing Mvx is the one time so far that I've really wanted Multiple Inheritance or Mixins in C#

developing library controls for xpages

I' working on a library control for Xpages and need some help in creating.
I would create a control which reads a configuration file and creates controls in a table, controls like Editboxes, checkboxgroups and so on.
so and now to my questions:
could I initiate controls from the Exlib or must I implement them all by my self?
if I could use them from the Exlib could anyone explain me how?
I hope its clear what i mean if not please ask me for further informations.
When creating your own components, if you're closely replicating some behavior that is already in an extension library component, I highly recommend you extend that component and just add what's needed to accommodate your different functionality. This makes things much easier and you don't have to code around every little scenario that the component might be placed in.
But, if you are developing a component that is nothing like any of the extension library or core components then just ensure your component extends UIComponent or UIComponentBase. If going this route, you'll also need to create your own renderer which extends Renderer. This is what will build the on-screen representation of your component. Again, if there's already something in the core components or extension library components that closely mimics what you need then make your renderer extend that renderer. Also, don't forget to include the renderer definition in the faces-config file and the component definition in the xsp-config file or your component won't work.
As for initiating controls from the extlib.... I assume you mean can you inject them onto the page at runtime. If so the answer is absolutely yes. To add an input text field to the page where there is a container (i.e. panel, div, span, whatever) with an ID of "someContainer"
XspInputText input = new XspInputText();
input.setValue("someValue");
input.setId("someID");
UIComponent container = FacesContext.getCurrentInstance().getViewRoot().findComponent("someContainer");
container.getChildren().add(input);
To see the api for all of the core and extension library components take a look at the XPages Controls Documentation. For a more complete tutorial on creating your own components take a look at my blog for creating a custom component inside an nsf, the steps are pretty much the same for putting them into a library:
Part 1,
Part 2 and there is an example database in the Part 2 post.

What is the best way to design the GUI in javafx 2.0?

In javafx 2.0 it is possible to create the layout by using FXML approach or by using normal java code.
What is the best way with respect to a well designed set of UIs. In my application there is about 100 sub UIs.
Thanks
FXML looks more logical for that purpose. By using FXML
you split business logic from view
you get option to edit design without recompiling project.
you get design as structured xml tree which is much easier to edit comparing to potentially randomly ordered java code
with SceneBuider tool you get an option to use visual editor for your fxml files
Get JavaFX Scenebuilder here.
FXForm2 is a library providing automatic JavaFX 2.0 form generation.
however FXForm2 is not full WYSIWYG GUI design tool.
http://dooapp.github.io/FXForm2/
Scene builder should be a good starting point to create unique UIs of your application. Considering you have 100s of UIs, I assume that some of their "appearances" should be identical with slightly different functions. You can load the FXML dynamically and assign controller at run time. Which means 1 FXML file can be used with multiple controllers. Which can save you some time while keeping the code dynamic for easier maintenance.
FXMLLoader loader = new FXMLLoader(getClass().getResource("DBedit.fxml"));
loader.setController(new DBeditEntityUser());
So, to make use of the same FXML with a different controller.
FXMLLoader loader = new FXMLLoader(getClass().getResource("DBedit.fxml"));
loader.setController(new DBeditEntityUserLevel());
Hope this helps.
fyi,
road map for Java fx http://javafx.com/roadmap/ shows that the scene builder will be released around middle of the year. From the above web page:
"JavaFX Scene Builder is a WYSIWYG GUI design tool for the
JavaFX platform. It enables designing user interface screens by simply
dragging and positioning GUI components from a palette onto a scene.
The tool generates files in FXML format2 that can be used within a project
in any IDE such as NetBeans or Eclipse. The JavaFX Scene Builder can be
used to create GUI for desktop applications and applets that run in a browser."

MonoTouch 5 Error: "Failed to find selector setXxxxxx" after migrating to Xcode 4.2 and MonoDevelop 2.8

I have an iPad project that has been under development in Xcode 3.2.6 / MonoTouch 4.2.x / MonoDevelop 2.6 and when I installed the Monotouch 5, MonoDevelop 2.8.1 and Xcode 4.2 the project compiled and run OK. But when I started to change the XIB files in Xcode 4.2 (adding outlets) I started to have some glitches:
1) App crashes and MonoDevelop in debug mode says the the views were not loaded because the view outlet was not set. I figured out that in Xcode 4.2 there is no need to create an outlet named "view" in the .h file and just need to connect the File Owner view to the view control in the designer. Correct?
2) The main problem and is not solved yet is that I get exceptions "Failed to find selector setXxxxx" where "xxxxx" is a name of an outlet for a UI control. It started to happen in ViewControllers that inherited from a base view controller, but as soon as I started to change other normal viewControllers I get the same type of errors. Any hints of what is wrong?
3) I have a controller that loads dynamically some views and that works fine. but when I set a property on one of this sub views outlets (like the Text of a UILabel) the value is set (on the debugger) but no change in the UI. The actions for example work fine. What could be wrong.
4) Any special settings in the XIB's that are needed in order to correctly under Xcode 4.2 to integrate well with MonoDevelop 2.8?
Help is much appreciated, I thought in going back to the previous set of tools and monotouch but I guess once the project was upgraded there is no way to go back without losing all the development done now.
Thanks, Pedro
EDIT: Beside the new clean attributed [Outlet] code generated by MonoTouch 5 (replacing lot of code previously generated), the significant difference is that before the partial class generated was public and now is private, this may be the source of my problem. Is there a way to generate public partial classes or protected?
EDIT 2: If I manually add the public keyword to the partial class and members generated by MonoTouch in designer.cs the selectors are generated behind the scenes and all works. This indicates that with the change from public to private partial class the bindings generation changed in MonoTouch 5, is it by design or bug? I would find it very strange that now its not possible to create a viewcontroller with outlets, subclasss it and the child viewcontroller cannot access the parent class outlets.
1) There is indeed a view outlet on the base UIViewController class, which is an Objective-C property. When using Interface Builder 3 with MonoTouch, IB often didn't allow connecting to the base view outlet so many users created their own in their custom subclasses. MonoDevelop 2.6 and older created these custom outlets as ivars (Objective-C fields), and apparently the UIKit runtime would connect the object to both versions of the outlet - both the custom ivar and the base property. MonoDevelop 2.8 generates the custom outlets as properties, which means that the redundant view outlet shadows the base view outlet, so the base view outlet is never set, and UIKit gets unhappy. You can fix this by removing the redundant custom view outlet - MD 2.8.2 will do this automatically.
2) Seems to be a bug with the way MonoTouch exports custom outlets that prevents connecting to custom outlets defined in base classes. A possible workaround is mentioned on the bug report.
3) The UI rendering won't update while your app is paused in the debugger, it'll update on the next mainloop iteration.
4) Nope.
5) The class accessibility should be controlled by the user class part, not the designer class part, so you can freely change it.

Resources