Xamarin UI: Programmatically VS XAML - android

recently I attended a talk on Xamarin and we were discouraged from using XAML for our UI design. I recently started mobile development and I would like to know which is the best way to design user-interfaces; doing it programmatically or using XAML?

EDIT 2017 - With XAMLC (XAML-Compiler, read more here) XAML is compiled to IL code at build time so no XAML parsing is involved at runtime anymore. In many cases this makes XAML as fast or very close to coding the UI in code-behind C#. With the latest improvements in XAML compilation, there is now strongly typed data binding (read more here), which is faster and the data binding errors are caught during build time. Some errors in XAML are caught at design time, when you edit the XAML in the Visual Studio XAML editor
I assume you're asking about Xamarin Forms.
It would be interesting if you could mention the reasons why you heard XAML is bad.
The only reason I would think that someone could discourage you from using XAML is performance.
I came across some posts on Xamarin Forms talking about performance issues when using XAML for creating some of the UI with Xamarin Forms.
But given than Xamarin Forms is an early technology, I guess these issues are temporary and will be fixed.
I don't agree declarative UI (XAML) vs coded UI is just a matter of personal taste.
A declarative UI (today is XAML, tomorrow it can be something else) can be a better approach than doing the UI by code, for the following reasons:
It's coincise, it's impersonal. Except few things, XAML looks the same for everyone. You can't say the same thing about actual code, which is more verbose, there are methods called, variables declared, etc.
It's more maintainable. It's easier to read and modify than actual code.
It helps with a clear separation of concerns, between the UI and logic. Same thing can be achieved without XAML, but just by using XAML you are closer to this goal.
Tooling friendly. Unfortunately Xamarin Forms today still doesn't have a previewer or designer, but this is supposed to change There is a XAML Previewer baked-in in Visual Studio when you edit XAML, but it still doesn't work 100%. See more about Xamarin Forms XAML Previewer here and here
I am NOT saying XAML is perfect. But I think it's better than manually coding the UI. XAML uses same C# classes and properties of controls. The XAML syntax is the same with XML, because actually all XAML documents are valid XML documents. XAML is based on XML.
When someone says XAML is bad, I can't help thinking about HTML. Is HTML bad too? Is it better to create HTML elements in Javascript? Obviously it's not.
I know some will say you can't really compare XAML vs HTML because XAML is not as "native" as HTML, but aren't both just related to the presentation layer?
Xamarin (now Microsoft) has some nice introduction to XAML you can read here.
I think developers with native iOS background have a strong inclination towards not using a declarative UI because that's how unfortunately native iOS development mostly works. You are forced to use code to implement different things which are not available in the designer and there's no native declarative UI markup language available(there's one but the format is not public and it's not meant to be directly modified).
You might know this by now but Xamarin Forms is not the only way to build apps with Xamarin. Xamarin != Xamarin Forms.
You can build your UI natively and still share the logic (ViewModels, services, etc). On Android, you can build your UI in the AXML. You will get as closer to the metal you can get with Xamarin with that. But you will need data-binding, so a framework like MvvmCross can help with that.

A very subjective question. Currently the state of Xamarin Forms XAML is similar to early Silverlight in terms of designer support. Similarly, in Visual Studio there has been a lack of Xaml Intellisence though if you use the latest version of Resharper it will provide Intellisense for it.
It can also be frustrating to debug as the command to load the Xaml can cause a crash if the Xaml is invalid. It is still debugable if you catch the exception right there but an error in custom code would be easier to find. Finally I have heard anecdotal evidence that in some cases the XAML is slower but I have not yet confirmed it.
Having said all that, I use XAML. It tends to be the defacto standard in development with languages that have XAML. I also suspect a Xamarin Forms XAML designer will come. Like the early days of Silverlight it is my belief that using XAML may not be the easiest today but it will likely be The best option to prepare your codebase for tomorrow. Once you code your UI you will have quite the task to switch to XAML when the tooling catches up.
But as I stated earlier, a subjective question.

It has been my experience that each tool excels or under performs dependent on the context in which it is used. XAML in Xamarin.Forms is no different.
There are benefits and drawbacks to XAML and the ultimate decision of which to use where needs to be evaluated in a context aware setting.
In a recent project I use both. XAML for the majority of UIs and c# for things like dynamically generated forms. Below is a short and admittedly biased / incomplete list of pros and cons for figuring out which to use where.
XAML Pros -
More clear separation of logic and UI
Easier to read when UI implements styles, triggers, bindings
Easier to add a second version of the UI for a different form factor (tablet/phone/watch)
XAML Cons -
Harder to create a single implementation of custom page generation
Intellisense is not currently supported without add ons or in SAP for Visual Studio
It is not a complete set of XAML like you would get in a WPF project
The only thing I can say with certainty regarding this question is that a blanket statement condemning XAML in forms is non sense.

In my opinion, and it's definitely just an opinion, I prefer to have a almost zero lines of code in a code behind class, where all the definitions of the view will residue in the Xaml file only.
This way, you'll separate the UI from your code logic and will probably have a much cleaner code eventually.
Having said that, Xamarin forms is still very young framework, and many times I've encountered in plugins that don't have support for Xaml, so I had to manipulate the UI using the code behind, but I do it only as a last resort.

