I'm struggling with creating a splash screen using (XML-based) vector drawables on old Android versions (API 16) in Xamarin.Android. I've read a lot of similar questions, but I can't get it to work no matter what I try.
I currently have the following, which works fine on recent Android versions (e.g. API 25):
drawable/itlogotext.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android" [...]>
[...]
</vector>
drawable/splash_screen.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#3498DB" />
</item>
<item
android:drawable="#drawable/itlogotext"
android:gravity="center" />
</layer-list>
values/styles.xml:
<resources>
[...]
<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen</item>
<item name="colorPrimaryDark">#1B6698</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
SplashActivity.cs:
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Support.V7.App;
using JetBrains.Annotations;
[Activity(Theme = "#style/MyTheme.Splash", MainLauncher = true, NoHistory = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Portrait)]
public sealed class SplashActivity : AppCompatActivity
{
protected override void OnResume()
{
base.OnResume();
this.StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
}
I have the latest support libraries (26.1.0.1, including Xamarin.Android.Support.v4 and Xamarin.Android.Support.v7.AppCompat, the latter depends on Xamarin.Android.Support.Vector.Drawable).
Unfortunately, when I try to run the app on old Android versions (e.g. API 16), I simply get an exception saying android.content.res.Resources$NotFoundException: File res/drawable/splash_screen.xml from drawable resource ID #0x7f020125, with this line at the bottom of the trace: Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector. Removing the <item> with the drawable from splash_screen.xml fixes the crash, but of course there's no logo in the splash screen anymore.
I have also tried adding AppCompatDelegate.CompatVectorFromResourcesEnabled = true in constructor, static constructor, and OnCreate, but no matter where I put it, the result is the splash screen not being shown at all (i.e. the Android app drawer is visible until the app's main screen is shown).
How can I get XML vector drawables on splash screens to work on earlier Android versions?
You could refer to :
https://bugzilla.xamarin.com/show_bug.cgi?id=41489
As #Jon Dick said, it should failed in < API Level 19. In API Level 21, vector support was added to Android, so it works on API Level 21+ with no changes, since the support library would just switch to the native implementation at that point.
Here is an solution : Custom an Android Application, add the AppCompatDelegate.CompatVectorFromResourcesEnabled = true like this :
[Application]
public class MainApplication : Application
{
public static Context AppContext;
public MainApplication()
{
}
public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer)
{
}
public override void OnCreate()
{
base.OnCreate();
AppCompatDelegate.CompatVectorFromResourcesEnabled = true;
}
}
Update :
You could use a picture as a background when Android device version is < 21.
Step 1 :
Create a values-21 folder in Resource folder, create a styles.xml :
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!--
Base application theme for API 21+. This theme completely replaces
MyTheme.Splash from BOTH res/values/styles.xml on API 21+ devices.
-->
<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen</item>
<item name="colorPrimaryDark">#1B6698</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
A Resource/values-v21/ directory contains resources that will be used when the device that is running your app is on API Level 21 or higher. If the device is running on an older version of Android, the Resource/values-v21/ directory will be ignored.
Step 2 :
In your Reources\values\styles.xml, modify the MyTheme.Splash's windowNoTitle with a different background.
Reources\values\styles.xml :
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen_low_version</item>
<item name="colorPrimaryDark">#1B6698</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
splash_screen_low_version.xml :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item>
<color android:color="#3498DB" />
</item>
<item
android:drawable="#drawable/yourlogo"
android:gravity="center"/>
</layer-list>
Please note that, yourlogo is a .png file.
Related
A splash screen implemented to be displayed while the Xamarin app loads in the background exhibits an unanticipated "upwards movement" while being displayed before switching to the main screen in Android devices (as seen in the following GIF of the loading screen).
My first assumption regarding the solution of this issue was to use the same style settings for both the splash screen activity and the main activity but it is to no avail as the unwanted motion is still exhibited, which, as of this moment, is completely baffling as the cause just cannot be segregated.
The splash screen is implemented as per the logic corresponding to the following code and style sheets in the Android Project:
Resources/drawable/splash_screen.xml
<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#color/splash_screen_background"/>
</item>
<!-- For bitmap images -->
<!--<item>
<bitmap
android:src="#drawable/app_icon"
android:tileMode="disabled"
android:gravity="center"/>
</item>-->
<!-- For Android's drawable vector images -->
<item
android:drawable="#drawable/android_v_drawable_application_icon"
android:gravity ="center"
android:width="150dp"
android:height="214.010dp" />
</layer-list>
values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="launcher_background">#FFFFFF</color>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="splash_screen_background">#FFFFFF</color>
</resources>
value/styles.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MainTheme" parent="MainTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from https://aka.ms/material-colors -->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#2196F3</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#1976D2</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#FF4081</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal. -->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">#style/AppCompatDialogStyle</item>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
<style name="SplashScreenTheme" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
</style>
</resources>
Attributes to the Android Activity class pertaining to the splash screen:
[Activity(Label = "App_App1",
Theme = "#style/SplashScreenTheme",
Icon = "#mipmap/icon",
ConfigurationChanges = ConfigChanges.ScreenSize
| ConfigChanges.Orientation,
MainLauncher = true,
NoHistory = true,
ScreenOrientation = ScreenOrientation.Portrait
)]
Thanks in advance.
I make the code sample to reproduce the "upwards movement".
In your description, you said "switching to the main screen in Android devices". I guess you have a special activity to do the splash screen. And have the main activity to LoadApplication.
When i set the same splash screen theme for the Splash_Activity and MainActivity, it would occure the "upwards movement".
So set the Theme to null or the default, it would be okay.
Splash_Activity:
[Activity(Label = "App_App1",
Theme = "#style/SplashScreenTheme",
Icon = "#mipmap/icon",
ConfigurationChanges = ConfigChanges.ScreenSize
| ConfigChanges.Orientation,
MainLauncher = true,
NoHistory = true,
ScreenOrientation = ScreenOrientation.Portrait
)]
MainActivity:
[Activity(Label = "SplashScreen", Icon = "#mipmap/icon", Theme = "#style/MainTheme", NoHistory = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
You could download the source file from the GitHub for reference.
https://github.com/WendyZang/Test/tree/master/SplashScreen2/SplashScreen
Removing the parent attribute of the <style> tag adhering to the splash screen (i.e. the <style> tag with the attribute name="SplashScreenTheme", in this case) removes the unwanted behavior.
I'm trying to create a toolbar to save items in my application. But my toolbar is not showing properly.
It is showing like this
I want to display like this
In my Fragment
public override void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
{
inflater.Inflate(Resource.Menu.menu_RefuelingToolbar, menu);
}
menu_RefuelingToolbar.xml
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".RefuelingFragment">
<item android:id="#+id/button_Save"
android:icon="#drawable/ic_done_white_24dp"
android:showAsAction="always"
android:title="Settings" />
</menu>
If you extend AppCompatActivity instead of Activity like this :
public class MainActivity : AppCompatActivity {...}//instead of Activity
In this case, the toolbar cant showing picture, the reason is that :
When using the appcompat library, menu resources should refer to the showAsAction in the app: namespace, not the android: namespace.
Similarly, when not using the appcompat library, you should be using the android:showAsAction attribute.
I think the problem is that you are mixing Framework Activity and AppCompat menu.
You should use AppCompatActivity with AppCompat Action bar and app:showAsAction; or Activity with android:showAsAction.
EDIT : Modify your code in menu_MGradeToolbar.xml like this :
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item android:id="#+id/button_MGradeSave"
android:icon="#drawable/ic_done_white_24dp"
app:showAsAction="ifRoom"
android:title="Save" />
</menu>
Here is the result.
I want to create a splash screen to my Android program. I am using C# and Xamarin in Visual studio to write my app.
I have followed a Xamarin Tutorial on how to create splash screen, when building the app, it gave me this error:
Error retrieving parent for item: No resource found that matches the
given name (Theme.AppCompat.Light)
in my Style.xml.
Here is my Style.xml file:
<?xml version="1.0" encoding="utf-8" ?>
<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">
<item name="android:windowBackground">#drawable/splash_screen</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
I have searched all the internet for a solution to my problem but didn't worked. Please don't consider it as duplicate cause I have seen all the answers and they didn't fixed my problem.
I have been having problems with incorporating a Progress Bar into my splash screen. I have an XML file called splash_setup,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background">
<bitmap
android:src="#drawable/splash"
android:tileMode="disabled"
android:gravity="top|center_horizontal" />
<ProgressBar
style="#android:style/Widget.ProgressBar.Small"
android:gravity="top|center_horizontal" />
</LinearLayout>
This file is used as background for 'splashTheme' (in Styles.XML) as following,
?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="splashTheme" parent="android:Theme">
<item name="android:windowBackground">#drawable/splash_setup</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
The splashTheme is used in splashActivity (the splash executes database initialisation and then finished),
[Activity(Label = "#string/appName", Theme = "#style/splashTheme",
MainLauncher = true, NoHistory = true)]
So to my problem... the splash_setup code runs fine without tags, but when they are in I get the following error,
android.content.res.Resources$NotFoundException: File res/drawable/splash_setup.xml from drawable resource ID #0x7f020002
Could anyone point out what I am doing wrong? Thanks!
What you have is a layout not a drawable. android:windowBackground attribute in style allows only drawable which may include shapes and/or layer lists. If you really wish to show a progressbar, then you need an activity.
I'm trying to create a new Xamarin.Forms project using Xamarin Studio (6.1.1 build 15. macOS v10.12).
I get a solution with 3 projects. One shared, one for iOS and one for Android. Everything build except the Android one. It fails when trying to compile the resources, specifically the styles file.
The following errors appear:
Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar'.
No resource found that matches the given name: attr 'colorAccent'.
No resource found that matches the given name: attr 'colorPrimary'.
No resource found that matches the given name: attr 'colorPrimaryDark'.
No resource found that matches the given name: attr 'windowActionBar'.
No resource found that matches the given name: attr 'windowActionModeOverlay'.
No resource found that matches the given name: attr 'windowNoTitle'.
Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.Dialog'.
No resource found that matches the given name: attr 'colorAccent'.
I tried to remove all Android support related libraries and Xamarin.Forms itself and add them again with NuGet. But that didn't help.
The Xamarin.Forms version I'm using is 2.3.2.127.
Maybe someone can help me out here?
Edit:
This is the file where the errors occurs. I didn't change anything. This is what Xamarin generated.
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="MyTheme" parent="MyTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#2196F3</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#1976D2</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#FF4081</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal. -->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">#style/AppCompatDialogStyle</item>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
</resources>
I also removed all packages and re-added Xamarin.Forms. It automatically adds the required support packages. But the error still occurs.
I solved this by downloading android_m2repository_r29.zip repository from https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/ with instruction given in same.