Xamarin Forms with native Android Toolbar - xamarin

We currently have an app designed in Xamarin NativeUI (shared code and Xamarin.Android, Xamarin.UWP etc) are in the process deciding whether Xamain.Forms is mature enough to transition to.
One of my core concerns is that we have a very customized actionbar/toolbar that makes use of Android inbuilt handling of automatically moving items to overflow based on screen size, having overflow menus and submenus, custom controls in place of icons+text.
I have managed to get custom controls and hardcoded overflow items. But I'm not sure if the rest is possible without going native. And if we do need to go native I'm not sure how you would then use Xamarin.Forms and then have a Xamarin.Android ActionBar.
Another fun thinig to add to the equation is that we have to support Android 4.3 so that means using the support library actionbar.
Apologies, the question is a bit rambling but can be broken into 2 specifics:
1) With Xamarin.Forms can items move to or from overflow in toolbar automatically based on screen size available in similar fashion to Android native toolbar?
2) If using Xamarin.Forms can you use a toolbar defined in Xamarin.Android? How?

In addition to #Steve Chadbourne's answer, it's really important to know what page control are you using in PCL of your XF project, if you open the MainActivity.cs file in client android project, you can find by default it codes like this:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
As you can see, there is defined Toolbar which you can find in the Resources/layout folder in client Android project:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
This Toolbar is used by default within some page type control in XF, for example NavigationPage.
For native control of XF, I think it's possible that you didn't notice that Xamarin.Forms.ToolbarItem. It is also rendered like Toolbar and you can set the Order property to ToolbarItem to specify the item to be displayed on primary, secondary or default toolbar surfaces.
For custom renderers, it's possible that you can refer to this blog: Xamarin Forms - ContentPage with SearchBar in the Navigation bar.
Don't know which is the best choice for you, but I want to explain to your two questions. XF is like a UI toolkit, all controls it has are created based on the native SDK, means all controls are renderered as a matching native control so it can be used in native platform. So if a XF control is rendered as Toolbar in Android platform, the behavior should be the same as normal Toolbar in native Android, BUT like we customize the renderers, when the XF control is created, many properties are hard coded in its code, which may have effect the result here. If you meet such problem, I suggest to read the open source on Github.
For your last question:
If using Xamarin.Forms can you use a toolbar defined in Xamarin.Android? How?
You can either using custom renderers or directly modify the code in Toolbar.axml in native client Android project.

This should be possible in Xamarin Forms using a custom renderer.
You are one step ahead in that you understand what you want to do and how to do it in Xamarin Native.
Here is an overview on custom renderers.
Here is a link to a very basic navigation page custom renderer for Android. You should be able to expand it to include the native Android code you require.
Be aware that you probably need to override NavigationPageRenderer if you are using app compat to support earlier versions of Android.

Related

Bind/set short text data from Android MainActivity.cs to a shared Xamarin Forms Label?

Is there a way to define a Label in Xamarin Forms (XAML or code-behind) AppXYZ that can be updated from the Xamarin Android AppXYZ.Android: MainActivity.cs and AppXYZ.iOS:Main.cs? i.e. upon an event in Android (e.g. BlueTooth) set a short text in a common Forms elements? After viewing many example, no clarity yet. MessageCenter perhaps?
In Android Project: MainActivity.cs (for example):
FormsAppXYZ.LabelXYZ.Text = "Updated string";
If not possible, then proper way to do this with Xamarin Android Native ? appreciated since I'll have to mix in the label into a shared Xamarin Forms Tabbed GUI.
Depending on how much Bluetooth work you need to do, my first suggestion would be to use MessagingCenter as you have already alluded to.
The other obvious approach would be to use a third party plugin, as I don't believe Xamarin have released one. I am currently aware of these two:
https://github.com/aritchie/bluetoothle
https://github.com/xabre/xamarin-bluetooth-le

xamarin forms start nativ uwp page