I personally prefer to do cross-platform WITHOUT using Xamarin.Forms. I feel like it's easier to create more expressive and pleasing UIs for each platform by using the native UI APIs. Again, as others have said, there is no "better" way to do this, it's all a matter of preference. I personally really enjoy using the iOS interface builder to create my views, and then wire them up to my custom view controllers. This is just what is most comfortable to me, and IMO allows me to create richer, more expressive UIs for my users.

Related

Is it possible to build android widgets in flutter?

The fact that Flutter heavily uses the word widget makes it difficult to find documentation on this topic.
On Android (I believe this isn't possible on iOS), we can add widgets on our home dashboard, allowing us to see app-related information or to trigger one-click actions without needing to open the app in question.
Is it possible to build such "widgets" in Dart & Flutter? Or should I do that in java and somehow plug it with my flutter app?
Can you share an example of a resource containing one?
EDIT: I have no android development experience, but it sounds like using a drawable canvas might do the trick.
I could find some canvas flutter code, but I can't connect the dots yet.
EDIT 2: From this Github issue, it looks like writing android home widgets in flutter is a no go since Flutter has its own rendering engine. I'm keen on learning kotlin to get this done, but if someone knew of nice tutorials to help me with that, that would be immensely helpful.
As the OP mentioned in an edit, this isn't currently possible because Flutter uses a custom rendering engine.
Widgets are quite limited in what they can render; the documentation explains that only certain layouts may be used. You could theoretically use Flutter's software renderer to render to an image in a seperate instance from the main one and display that, but that would be very technical, likely not very performant, and not straightforward at all!
Here is a quite detailed tutorial for widgets that guides you through creating a few examples although in Java. The same logic applies with just a few syntactical changes for Kotlin as the classes are pretty much interchangeable. However, realistically, most of the work is in the layout with some wiring in android; if you're already familiar with Android & Java, keep in mind that Kotlin does add a bit to your app size (The Kotlin runtime adds about 7,000 methods and ~1MB to your debug APK from the kotlin on android FAQ) and since most of your logic should be in flutter you probably won't have all that much Kotlin/Java code if this is all you're using it for (although if you're new to both Java & Kotlin, Kotlin is arguably more friendly in some ways).
Also, to be able to communicate between your flutter app and the java/kotlin backend, you'll need to use platform channels as described in the flutter documentation.

Pure functional programming on android

