Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Title Bar In Xamarin.Android

I just started out with Xamarin. Spent literally a day fixing stupid debugging issues even in VS 2019. Anyways, i guess all the Xamarin devs reading this post already knows how much of a pain in the ar$e Xamarin is.

Anyways, in my Xmarin.Android (Blank) project, i am trying to remove the title bar which says the app/project name(I guess so). So, here's what i'm talking about :

App Header

Please note :

Before i go on saying what i tried, i must add, all of the solutions were found from Xamarin.Forms related posts/forums. I am very confused thinking if i made the wrong choice by choosing Xamarin.Android as it has little to no support available.

What i tried so far :

  • Added android:theme="@android:style/Theme.NoTitleBar" in AndroidManifest.xml

  • Added RequestWindowFeature(WindowFeatures.NoTitle); in MainActivity.cs, inside OnCreate method, before calling base.OnCreate.

The result remains the same. The title bar is always there. However, here's another mysterious bug of Xamarin(Ah, nothing new, right?) i found!

When changing the theme from the designer's top bar :

Top Bar

the changes are reflected in the designer. I mean if i change the theme to, say, Black NoTitleBar, the title bar goes away. But as soon as i launch the app, the title bar pops up in the emulator as well as in the designer.

So, is this the way it is? Are there any Xamarin.Android specific solution for this? Or did i really make a bad decision choosing Xamarin.Android as it lacks support forums?

EDIT 1

When i change the manifest file manually and set android:theme="@android:style/Theme.NoTitleBar", then from the designer, i change the theme to AppDefault, the manifest file seems to remain the same. I mean the theme declared in the manifest file remains Theme.NoTitleBar.

And this is how the Activity attribute looks like :

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]

Changing AppTheme with Theme.NoTitleBar throws a runtime exception saying No resource found with the given name (Theme.NoTitleBar).......

Update on Xamarin - Update 1.0

So it's been about a year since i posted this question. In this time, i have had worked extensively with Xamarin, both Xamarin Native and Xamarin Forms. So i'm thinking of sharing my views on Xamarin. So let's start with the pros first :

Xamarin Native If you're someone who wants to take freelance projects and wants to handle both the backend as well as the app development yourself, then Xamarin Native is actually a great choice(if you come from the .Net stack). Before you start bashing me, please note that i'm a senior engineer who has worked with android native development in Java, iOS in Swift, cross-platform in React Native along with Xamarin. So getting back to where i was, Xamarin is actually an amazing tool. The Native part of Xamarin is literally no different than the actual Native development. From building you dream UI, to writing the best in-app image caching libraries, to building the best performing app, you can do it all with Xamarin native.

Xamarin Forms Now i've seen a lot of tutorials on Xamarin Native vs Forms and their biggest argument is that you can build native UIs with Xamarin Native but not with Forms. As i've had a great fortune working as a UI designer, i can say for sure that it's an absurd logic to justify using Xamarin Native. Even in Xamarin Forms, you can build the best looking UI. From freelance to enterprise projects, if you want to save a lot of money and if you want an amazing looking app with a great REST API supported backend, all powered by C#, Xamarin Forms is the best way to go. There's literally no arguments on that. If you want to put forward any arguments, i highly suggest you to point to some data as well that support your arguments.