any idea how I can start a native UWP page from a Xamarin Forms page?
The reason why I'm asking:
I have a XF application that needs to start a UWP library after a click event.
This library provides a special ORC reader.
Therefore I created a dependency service to run the platform specific function.
What I need now, is to open that UWP page, that contains the "Camera stream" to show the user what he is currently scanning.
And this is where I am hanging at the moment.
In my class that is called from the dep. service, I
myPage mp = new myPage();
But how can I show the new page?
Frame fr = Window.Current.Content as Frame gives me the error, that this page cannot be converted from a XF Frame to a Window Frame.
Or is there generally another way to handle this? Some advice would be great.
This sounds like a custom renderer.
Create a new OcrView class in your shared project inherited from View.
Place it on your Xamarin Forms XAML page.
Handle the OCR component in the UWP renderer.
Have a look at the Implementing a View section on this custom renderer overview

can't style xamarin android calendarView

how can i style xamarin calendarView. i've tried but it's not working. also it appears differently on different android API levels.
this is what i have tried so far
<CalendarView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/calendarView1"
android:overScrollMode="always"
android:scrollbars="horizontal"
android:showWeekNumber="false"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
The Android.Widget.CalendarView class is a widget that consists of several subviews. The widget itself does not provide any real styling properties/methods. See the API docs:
Xamarin Android: https://developer.xamarin.com/api/type/Android.Widget.CalendarView/
Native Android:
https://developer.android.com/reference/android/widget/CalendarView.html
And it will look different on different versions of Android as the widget will use the theme/style of the Android version on which it is running.
You should be able to use styles and themes to style the CalendarView though. See this Android doc:
https://developer.android.com/guide/topics/ui/themes.html
As a very simple example, you could do this to use the Android built in Holo theme:
<CalendarView
...
android:theme="#android:style/Theme.Holo" />
And by following the guide linked above, you can create your own theme to use and even inherit from a built-in theme and just modify it. And setting a theme should keep it looking the same on all versions of Android.

Is possible to do sliding drawer in xamarin cross platform?

I am working in xamarin pcl project , I am trying to keep sliding drawer in my pcl project but i dont know how to do that .
Are you using Xamarin.Forms?
If not, i would advice to use a framework like MvvmCross (https://github.com/MvvmCross/MvvmCross)
This would enable you to split the actual implementation of the drawer / hamburger menu into the different UI projects like iOS and Android(maybe WP8 too).
Since those platforms all behave differently, for example Android uses Fragments which are replaced inside the activity containing your navigation drawer, you should use a custom presenter to determine where you want the specific platform to navigate to. You can find more information on that topic here: http://slodge.blogspot.co.uk/2013/06/presenter-roundup.html

Adding Xamarin.Forms to an existing Xamarin app

I'm trying to add a Xamarin.Forms page to an existing Xamarin Android app.
I'm struggling to figure out how to mix native Xamarin Android pages and Xamarin.Forms pages.
All the articles and examples I have seen describe either Forms or Xamarin apps and not a hybrid of the two.
The best idea I have read about so far is to add a native stub and replace the content with Xamarin.Forms content but I'm not sure how to achieve this.
Update
I'm using MvvmCross so my android view is inherited from MvxActivity. I have a StackLayout containing views using Xamarin.Forms. How do I get XF to convert to android code so I can use with SetContentView?
When you create a new Xamarin.Forms project, for instance with a SharedProject approach, it will create the launcher project and initial class file that will be executed for you for each platform.
For instance on Android this is in MainActivity.cs. If you look inside this file you will notice that it inherits from AndroidActivity which in turn inherits from Activity, which is the typical class used on activities.
So you have two options available for you to create a mixed hybrid solution:-
1) Inherit from AndroidActivity in your platform-specifc stub-page should you want to display Xamarin.Forms content, or alternatively
2) Inherit from Activity, and do the same as usual.
If you want to display a Xamarin.Forms page from the stub-page you can then just use some boiler-plate code such as the following in the OnCreate method:-
SetPage({some class that returns a Xamarin.Forms.Page object, i.e. ContentPage});
You can then use the normal navigation that is specific to Android to either display a Xamarin.Forms page or a platform-specific Activity page.

Resources