Are there any advancements in this area? I want to be able to write purely functional code on Android in Haskell or similar languages. I've tried some examples with Scala but it seems to be a pain to get started. Are there any other functional JVM languages which I can use to write Android applications?
Edit: functional languages that write native android applications. My mistake about the JVM.
I doubt that you can find anything mature for writing Haskell-like code for Android. You do need to implement Java abstractions which are required by Android API (implement activity, etc.).
But if you really want to write for Android in a purely functional style you can try to implement your business logic in a pure functional language that compiles to JVM and call it from your Java classes. That approach would be much simpler than trying to implement it entirely in pure functional style.
As your language choice, you can try
Frege, it even has a library for android - froid
Eta lang, it is very new and probably nobody has tried to use it for Android yet
I you want a painless solution in terms of Gradle builds etc., you have only two options: Java and Kotlin, of which of course you should choose Kotlin ;)
Kotlin has most of the things you need to write in functional style:
functions as first class citizen
higher-order functions
immutable collections
var and val like in Scala
if-else as a statement
elements of pattern matching (where statement)
tail recursion
and more...
If you also include funKTionale and kotlinx.collections.immutable, you'll have all the functional goodies like: Option, Try, currying, memoization, persistent data structures and so on...
To start with Kotlin just install the latest Android Studio 3 Preview, which already has built-in Kotlin support.
BTW, don't be so polarized into "pure" functional ;) After all, being 100% "pure" means no side-effects, which means your app can't interact with the user ;)
Hope this helps :)
I've never tried it personally, but you can do F# programming using Xamarin.Android (and, I believe, with Xamarin Forms too). You can see the guide here. (It also includes sample code).
As some background, F# is the .NET Framework's functional language. It is derived from ML; in fact, many ML scripts can be compiled almost "directly" as F# (with the caveat that you may have to do some renaming because F# has some additional keywords that ML doesn't have in order to support several .NET-specific extensions).
Xamarin allows for native development for Android, iOS, and Windows phone. Xamarin Forms allows for a single code-base for all three platforms (it's a competitor to Ionic).
One more quick point: Android does not use the JVM, even if you're writing apps in Java. (In fact, Android does not even support all Java 8 features yet). Through Android 4.4 it used Dalvik; after that, it started using Android Runtime.
You could also try using a JVM language like Scala to create a JAR file and create a bindings library for it.
Please also note that you'll end up using at least somewhat of a mixed paradigm - for example, things like Activities are objects, and the XML files used to define an Android screen is, for all practical purposes, declarative. Edit: This last point is slightly debatable - see the comments.
One final possibility: I haven't checked this out too closely, but try also this link for a site claiming you can do Scala in Android.
Between Pure functional and Java, there is a way which is IMO Pragmatic functional. For instance Redux achieves that in the React arena.
My goal is to write an app (Activity) having an immutable state that advanced as a result of interactions is functional.
In the browser you can see that done with elm (Haskel like language which is also web platform)
Since we want an Android app, I opened the Android Studio used the wizard to create and app with Navigaton Drawer Action bar (with Drawer, FloatingActionBar),
Then converted it to use Elm concepts of immutable model functional approach into a working POC based on a small ElmBase class and idioms.
The code is written in Kotlin (JetBrains tool of choice for the JVM).
You can find the app at my GitHub https://github.com/saffih/ElmDroid The sweet spot of that approach is that it leverages Kotlin being strongly typed and the editor does code completions very well,
making lot's of the code completed for me in a way I have never seen before - amazing experience (But it require using idioms like the sealed class and when properly).

Cross Mobile Options

I created an Android app. While creating one specific app was an interesting challenge, I'm now looking into creating a group of similar apps.
I'd like to create a group of similar Android apps and then move on to creating the same on tablets and iOS... (anything mobile).
I've considered doing so with a product called PhoneGap or doing a web based mobile app. Both of these options seem less than ideal. Doing the Android app I've been frustrated by Java's lack of control and low level constructs. Moving to something like a web based app seems like the exact wrong direction.
C++ is my language of choice. It has the ability to work at a low level, is highly portable across platforms, and has significant support for generic coding which would be useful for generating a group of similar apps. However, the Android documentation suggests to not use C++ unless your goal is porting existing code or dealing with computationally heavy tasks.
I'm leaning towards using C++ anyway, but are there other options I've not considered?
Thanks
You could in theory write your logic in C++ and then have UI layers on top that make use of it. If you are really comfortable with C++ that might be the way to go.
Almost any other parts (networking, UI, animation, etc) are better off being done in the native language of the platform. Use of cross platform solutions always limits you in some way, and usually leads to an application that is not as good as it could be for any platform.
Well, Google's recommendation to not use C++ is based on the following, I believe. C++ is low level, so you can get extra performance out of it if you know what you are doing. Google makes the reasonable assumption that many programmers do not. It is easier for an inexperienced programmer to do harm in C++ then to get a performance boost.
But, if you know what you are doing, it can help you. UI elements on both iOS and Android are implemented in their main language (obj-c, and Java respectively) so there is not a great way around that, but you can write core logic and other functions in C++ and it will be portable between them (iOS can use C++ directly and Android can use it via the Native Development Kit).
There are a few other options available. The one I ended up using is Appcelerator Titanium but please stay away from it. If your project gets complicated or large at all you will hate yourself for choosing it, as I did. Another interesting one that uses C++ instead of Javascript is Marmalade. I haven't used it though, so I can't comment on it.
A non-free solution that I hear good things about is Xamarin, who have ported both environments to C# and a .NET using Mono. However, you still have to write two versions of your code for the UI as far as I can tell.

is there any high level GUI framework around for android sdk?

I am rewriting an Android project, and I am stucked and sucked at making the GUI look like the way I wanted it too. It is easily achievable using HTML and CSS, I have done it with PhoneGap, CSS, HTML, JS, but it is kind of slow, so I want to do it native using Java.
(My background is C++ and some web programming stuff)
What I have been encountered so far is that the default GUI builder is not really pretty and I feel frustated almost every the time using it. And people recommend droiddraw in few other questions in Stackoverflow, but it also not resulting in pretty GUI as well.
What I want to have is some flexibility like HTML and CSS and from that I can execute native code.
I believe I can achieve GUIs like from the Facebook or Twitter client easily with HTML and CSS, but if i m going to do native using the SDK, I think I'm gonna need something like 1 month just to do the GUI.
So the questions would be:
Is there any GUI framework for Android SDK ? (More high level then the provided one)
How to render HTML/CSS that allows executing native code on interaction with its element.
Any suggestions welcome for any workflow.
And of course, Open Source solutions are preferable.
There are some projects for some iphone like widgets in android. You can find them seperately mostly in github, so I have not even seen a complete higher level widget library yet. But you can give appcelerator a try. You are writing html,css,js and it compiles to native code. I personally did not used it. It is a free software. Some people are happy with it, and some are not. You should read some experiences before beginning development. http://www.appcelerator.com/

Android Designer like "Interface Builder"?

We are beginning to learn Android, converting our iPhone apps over.
There is a rudimentary editor for layouts in Eclipse, but it's not very good. Is there a visual designer that would be on par with the iPhone "Interface Builder" ?
AFAIK there is no such builder yet for Android,
Check out droiddraw and try to make the most out from Eclipse builder, those are the best and only interface builders. Anyway once you get in, you won't spend to much time creating lots of interfaces, as you will reuse them or the inbuilt components like preferencescreen.
When developing and thinking about layouts, think them as they were like HTML layouts.
App Inventor looks interesting, but it's still in beta and you need to request access:
http://appinventor.googlelabs.com/about/
It seems to have a nice interface for building Android UI's and actually generates other code such as networking and database code.

Categories

Resources