Xamarin Forms – Creating a Gradient Header

Pieter van der Westhuizen, a professional freelance web & mobile developer and founder of Coalition Software.
Search for a command to run...

Pieter van der Westhuizen, a professional freelance web & mobile developer and founder of Coalition Software.
A recent requirement came up for configurable dynamic forms in a mobile application. There are a few dynamic form packages for Flutter, but I wanted a bit more control over the schema and behavior of the form. Here follows my attempt at creating dyna...

If you've ever written a mobile app with Flutter that integrates with an API, then you'll know that there are a lot of model objects that get passed between the app and the API. JSON Serialization in Flutter can also take a fair amount of boilerplate...

Back in May 2020, Microsoft announced a new feature for Azure Blob Storage called Blob Index. Essentially, this enables you to add key/value tags to your Blob objects and be able to query said Blob objects without having to use a separate service lik...

In my last post, "Using AWS Cognito with Xamarin Forms", I showed how to authenticate with Amazon Cognito using Xamarin Forms and the Xamarin.Essentials Web Authenticator. In this post, we'll go through the process of using the AWS Cognito Hosted UI ...

In this post, I'll show you how to quickly and easily set up user authentication for your Xamarin Forms app using Amazon Cognito. AWS Cognito is a user identity management solution by Amazon. It is a really easy way to add authentication to your appl...

In the design document for the Doctriod app you would’ve noticed that it uses a gradient header. To achieve a similar effect (shown in the screenshots below) in Xamarin Forms, we need to make changes in the Shared, Android and iOS project.


Start by creating a custom NavigationPage class in the shared project (Doctriod) called GradientHeaderNavigationPage. The class should inherit from the NavigationPage class.
The only thing happening in the class mentioned above is that we’re setting the navigation bar text color to white. Next, because the project uses Prism, we need to change theRegisterTypes method in the App class (App.xaml.cs) to use our newly created GradientHeaderNavigationPage class instead of the normal Navigationpage.
In your Android project(Doctriod.Android) create a new class called GradientHeaderNavigationPageRenderer in the Renderers folder. The class inherits from NavigationPageRenderer and there’s a lot of stuff happening this class, but the method that does the work is called SetGradientBackground which in turn is called from OnViewAdded
You can view the GradientHeaderNavigationPageRenderer.cs file in its entirety on Github
Our last step will be to create a new class called GradientHeaderPageRenderer in our iOS project(Doctriod.iOS) You can create the file anywhere inside the iOS project, but I like to create renderers inside the Renderers folder.
The GradientHeaderPageRenderer will inherit from PageRenderer and the method that does most of the work is called SetGradientBackground and is called from ViewWillAppear
You can view the contents of the entire file on Github.
That should do it. If all goes well you should see similar results as shown in the images at the top of this post. Additional reading and research I used to compile this post is listed below.
Thank you for reading. Until next time, keep coding!