How to make activity transparent in Xamarin android - xamarin

I have refered here. But I just see a black background.
Then I use a Android standard Theme "ThemeTranslucentNoTitleBar". It is still a black background. So how to make a activity completely transparent?
protected override void OnCreate(Bundle bundle)
{
SetTheme(Android.Resource.Style.ThemeTranslucentNoTitleBar);
base.OnCreate(bundle);
SetContentView(Resource.Layout.xxx);
}

After trying lots of times. I find the resolution. You also have to set Android application theme. For exmaple <application android:theme="#android:style/Theme.Translucent" /> If you are using a custom application theme. Do not forget to set android:windowIsTranslucent = true
<style name="CustomTheme"
parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/CustomActionBar</item>
<item name="android:actionMenuTextColor">#color/actionbar_text</item>
<item name="android:windowIsTranslucent">true</item>
</style>

Related

How can I make my Xamarin.Forms for iOS and Android application show a splash screen as soon as possible after a user has opened the application?

I'm looking for hints, an example or anything that could guide me with how to make a start up splash screen for Xamarin application Xamarin.Forms application.
I assume I need to make different changes to the iOS and the Android projects and have researched what I can find already.
However much of the information seems dated or no longer works.
Would appreciate any suggestions and advice on how to do this for iOS and Android.
Xamarin.Forms is a solution that, in itself, takes a while to load. Since all cross-platform things have to wait for Xamarin, there is no cross-platform good implementation of a splash screen.
It looks like you'll have to implement it separately for iOS and Android. This will enable the splash screen to show up much faster and before the standard blue Xamarin screen.
EDIT: Alan asked for a complete solution, so here it is:
iOS: Make changes to the LaunchScreen.storyboard file in the Resources section of the .iOS project (that's the splash screen, by the way). I'd suggest opening it in Xcode instead because the storyboard editor of Xcode is frankly just better than Xamarin.
Android: You're going to have to have an image here. From official Xamarin support-
The quickest way to render and display the splash screen is to create a custom theme and apply it to an Activity that exhibits the splash screen. When the Activity is rendered, it loads the theme and applies the drawable resource (referenced by the theme) to the background of the activity. This approach avoids the need for creating a layout file.
The splash screen is implemented as an Activity that displays the branded drawable, performs any initializations, and starts up any tasks. Once the app has bootstrapped, the splash screen Activity starts the main Activity and removes itself from the application back stack.
Here's the specific code involved for that. Below is for a drawable. Place it in the Resources/Drawable folder of the Android project.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#color/splash_background"/>
</item>
<item>
<bitmap
android:src="#drawable/splash"
android:tileMode="disabled"
android:gravity="center"/>
</item>
</layer-list>
This is for a theme (add it to values/styles.xml, or add values/styles.xml if the document doesn't exist.).
<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/splash_screen</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>
Add this code to your Android project as MyTheme.cs:
[Activity(Theme = "#style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
static readonly string TAG = "X:" + typeof(SplashActivity).Name;
public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
{
base.OnCreate(savedInstanceState, persistentState);
Log.Debug(TAG, "SplashActivity.OnCreate");
}
// Launches the startup task
protected override void OnResume()
{
base.OnResume();
Task startupWork = new Task(() => { SimulateStartup(); });
startupWork.Start();
}
// Simulates background work that happens behind the splash screen
async void SimulateStartup ()
{
Log.Debug(TAG, "Performing some startup work that takes a bit of time.");
await Task.Delay (8000); // Simulate a bit of startup work.
Log.Debug(TAG, "Startup work is finished - starting MainActivity.");
StartActivity(new Intent(Application.Context, typeof (MainActivity)));
}
}
Then, after you've done all that for Android, remove the MainLauncher attribute from MainActivity, since your launcher is now MyTheme.
This is by no means easy, but it's the only way there is right now.
Make sure you do not have SimulateStartup sample code. It used to be boiler plate code a while ago:
class SplashActivity:
It forces an await when the app starts up incase you want to make any service calls on the splash screen
MainActivity.cs
// Launches the startup task
protected override void OnResume()
{
base.OnResume();
Task startupWork = new Task(() => { SimulateStartup(); });
startupWork.Start();
}
// Simulates background work that happens behind the splash screen
async void SimulateStartup ()
{
Log.Debug(TAG, "Performing some startup work that takes a bit of time.");
await Task.Delay (8000); // Simulate a bit of startup work.
Log.Debug(TAG, "Startup work is finished - starting MainActivity.");
StartActivity(new Intent(Application.Context, typeof (MainActivity)));
}
Other alternative to improve splash speed:
use small or native components to create your splash screen
make sure nothing is on OnResume and not calling any services

CardView styling not showing on device

I am trying to make a custom style for a cardview on my activity. It works just as I want to in Android Studio. But when I run it on my device - Galaxy S7 Edge android 7.0 - the cardview attributes are not changing. It uses the background from the style of the layout, and the rest of the attributes - elevation, corner radius and card background seem to be ingnored.
I dont get any errors in the log.
The layout uses style="#style/AddItemStyle"
The card uses style="#style/AddItemStyle.CardView"
Thats the card style
<style name="AddItemStyle.CardView" parent="CardView">
<item name="cardBackgroundColor">#color/card_bg</item>
<item name="cardCornerRadius">15dp</item>
<item name="cardElevation">15dp</item>
</style>
Thats the layout style
<style name="AddItemStyle" parent="Theme.AppCompat">
<item name="colorPrimary">#color/mySecondary</item>
<item name="colorAccent">#color/ColorMain</item>
<item name="editTextColor">#color/ColorMain</item>
<item name="android:background">#color/mySecondary</item>
</style>
In android studio
On my phone
For some reason removing android:theme="#style/AddItemStyle" from the activity in manifest fixed the problem. I don't know why but it worked.

Xamarin forms: Statusbar doesn't update color when NavBar changes color on Android

I'm making an app with Xamarin Forms.
Usually the app will have a blue NavBar, but I'm changing the NavBarfor a specific page.
The way I do that is:
InitializeComponent();
var navigationPage = Application.Current.MainPage as NavigationPage;
navigationPage.BarBackgroundColor = Color.FromHex("#99CC07");
But then the statusbar doesn't update it's color:
How can I make it so that the statusbar has the same (shade of) color as the navigationbar?
On iOS it works just fine.
You can modify both colors in the Android project. Go to Resources > values > styles.xml and edit the following values:
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#0066A1</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#0E4369</item>

How to set background image on xamarin forms master detail page and android

I want to set a navigation bar background image, but so far, no methods worked.
I don't know if I have to override the navigation-page renderer or if I have to change the "styles.xml", or if I have to override the Android "Toolbar", I have tried everything and I can't seem to find the solution.
I am using Xamarin Forms and Master Detail Page navigation.
I already tried changing styles.xml, making a custom toolbar layout, but doesn't mean I have done it right.
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>

animation effects are not working on locked screen

I am trying to give simple animation effect to dialog. Here I set animation theme to dialog for how it enters and how it exits. This code works properly on unlocked device but on locked device when that dialog comes and I clicked on cancel button, dialog disappear and locked screen is displayed but when i try to unlock screen immediately after clicking button then I see the dialog exits with that animation effect. But this effects goes to background and I want to show that animation effect on locked screen. The same code works on tablet but when i tried to do it on mobile it gives me this abnormal behavior.
this is zoom_out_exit.xml file.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator"
android:zAdjustment="top" >
<translate
android:duration="5000"
android:fromYDelta="0%p"
android:toYDelta="50%p" />
</set>
this is style.xml file
<style name="AnimationTheme" parent="#android:style/Theme.Translucent.NoTitleBar">
<item name="android:windowAnimationStyle">#style/DialogAnimation</item>
</style>
<style name="DialogAnimation" >
<item name="android:windowEnterAnimation">#anim/zoom_in_enter</item>
<item name="android:windowExitAnimation">#anim/zoom_out_exit</item>
</style>
and this theme of animationtheme is set to dialog.
class MyDialog extends Dialog {
MyDialog(Context context) {
super(context, R.style.AnimationTheme);
}
}
can anybody solve my problem ? thanks in advance.

Resources