In a previous post, we talked about the iOS side of setting background images for both NavigationBars
and TabBars
. Beyond that, we looked at setting them so that we can simulate having transparent bars that show a background image behind the entire Page
. That looked like this:
So now let’s do the same thing in Android!
Android
So Android handles images very differently compared to iOS, and also gives us a few easy ways to do what we want here. Rather than having to create custom renderers, we need to create Drawables
for our images, and apply them in our Android Layouts
for our Toolbar
and TabBar
. This approach is going to be more similar to Morgan Skinner’s approach for iOS here: http://www.morganskinner.com/2015/01/xamarin-formsusing-background-images-on.html
So we will need to crop our image where our Toolbar
and Tabbar
will be. If you are planning to also support landscape views, be sure to also crop the image separately for landscape and include those images in your appropriate drawables folders such as drawables-land-hdpi
.
Another thing to consider is that, unlike in iOS, Android tabs are placed below the toolbar rather than at the bottom of the entire view. Because of this, you made need to use a different background for your pages that have a TabBar
versus the pages that don’t. Here are some examples of how you might need to crop your images:
Portrait with no tabs
Portrait with tabs
Landscape with no tabs
Landscape with tabs
Be sure that you name the individual images the same, but place them in the appropriate resource folder as explained before. In this example we will call them:
- toolbar_background.png
- tabbar_background.png
- page_background.png
- tab_page_background.png
Unlike our iOS implementation, we do NOT need any custom renderers. Instead, we will set the background drawable of our layout files for our bars, and then in our xaml page, add an image to be the background.
Toolbar.axml
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:minHeight="?attr/actionBarSize" android:background="@drawable/toolbar_background" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_scrollFlags="scroll|enterAlways" />
Tabbar.axml
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/sliding_tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/tabbar_background" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:tabIndicatorColor="@android:color/white" app:tabGravity="fill" app:tabMode="fixed" />
Then be sure to set the resources before starting Xamarin.Forms in your MainActivity
:
MainActivity.cs
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()); } }
With all those things set, you can now use your background images on pages with or without tabs:
ContentPageWithTabs.xaml
<ContentPage ...> <Grid ...> <Image Source="tab_page_background.png" .../> <!-- The rest of your content on top of the image --> </Grid> </ContentPage>
ContentPageWithNoTabs.xaml
<ContentPage ...> <Grid ...> <Image Source="page_background.png" .../> <!-- The rest of your content on top of the image --> </Grid> </ContentPage>
You can see an example result here (with a rushed crop job):
If you like what you see, don’t forget to follow me on twitter @Suave_Pirate, check out my GitHub, and subscribe to my blog to learn more mobile developer tips and tricks!
Interested in sponsoring developer content? Message @Suave_Pirate on twitter for details.
I cannot find the example code on your github repository, could you share it on github.
LikeLike
That’s a work in progress. However, all you need is the code in this post to get what’s in the screenshots.
LikeLike
This is amazing. I was wondering if there is such a solution for Xamarin Forms application?
LikeLike
This solution works out of the box in Xamarin forms, and the link at the top of the article shows how to do it in iOS for Xamarin forms
LikeLike
Hi Alex,
Is it possible to do this without a background image? Because I have a webview and I want the loaded content to appear in the same way with a transparent toolbar overlaid on top of it. I’ve tried setting the background of the toolbar and it doesn’t actually shift the content up, so I end up with a white-ish toolbar.
Cheers,
Mel
LikeLike
I have noticed you don’t monetize your website, don’t waste your traffic, you can earn extra bucks every month because you’ve
got hi quality content. If you want to know how to make extra bucks,
search for: Mertiso’s tips best adsense alternative
LikeLike