Keyboard scroll up and hide Toolbar and Status bar on Android - xamarin

I want to keep status bar and toolbar still visible on top while displaying soft keyboard. I tried many ways but cannot done. Tried windowSoftInputMode and set/clear flag of windows, but all failed.
Could anyone help me, please ?
Please click here to see gif file!

This is a Xamarin bug. See here.
There are some workaronds. You have to do the following in your MainActivity:
protected override void OnCreate(Bundle bundle)
{
ToolbarResource = Resource.Layout.toolbar;
TabLayoutResource = Resource.Layout.tabs;
base.OnCreate(bundle);
//Remove the status bar underlay in API 21+
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
Window.DecorView.SystemUiVisibility = 0;
var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
statusBarHeightInfo.SetValue(this, 0);
Window.SetStatusBarColor(new Android.Graphics.Color(18, 52, 86, 255));
}
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
App.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
}
If you use FormsApplicationActivity instead of FormsAppCompatActivity, then you have to remove the following lines:
var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
statusBarHeightInfo.SetValue(this, 0);
You can see more details with Jimmy Garrido here.
But to hide ActionBar, if you use the following code in your MainActivity, so only the above solution will not work:
Xamarin.Forms.Forms.SetTitleBarVisibility(Xamarin.Forms.AndroidTitleBarVisibility.Never);
To fix the problem, what worked for me was to delete this line above and create a specific theme to make the ActionBar disappear.
Under Resources / values / Styles.xml I created a new theme (you can change yours if you already have one):
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarSize">0dp</item>
</style>
</resources>
And don't forget to change your theme in MainActivity, like:
[Activity(WindowSoftInputMode = SoftInput.AdjustPan, Label = "Test", Icon = "#drawable/icon", Theme = "#style/MyTheme", MainLauncher = true, NoHistory = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
I hope this can help you.

Related

How can I code-behind use Font Awesome in Xamarin Forms?

I'm having difficulty creating a form with Font Awesome text. Everything works fine when I'm doing it in .xaml file, like this:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Name="contentPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="myappname.Main">
<Label Text=""
FontFamily="Font Awesome 5 Free-Solid-900.otf#Regular"
FontSize="40" />
</ContentPage>
The result is:
Assets catalog:
I'm not creating my app for iOS so I haven't added any additional configuration, i.e. configuring ResourceDictionary. This is what I've tried in Main.xaml.cs (I'll show three ways of doing it because I'm not sure how exactly it should work):
protected override async void OnAppearing()
{
Label newLabel = new Label()
{
Text = "",
FontFamily = "Font Awesome 5 Free-Solid-900.otf#Regular",
FontSize = 40
};
contentPage.Content = newLabel;
}
protected override async void OnAppearing()
{
Label newLabel = new Label()
{
Text = "",
FontFamily = "Font Awesome 5 Free-Solid-900.otf#Font Awesome 5 Free-Solid-900.otf",
FontSize = 40
};
contentPage.Content = newLabel;
}
protected override async void OnAppearing()
{
Label newLabel = new Label()
{
Text = "",
FontFamily = "Font Awesome 5 Free-Solid-900.otf#Font Awesome 5 Free-Solid-900",
FontSize = 40
};
contentPage.Content = newLabel;
}
The result in every of them is:
What am I doing wrong?
Try using \u instead of &#x
so it should look like this:
Text = "\uf00c",

Xamarin Forms Android - crash while taking photo without exception

Xamarin Forms Android crashes while taking photo without exception. But this happens in like 65% of tries. Sometimes app doesn't crash and I can take photo normally. On other phone(same model) application crashes every time I want to take photo.
I'm using https://github.com/jamesmontemagno/MediaPlugin like this:
if (!CrossMedia.Current.IsCameraAvailable)
{
DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
return;
}
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "testPO.jpg"
});
if (file == null)
return;
DisplayAlert("File Location", file.Path, "OK");
Camera loads without problem, but when I press "Take photo" button application crashes and debugging in Visual Studio stops without any exception.
Thank you for any help.
I realize this is old but in case someone stumbles across this issue in the future:
I've had this issue and it ended up being the NoHistory flag in MainActivity.cs being set to true.
Remove NoHistory = true or set NoHistory = false to avoid the crash.
eg:
[Activity(Label = "MyApp", Icon = "#mipmap/icon", Theme = "#style/MainTheme", ScreenOrientation = ScreenOrientation.Portrait, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, NoHistory = false)]
public class MainActivity : FormsAppCompatActivity {
//...
}

How to change height of progress bar in Xamarin Forms?

