Home > front end >  How can I create a transparent activity in Xamarin Android? Applying the theme crashes the app
How can I create a transparent activity in Xamarin Android? Applying the theme crashes the app

Time:05-31

I created a biometric authentication service that starts an activity.

The activity then starts another activity with StartActivityForResult.

I created a style with the intention to make both activities invisible.

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>


[Activity(Label = "BiometricActivity", Theme = "@style/Theme.AppCompat.Translucent")]
public class BiometricActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static event EventHandler<BiometricEventArgs> BiometricEventHandler;
    private readonly int BIOMETRIC_REQUEST = 1;

    protected override void OnCreate(Bundle savedInstanceState)
    {           
        base.OnCreate(savedInstanceState);

        Intent intent = new Intent(Application.Context, typeof(BiometricAuth));            
        StartActivityForResult(intent, BIOMETRIC_REQUEST);
    }
}

[Activity(Label = "BiometricAuth", Theme = "@style/Theme.AppCompat.Translucent")]
public class BiometricAuth : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{        
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetResult(Result.Ok);

        Finish();
    } 
}

When I define the activities with Theme in the ActivityAttribute, the style is applied and looks great, but as soon as the activities finish the app crashes with NullReferenceException

Translucent

[InputMethodManager] prepareNavigationBarInfo() DecorView@9cdfd0[BiometricActivity]
[InputMethodManager] getNavigationBarColor() -855310
[InputMethodManager] prepareNavigationBarInfo() DecorView@9cdfd0[BiometricActivity]
[InputMethodManager] getNavigationBarColor() -855310
[InputMethodManager] Starting input: tba=com.aplicatzia.meklockv2 ic=null mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
[InputMethodManager] startInputInner - Id : 0
[InputMethodManager] startInputInner - mService.startInputOrWindowGainedFocus
[InputTransport] Input channel destroyed: 'ClientS', fd=98
[ViewRootImpl@86b53bb[BiometricAuth]] MSG_WINDOW_FOCUS_CHANGED 0 1
[InputMethodManager] prepareNavigationBarInfo() DecorView@e5be706[BiometricAuth]
[InputMethodManager] getNavigationBarColor() -855310
[ViewRootImpl@c984152[MainActivity]] stopped(false) old=false
**System.NullReferenceException:** 'Object reference not set to an instance of an object.'

Worker Thread       System.Diagnostics.Debugger.Mono_UnhandledException_internal()
0xFFFFFFFFFFFFFFFF in System.Diagnostics.Debugger.Mono_UnhandledException_internal
0x1 in System.Diagnostics.Debugger.Mono_UnhandledException at /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Diagnostics/Debugger.cs:125,4
0x20 in Android.Runtime.DynamicMethodNameCounter.55
0x6 in Xamarin.Forms.Platform.Android.PlatformConfigurationExtensions.OnThisPlatform<Xamarin.Forms.Application> at D:\a\_work\1\s\Xamarin.Forms.Platform.Android\PlatformConfigurationExtensions.cs:8,4
0xC in Xamarin.Forms.Platform.Android.AppCompat.FragmentContainer.OnResume at D:\a\_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\FragmentContainer.cs:126,4
0x8 in AndroidX.Fragment.App.Fragment.n_OnResume at C:\a\_work\1\s\generated\androidx.fragment.fragment\obj\Release\monoandroid12.0\generated\src\AndroidX.Fragment.App.Fragment.cs:2570,4
0x11 in Android.Runtime.DynamicMethodNameCounter.55

If I instead apply both activities with the theme programmatically with SetTheme, the crash does not happen, but the background is black instead of transparent.

[Activity(Label = "BiometricActivity")]
public class BiometricActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static event EventHandler<BiometricEventArgs> BiometricEventHandler;
    private readonly int BIOMETRIC_REQUEST = 1;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        SetTheme(Resource.Style.Theme_AppCompat_Translucent);
        base.OnCreate(savedInstanceState);
    }
}

Black background

What can I do to create a transparent background for both activities?

CodePudding user response:

At first, if your project is a xamarin.android project, I suggest you to make the activity implement the AppCompatActivity. Such as:

public class MainActivity : AndroidX.AppCompat.App.AppCompatActivity{}

And for a forms project, there is also only one MainActivity in it. I had test your theme in a forms project and android project. Both of them worked well.

So you can have a try and use it such as:

[Activity(Label = "BiometricActivity", Theme = "@style/Theme.AppCompat.Translucent")]
public class BiometricActivity : AndroidX.AppCompat.App.AppCompatActivity
  • Related