How to change the android launcher icon for xamarin forms - xamarin.forms

There are 2 places I can change the icon for my xamarin forms android project.
One is in the
.Android > MainActivity.cs
[Activity(Label = "ZammyTestApp", Icon = "#mipmap/icon", Theme = "#style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
and there is also the manifest file.
Which one is the right place

I will suggest you to change the icon from MainActivity. Suppose you have an icon activity definition in your MainActivity and then you try to change icon from manifest file, icon would not change because icon activity definition in your Main Activity overrides the manifest.
Why MainActivity overrides the manifest?
If you click here, document states that if you use [Activity] custom attribute, it causes xml fragment to be added to AndroidManifest.xml at compile time.
Click here to see that how the [Activity] attribute helps to produce icon in xml fragment. For example:
[Activity (Label="My App", MainLauncher=true, Icon="#drawable/myicon")]
public class MyActivity : Activity
{
}
This example produces the following xml fragment:
<activity android:icon="#drawable/myicon" android:label="My App"
android:name="md5a7a3c803e481ad8926683588c7e9031b.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And check the accepted answer here in xamarin forums.

Related

Cross Platform Xamarin Forms App - Android splash screen not displaying

I am creating my first Xamarin Forms cross-platform application. I am in the process of creating a splash screen using the native platform code for each OS.
My Android splash screen is not displaying. I trace through the application and it hits the correct code, but the page does not display.
My xml file is in the resources\layout folder which renders nicely when I view it. It is called SplashScreen.xml.
Can anyone shed some light as to why the page does not display?
{
[Activity(MainLauncher = true, NoHistory = true)]
public class SplashScreenActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
RequestWindowFeature(WindowFeatures.NoTitle);
base.OnCreate(savedInstanceState);
SetContentView(Droid.Resource.Layout.SplashScreen);
System.Threading.Thread.Sleep(3000);
StartActivity(typeof(MainActivity));
}
public override void OnBackPressed() { }
}
}
Better Approach
In your Resources -> Values -> styles.xml create your splash screen theme.
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="splashscreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splashscreen</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
Go to your MainActivity.cs and ensure MainLauncher = true. Ensure no other activity has MainLauncher=true.
In MainActivity.cs change the default theme to the splash screen.
[Activity(Label = "Mobile App", Theme = "#style/splashscreen", Icon = "#drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode = LaunchMode.SingleTop)]
public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
In OnCreate, switch your theme with the below code.
protected override void OnCreate(Bundle bundle)
{
base.Window.RequestFeature(WindowFeatures.ActionBar);
// Name of the MainActivity theme you had there before.
// Or you can use global::Android.Resource.Style.ThemeHoloLight
base.SetTheme(Resource.Style.MainTheme);
base.OnCreate(bundle);
...
Please refer this blog.
You should write a theme to load your xml first, like this:
<resources>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light">
</style>
<style name="MyTheme" parent="MyTheme.Base">
</style>
<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/SplashScreen</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowActionBar">true</item>
</style>
</resources>
Then use that theme in the activity:
[Activity(Theme = "#style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
Here is the document about Splash Screen in Xamarin.Android and there are detailed steps you can follow.
A sample project is also available : splashscreen.

No resource identifier found for attribute 'configchanges' in package 'android'

<activity android:configChanges="orientation|screenSize" android:icon="#drawable/a512" android:label="KMI" android:theme="#style/MainTheme" android:name="md5ce51fed3f5ce2f508bfc10049c6540f6.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
the problem seems to be with configchanges part here. Is there any way to fix it? I did try to remove it, but it just comes back and the same error comes up.
[Activity(Label = "KMI", Icon = "#drawable/a512", Theme = "#style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
If you are using Xamarin, it is better that you provide these in the Activity Attribute of your Activity class file. Assume that you would like to do this in your MainActivity file. Then what you can do is something like below:
[Activity(ScreenOrientation = ScreenOrientation.Portrait, Theme = "#style/MyTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global:: Xamarin.Forms.Platform.Android.FormsAppCompatActivity

Xamarin.Forms: App crash after splash activity

I have splash screen, When I run my app its crash. Here is my splash screen code
If I don't use splash screen app is working fine but when I use splash screen and close my app and run again its crash after splash screen.
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var mainIntent = new Intent(Application.Context, typeof(MainActivity));
if (Intent.Extras != null)
{
mainIntent.PutExtras(Intent.Extras);
}
mainIntent.SetFlags(ActivityFlags.SingleTop);
StartActivity(mainIntent);
}
}
I presume the issues is connected with the fact that you are trying to start the already started SingleTop activity for the second time.
However, the recommendation is to write a splash screen a bit differently - without needing separate activity. See this nice blog post by Adam Pedley on splash screen implementation in Xamarin.Forms.
Instead of having a separate activity, you can just temporarily apply a "splash" theme to the main activity, before the activity loads. This is useful, because it makes your app load faster than having a full separate splash screen activity.
Create a style in Resources/values/styles.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="splashscreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splashscreen</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
<resources>
Set this theme to the MainActivity via attribute:
[Activity(Label = "Mobile App",
Theme = "#style/splashscreen",
Icon = "#drawable/icon",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize |
ConfigChanges.Orientation,
LaunchMode = LaunchMode.SingleTop)]
public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
And then override the OnCreate method to set the actual theme back:
protected override void OnCreate(Bundle bundle)
{
base.Window.RequestFeature(WindowFeatures.ActionBar);
// For global use "global::" prefix - global::Android.Resource.Style.ThemeHoloLight
base.SetTheme(Resource.Style.MainTheme);
base.OnCreate(bundle);
...
}
Happened to me on .net Maui, It turned out I didn't register the services correctly, so the builder could not generate the page.

How can I make CommandBar render in Xamarin Forms UWP?

I have a ContentPage (actually an MvxContentPage) with ToolbarItems defined. The Items appear in Android and iOS as expected, but do not appear at all in UWP. I have tried setting SetToolbarPlacement to both Top and Bottom manually, in both the constructor and the OnAppearing method. Thus far, I have not been able to affect any change in the UWP application. Am I doing something wrong? Can Mvx not render the Toolbar?
<mvx:MvxContentPage
xmlns:mvx="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
x:TypeArguments="viewModels:CategoryListViewModel"
xmlns:viewModels=""
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="GSP.X.AccuStore.Forms.Views.Site.Profiles.CategoryListView">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Areas" Command="{Binding Path=GoToAreasCommand}" />
</ContentPage.ToolbarItems>
</mvx:MvxContentPage>
The problem is you have not add start page to NavigationPage. I have test with the follow code in the blank Xamrin.Froms app, the ToolbarItem display well in the top of MainPage.
public App()
{
InitializeComponent();
var nav = new NavigationPage(new MainPage());
MainPage = nav;
}

Embedded images not showing

This is my page in portable project
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:b="clr-namespace:Corcav.Behaviors;assembly=Corcav.Behaviors"
xmlns:local="clr-namespace:MyMobileApp;assembly=MyMobileApp"
x:Class="MyMobileApp.MainPage"
x:Name="MainPage">
<Image Source="{local:ImageResource myimage.jpg}" />
This is my ImageResourceExtension in same portable project
namespace MyMobileApp
{
[ContentProperty("Source")]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Source == null)
return null;
var imageSource = ImageSource.FromResource(Source);
return imageSource;
}
}
}
I have tried to add myimage.jpg as embedded in root of my project and in Resources folder, but no image is shown.
While debugging I see that the returned imageSource is of type Xamarin.Forms.StreamImageSource. How do I check if this is really found?
Can anyone spot the error here?
The correct XAML was to add the app name to the source.
<Image Source="{local:ImageResource MyMobileApp.myimage.jpg}" />
By default the image will have Build Action: None; this needs to be set to Build Action: EmbeddedResource.
Right click on Image > properties > set [Build Action: EmbeddedResource]
[]1
Can you explain the intent of the extension a bit more? Assuming you are putting your images in the proper folders ('Resources' on iOS, 'Resources/Drawable' on Android), then all you need in your XAML is just:
<Image Source="FeaturedAreaMockup.jpg" />
That will find the images in the appropriate folder and show them - why aren't you just doing that? What's the point of the extension?
To read assets/resources folder you need to use:
ImageSource.FromFile("myimage.jpg");'
ImageSource.FromResource uses images included as EmbeddedResource
More here: https://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/images/

Resources