How do I change the height of a Xamarin Forms ProgressBar in code? Am using Xamarin Forms V2.1.
.HeightRequest and .MinimumHeightRequest seem to have no effect. The default progress bar is so thin that it might not even be noticed.
.BackgroundColor does not seem to work either.
What am I missing here?
You need custom renderers for this:
First create a class for your custom progress bar:
public class CustomProgressBar :ProgressBar
{
public CustomProgressBar()
{
}
}
And then also add a new file for your custom progress bar renderer:
For iOS:
public class CustomProgressBarRenderer : ProgressBarRenderer
{
protected override void OnElementChanged(
ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
{
base.OnElementChanged(e);
Control.ProgressTintColor = Color.FromRgb(182, 231, 233).ToUIColor();// This changes the color of the progress
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
var X = 1.0f;
var Y = 10.0f; // This changes the height
CGAffineTransform transform = CGAffineTransform.MakeScale(X, Y);
Control.Transform = transform;
}
}
[EDIT: fixed code above as per comment]
For Android:
public class CustomProgressBarRenderer :ProgressBarRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
{
base.OnElementChanged(e);
Control.ProgressTintList = Android.Content.Res.ColorStateList.ValueOf(Color.FromRgb(182, 231, 233).ToAndroid()); //Change the color
Control.ScaleY = 10; //Changes the height
}
}
I hope this helps you!
To make the progress bar thicker, you just have to change the ScaleY property of the ProgressBar.
For example:
This code
<ProgressBar Progress=".5"/>
<ProgressBar ScaleY="2" Progress=".5"/>
<ProgressBar ScaleY="5" Progress=".5"/>
Produces this
Note that you may need to adjust your margins accordingly.
<ProgressBar
BackgroundColor="White"
ProgressColor="#BCC7EF"
Progress="0.7">
<ProgressBar.ScaleY>
<OnPlatform
x:TypeArguments="x:Double"
iOS="2"
Android="1" />
</ProgressBar.ScaleY>
#BillF is correct basically
In the iOS renderer code try using
this.Control.Transform = transform;
instead of
this.Transform = transform;
I faced the same need and on the latest version of Visual Studio, 16.5.2 I figured out that to get a bigger horizontal bar you just need to set ScaleY within the progressbar declaration inside the xml.
To avoid glitches on Android and be sure that the progress bar is not overwhelming other elements I added a margin as you can see from the declaration here below.
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/progressBar1"
android:layout_below="#+id/message"
android:scaleY="8"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"/>
For me, the most simple solution is to use Xamarin.Forms.Visual.Material
Then in your XAML, in progress bar set HeightRequest property to what you want and use Visual property as Material.

Xamarin forms - set switch text color

How can I set the color of the text in an android renderer? I have the following renderer:
public class CustomSwitchRenderer : SwitchRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.TextOn = "Yes";
Control.TextOff = "No";
Android.Graphics.Color colorOn = Android.Graphics.Color.Rgb(239, 201, 6);
Android.Graphics.Color colorOff = Android.Graphics.Color.LightGray;
Android.Graphics.Color colorDisabled = Android.Graphics.Color.Gray;
Android.Graphics.Color textColor = Android.Graphics.Color.Black;
Control.SetTextColor (ColorStateList.ValueOf (textColor));
Control.SetTextColor (textColor);
StateListDrawable drawable = new StateListDrawable();
drawable.AddState(new int[] { Android.Resource.Attribute.StateChecked }, new ColorDrawable(colorOn));
drawable.AddState(new int[] { -Android.Resource.Attribute.StateEnabled }, new ColorDrawable(colorDisabled));
drawable.AddState(new int[] { }, new ColorDrawable(colorOff));
Control.ThumbDrawable = drawable;
}
}
}
I am able to change the color of the switch, but I can't figure out how to change the color of the YES/NO text. SetTextColor doesn't seem to work.
Create an xml file under the Resources\values directory in your droid project.
doesn't matter the name but it needs to end in .xml and contain
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor">#00FF00</item>
</style>
</resources>
Then in your renderer call Control.SetSwitchTextAppearance. Pass in the Context and the ResId of the resource you created. You can get the Id from the generated file under Resources titled Resource.designer.cs. Alternately you can call the Const that generated as follows.
Control.SetSwitchTextAppearance (Control.Context, Resource.Style.CodeFont);
Hope it helps. If you cant get it let me know Ill upload a sample app.

Xamarin forms: hiding blue line from selected tab with a CustomTabRenderer for android

I made a customrenderer for showing images in the tabs for the Android platform.
Now i'd like to know how i can remove the bottom line that shows which tab is selected.
Also, where can i get info about creating custom renderers? i looked on youtube but the example with the rounded corners doesn't show much...
namespace Plopsa.Android
{
public class CustomTabRenderer: TabbedRenderer
{
private Activity _activity;
protected override void OnModelChanged(VisualElement oldModel, VisualElement newModel)
{
base.OnModelChanged(oldModel, newModel);
_activity = this.Context as Activity;
}
// May put this code in a different method - was just for testing
public override void OnWindowFocusChanged(bool hasWindowFocus)
{
// Here the magic happens: get your ActionBar and select the tab you want to add an image
ActionBar actionBar = _activity.ActionBar;
if (actionBar.TabCount > 0)
{
ActionBar.Tab tabOne = actionBar.GetTabAt(0);
tabOne.SetIcon(Resource.Drawable.icon_tab1);
ActionBar.Tab tabTwo = actionBar.GetTabAt(1);
tabTwo.SetIcon (Resource.Drawable.icon_tab2);
ActionBar.Tab tabThree = actionBar.GetTabAt(2);
tabThree.SetIcon(Resource.Drawable.icon_tab3);
ActionBar.Tab tabFour = actionBar.GetTabAt(3);
tabFour.SetIcon(Resource.Drawable.icon_tab4);
}
base.OnWindowFocusChanged(hasWindowFocus);
}
}
You can achieve the desired result by setting the Tabbar.axml class 'android:tabIndicatorColor' to the same value as the 'android:background'.
Below is the code showing the desired result with a tab indicator matching the tab background color. This is all contained in the Tabbar.axml file. You can register the tabs in the MainActivitiy.cs.
Tabbar.axml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:tabTextAppearance="#style/MyCustomTabText"
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Light"
app:tabTextColor="#color/primaryOrange"
app:tabIndicatorColor="?attr/colorPrimary"
app:tabIndicatorHeight="0dp"
app:tabSelectedTextColor="#color/primaryOrange"
app:tabGravity="fill"
app:tabMode="fixed" />
MainActivity.cs
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
Example Result

Resources