Anyways, as i mentioned the pros in a short manner, let me talk a bit about the cons(Yeah, i know, the non-C# community should start smiling by now). So here're the top cons of using Xamarin, both native and forms:

  1. You don't fix your bugs, but rather the bugs that already exist there

My my! This is the biggest pain in the b**t! If you're someone who started off with Xamarin two years back, in Visual Studio 2017, i think you can relate the most to it. I literally had to spend countless hours just to get a "Hello World" app running, because guess what? I had to fix the bugs pre-existing in Xamarin. Now don't get baffled with the fact that Visual Studio is the best IDE so it can point out where the bug is coming from. Believe me, IT CAN'T!!!.

That's not the worst part tho. The worst part is, even the Xamarin community can't help you! How can they, if they don't actually exist lol ? ... which brings me to the next point :

  1. A community so small that you literally need glasses to see!

Sorry to put it that way, but this question is about a year old. How many well-written answers do you see below? I see 2 answers with little to no explanation unfortunately! Well, not to hurt the feelings of those who answered, but that's the truth. Even if you seek help on Xamarin's official site, you'ld be lucky even to get a response like "Can you post the entire exception log ?". So with a very small community, on an initial stage, you're literally on your own :(

  1. Millions of libraries, yet not the useful ones.

Yes, with C#, you get access to a billion of libraries. But when it comes to native Android and iOS, Xamarin literally makes you feel handicapped :(

I remember that i once needed something similar to Picasso for image caching but the best option Xamarin had was FFImageLoading. The library is good but not even close to Picasso. The first time i installed and ran it to get a number of images from the web, it crashed my entire app, without even pointing to what caused the crash!!... which brings me to :

  1. It's not just you, but even the IDE is clueless

Yes, when your app crashes, the IDE just shows you the tip of the ice-berg :( You are thrown ambiguous exceptions like Java exited with code 1 and bla-bla! But when you ask the IDE what caused it, the IDE be like : "Meh, i don't know dude. What is google for?". Then you run to google. You search up the issue and you see that it is literally related to something that you haven't even touched yet! But guess what actually fixes the issue? Remove the last library your installed or remove the last line you wrote and wallah! it works again!!!

Now i guess i've talked enough and it's time to encourage you to use Xamarin. Let me give you some tips i followed, which will definitely help you be on track with Xamarin :

  1. Search issues with Android Java, not with Xamarin.Android

For the most cases, you can find the best solution by typing your issue in google and instead of mentioning "in/for Xamarin.Android" use the term "android java", because that's what Xamarin compiles down to. The Native android has a much bigger community, so you have much higher chances of finding your solution! Yes, sometimes you'll find Java code that you'ld have hard time to find the perfect C# syntax for, but the xamarin community on the official Xamarin site has most of it written out, in their answers, for you. So once you get a java code you don't know how to write in C#, just search up the line of code and add "Xamarin" in the end of your search query.

  1. Use Android Studio and XCode for design

Never ever, i repeat never ever, use the Xamarin designer, specially on Android. It is so misleading that even if you write a correct syntax in XML, it shows you red lines, and when you actually actually write something wrong, it complies perfectly but crashes on launch(or on the specified screen) leaving you mostly clueless. So always use Android Studio and XCode for designing. Create the drawables, reference them in your fragments or views within Android Studio and create your dream design. When all is good, just copy paste the files/codes in them back to Visual Studio and it'll save of tons of headache and time! However, the Xamarin iOS designer does seem to work a bit better than the android one. But whenever it throws you some random error, just copy paste the code from the .xib or storyboard file and open it in XCode, you will have much better understanding of what's going on!

  1. Clean, rebuild, launch, crash, restart PC, clean, rebuild, launch success!

Yep, that's the secret 99% newcomers don't know! But i hope you know it now ;-)

  1. Build your own libraries

Yes, i know you found the best library you've been looking for and it has a million+ downloads, but if it makes your app crash unexpectedly, it's time to write your own library! It's not gonna be optimal, but it'll help you sharpen your coding skills and you'll have full control on what's going on. And this advice is coming from someone who had to spend a week writing an amazing image caching library for not just Xamarin but for any platform powered by C#(I'm thinking of releasing on the Nuget store someday).

  1. Use the latest Visual Studio

Spend your time and download the latest Visual Studio(2019, at the time of writing this) with all the latest updates. VS 2019 has tons of bug fixes for Xamarin and even the designers work pretty well now! So go on, update!!

Oh, and for the bigger question: Why use Xamarin Forms instead of React Native or Flutter or anything else?

Well, the one line answer is : With Xamarin, you only need to write C#, with others, you end up writing a lot of Java and Swift code.

Anyways, let's talk a bit about THE FUTURE!!

So, to be honest, Xamarin never led the market. You will find some big apps built with Xamarin but the numbers are small! But if you don't judge a technology by the number of projects it powers, then you'ld understand that Xamarin is actually a game changer! You can build anything and everything with it. However, i'ld still suggest you try out React Native or Flutter, because if you know multiple technologies, you have better knowledge of what to choose when, under what circumstances. So best of luck for your future! Pick your tech and start rocking!

Cheers!!

like image 574
Christopher H. Avatar asked Apr 04 '19 15:04

Christopher H.


Video Answer


2 Answers

Hmm, so it was a one line fix after all?

Anybody struggling with this issues: The following change in the activity class MainActivity.cs fixed it :

[Activity(Label = "@string/app_name", Theme = "@style/Theme.AppCompat.Light.NoActionBar", MainLauncher = true)]

You can set other themes as well, i guess. After all, you are the UI master!

like image 182
Christopher H. Avatar answered Sep 17 '22 20:09

Christopher H.


[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

This work for me

like image 29
sohail sk Avatar answered Sep 18 '22 20:09

sohail sk