How to hide softkeyboard in xamarin Android programmatically - xamarin

This is my code to show keyboard:
AppData.SoftKeyBoard += () =>
{
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
imm.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
};
I need a method to hide the softkeyboard.

Here you go. This is code for Visual Studio.
// Hide keyboard
var inputManager = (InputMethodManager)GetSystemService(InputMethodService);
inputManager.HideSoftInputFromWindow(btnSignIn.WindowToken, HideSoftInputFlags.None);

this code for hide soft keyboard in fragment of Xamarin Native Android:
View view = inflater.Inflate(Resource.Layout.fragment, container, false);
var imm =(InputMethodManager)Activity.GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(view.WindowToken, 0);
and in Activity you can use this code:
InputMethodManager imm = (InputMethodManager)this.GetSystemService(Activity.InputMethodService);
//Find the currently focused view, so we can grab the correct window token from it.
View view = this.CurrentFocus;
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(this);
}
imm.HideSoftInputFromWindow(view.WindowToken, 0);

Related

Should I be thinking differently about cancelling the back button in Xamarin Forms

I have a Prism based Xamarin Forms app that contains an edit page that is wrapped in a Navigation page so there is a back button at top left on both Android and iOS. To avoid the user accidentally losing an edit in progress by accidentally clicking the back button (in particular on Android) we want to prompt them to confirm that they definitely want to cancel.
Thing is, this seems like something that is not baked in to Xamarin forms. You can override OnBackButtonPressed in a navigation page, but that only gets called for the hardware/software back button on Android. There are articles detailing techniques to intercept the actual arrow button at the top left on Android (involving overriding OnOptionsItemSelected in the Android MainActivity), but on iOS I'm not sure it is even possible.
So I can't help but wonder if I am going about this the wrong way? Should I not be intercepting the top left / hardware / software back button in this way? Seems like a pretty common thing to do (e.g. press back when editing a new contact in the android built in Contacts app and you get a prompt) but it really feels like I am fighting the system here somehow.
There are previous questions around this, most relevant appears to be How to intercept Navigation Bar Back Button Clicked in Xamarin Forms? - but I am looking for some broad brush suggestions for an approach here. My objective is to show the user a page with the <- arrow at top left for Android, "Cancel" for iOS but I would like to get some views about the best way to go about it that does not involve me fighting against prism / navigation pages / xamarin forms and (where possible) not breaking the various "best practices" on Android and iOS.
After going down the same path as you and being told not to prevent users from going back, I decided on showing an alert after they tap the back button (within ContentPage.OnDisappearing()) that says something like Would you like to save your work?.
If you go with this approach, be sure to use Application.MainPage.DisplayAlert() instead of just this.DisplayAlert() since your ContentPage might not be visible at that point.
Here is how I currently handle saving work when they click the back button (I consolidated a good bit of code and changed some things):
protected override async void OnDisappearing() {
base.OnDisappearing();
// At this point the page is gone or is disappearing, but all properties are still available
#region Auto-save Check and Execution
/*
* Checks to see if any edits have been made and if a save is not in progress, if both are true, it asks if they want to save, if yes, it checks for validation errors.
* If it finds them, it marks it as such in the model before saving the model to the DB and showing an alert stating what was done
*/
if(!_viewModel.WorkIsEdited || _viewModel.SaveInProgress) { //WorkIsEdited changes if they enter/change data or focus on certain elements such as a Picker
return;
}
if(!await Application.Current.MainPage.DisplayAlert("ALERT", "You have unsaved work! Would you like to save now?", "Yes", "No")) {
return;
}
if(await _viewModel.SaveClaimErrorsOrNotAsync()) { //The return value is whether validation succeeds or not, but it gets saved either way
App.SuccessToastConfig.Message = "Work saved successfully. Try saving it yourself next time!";
UserDialogs.Instance.Toast(App.SuccessToastConfig);
} else if(await Application.Current.MainPage.DisplayAlert("ERROR", "Work saved successfully but errors were detected. Tap the button to go back to your work.", "To Work Entry", "OK")) {
await Task.Delay(200); //BUG: On Android, the alert above could still be displayed when the page below is pushed, which prevents the page from displaying //BUG: On iOS 10+ currently the alerts are not fully removed from the view hierarchy when execution returns (a fix is in the works)
await Application.Current.MainPage.Navigation.PushAsync(new WorkPage(_viewModel.SavedWork));
}
#endregion
}
What you ask for is not possible. The back button tap cannot be canceled on iOS even in native apps. You can do some other tricks like having a custom 'back' button, but in general you shouldn't do that - you should instead have a modal dialog with the Done and Cancel buttons (or something similar).
If you use xamarin forms that code it is work.
CrossPlatform source
public class CoolContentPage : ContentPage
{
public Action CustomBackButtonAction { get; set; }
public static readonly BindableProperty EnableBackButtonOverrideProperty =
BindableProperty.Create(nameof(EnableBackButtonOverride), typeof(bool), typeof(CoolContentPage), false);
public bool EnableBackButtonOverride{
get { return (bool)GetValue(EnableBackButtonOverrideProperty); }
set { SetValue(EnableBackButtonOverrideProperty, value); }
}
}
}
Android source
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (item.ItemId == 16908332)
{
var currentpage = (CoolContentPage)
Xamarin.Forms.Application.
Current.MainPage.Navigation.
NavigationStack.LastOrDefault();
if (currentpage?.CustomBackButtonAction != null)
{
currentpage?.CustomBackButtonAction.Invoke();
return false;
}
return base.OnOptionsItemSelected(item);
}
else
{
return base.OnOptionsItemSelected(item);
}
}
public override void OnBackPressed()
{
var currentpage = (CoolContentPage)
Xamarin.Forms.Application.
Current.MainPage.Navigation.
NavigationStack.LastOrDefault();
if (currentpage?.CustomBackButtonAction != null)
{
currentpage?.CustomBackButtonAction.Invoke();
}
else
{
base.OnBackPressed();
}
}
iOS source
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
if (((CoolContentPage)Element).EnableBackButtonOverride)
{
SetCustomBackButton();
}
}
private void SetCustomBackButton()
{
var backBtnImage = UIImage.FromBundle("iosbackarrow.png");
backBtnImage = backBtnImage.ImageWithRenderingMode
(UIImageRenderingMode.AlwaysTemplate);
var backBtn = new UIButton(UIButtonType.Custom)
{
HorizontalAlignment =
UIControlContentHorizontalAlignment.Left,
TitleEdgeInsets =
new UIEdgeInsets(11.5f, 15f, 10f, 0f),
ImageEdgeInsets =
new UIEdgeInsets(1f, 8f, 0f, 0f)
};
backBtn.SetTitle("Back", UIControlState.Normal);
backBtn.SetTitleColor(UIColor.White, UIControlState.Normal);
backBtn.SetTitleColor(UIColor.LightGray, UIControlState.Highlighted);
backBtn.Font = UIFont.FromName("HelveticaNeue", (nfloat)17);
backBtn.SetImage(backBtnImage, UIControlState.Normal);
backBtn.SizeToFit();
backBtn.TouchDown += (sender, e) =>
{
// Whatever your custom back button click handling
if(((CoolContentPage)Element)?.
CustomBackButtonAction != null)
{
((CoolContentPage)Element)?.
CustomBackButtonAction.Invoke();
}
};
backBtn.Frame = new CGRect(
0,
0,
UIScreen.MainScreen.Bounds.Width / 4,
NavigationController.NavigationBar.Frame.Height);
var btnContainer = new UIView(
new CGRect(0, 0,
backBtn.Frame.Width, backBtn.Frame.Height));
btnContainer.AddSubview(backBtn);
var fixedSpace =
new UIBarButtonItem(UIBarButtonSystemItem.FixedSpace)
{
Width = -16f
};
var backButtonItem = new UIBarButtonItem("",
UIBarButtonItemStyle.Plain, null)
{
CustomView = backBtn
};
NavigationController.TopViewController.NavigationItem.LeftBarButtonItems = new[] { fixedSpace, backButtonItem };
}
using in xamarin forms
public Page2()
{
InitializeComponent();
if (EnableBackButtonOverride)
{
this.CustomBackButtonAction = async () =>
{
var result = await this.DisplayAlert(null, "Go back?" Yes go back", "Nope");
if (result)
{
await Navigation.PopAsync(true);
}
};
}
}

Ripple Effect gone after adding TapGestureRecognizer to ViewCell

I added a custom LongPressGestureRecognizer to the ViewCell's root layout to handle certain cases, but after adding it, I find that the ripple effect when tapping the ViewCell is gone on Android. I tried to add back the animation by getting the native view, set background drawable to Android.Resource.Attribute.SelectableItemBackground by using below code
int[] attrs = { Android.Resource.Attribute.SelectableItemBackground };
var ta = CrossCurrentActivity.Current.Activity.ObtainStyledAttributes(attrs);
var drawable = ta.GetDrawable(0);
nativeView.SetBackgroundDrawable(drawable);
ta.Recycle();
Even this doesn't work. Any other way to make it work?
For those who want to know, I discarded the custom long press gesture recognizer way of achieving the goal, since it's the wrong way of doing things. On Android, we should use ItemLongClick event instead. Here is what I did, first, find out the native ListView through some method, my way is to first get the renderer of the ListView, then get underlying ListView. Another way is to use below code to find the ListView, but this way requires more work if you have multiple ListView
public static List<T> FindViews<T>(this ViewGroup viewGroup) where T : View
{
var result = new List<T>();
var count = viewGroup.ChildCount;
for (int i = 0; i < count; i++)
{
var child = viewGroup.GetChildAt(i);
var item = child as T;
if (item != null)
{
result.Add(item);
}
else if (child is ViewGroup)
{
var innerResult = FindViews<T>(child as ViewGroup);
if (innerResult != null)
{
result.AddRange(innerResult);
}
}
}
return result;
}
var rootView =(ViewGroup)CurrentActivity.Window.DecorView.RootView
var nativeListView = rootView.FindView<Android.Widget.ListView>();
Then override the OnAppearing method of the Page, in it, attach ItemLongClick event handler. Also override OnDisappearing method, in it, detach the ItemLongClick event handler. This is important. Simply add ItemLongClick event handler in constructor seems not working.

Appcelerator - Android Picker not retaining selected value

Consider the following code below:
function doClick(e) {
win.open();
}
var picker = null;
var backBtn = null;
var selectedIdx = 0;
$.index.open();
var win = Titanium.UI.createWindow({
exitOnClose : false,
navBarHidden : true
});
win.addEventListener("open", function() {
if(null != picker) {
//picker.setSelectedRow(0, selectedIdx, false);
return;
}
picker = Titanium.UI.createPicker({
width : "75%",
height : "50dip",
selectionIndicator : true
});
picker.addEventListener("change", function(id) {
selectedIdx = id.rowIndex;
});
var arr = [];
for(var i = 0; i < 10; i++) {
arr.push(Titanium.UI.createPickerRow({
title : i,
color : "black"
}));
}
selectedIdx = 0;
picker.add(arr);
backBtn = Titanium.UI.createButton({
bottom : "10dip",
title : "Close"
});
backBtn.addEventListener("click", function() {
win.close();
});
win.add(picker);
win.add(backBtn);
});
If the above code is executed with SDK 5.2.0.GA or 5.2.2.GA, then you will see that the picker value is not being retained. After ever open/close of the window, even if we reuse the old picker object. But, if you uncomment the setSelectedRow (hack code I would say), then it behaves as required.
Anyone faced such issue or is aware of a proper solution or root cause for it.
I think this is expected behavior. The picker is attached to a window and on Android starting with Release 3.2.0, all the windows are heavyweight. A heavyweight window is associated with a new Android Activity. So when the window is closed the activity is closed and so will be the picker. It that case the picker is not supposed retain its selected value. You can try using lightweight window in this case.
Prior to Release Titanium 3.2.0 in Android, Titanium windows can be heavyweight or lightweight:
A heavyweight window is associated with a new Android Activity.
A lightweight window is a fullscreen view, and runs in the current Android Activity.
If you still want the old behavior, you can enable the ti.android.useLegacyWindow property in the tiapp.xml:
<property name="ti.android.useLegacyWindow" type="bool">true</property>
Documentation Link
I use Ti.UI.Picker for date/times. And in that case you need to set the value property to tell the picker what value is selected. I guess you need the same with other types of picker, like:
picker = Titanium.UI.createPicker({
width : "75%",
height : "50dip",
selectionIndicator : true,
value : '0'
});
/John

How to hide a TabBarItem in Xamarin iOS

I have a Storyboard with a TabBarController and six subviews as Tabs. The TabBarController and each Tab has a Controller Class assigned.
The content of some tabs is loaded from a server. In some cases there is no content for a tab and for this case I want to hide the TabBarItem.
I've tried (just for testing)
this.TabBarController.TabBar.Items [0].Enabled = false;
but it doesn't work.
How can I hide a single TabBarItems? I've searched Google, StackOverflow and the Xamarin Forum but found no solution for my problem.
The only solution I've found was to remove the subview from the subviews-array, but in this case I cannot simply "reactivate" the tab if I want to show the tab again.
In case of UITabBar:
UITabBar sampleTabBar;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
sampleTabBar = new UITabBar ();
sampleTabBar.Frame = new CoreGraphics.CGRect (10f, 64f, this.View.Frame.Width - 10, 50f);
var tabBarItem = new UITabBarItem ("TAB BAR ITEM", null, 4);
sampleTabBar.Items = new UITabBarItem[]{ tabBarItem };
sampleTabBar.ItemSelected += (sender, e) => {
Console.WriteLine ("tab bar button item slected");
//Disable Tab Bar item here
tabBarItem.Enabled = false;
tabBarItem.Title = "Disabled now";
};
this.View.AddSubview (sampleTabBar);
}
tabBarItem.Image property also can be used for getting the result.
1) Set a placeholder image as default.
2) Populate images from array/source to uibarButtonItem.
3) Use placeholder image wherever need to hide uibarbuttonitem.
I think if you want to completely hide the UITabbarItem you will need to remove the UIViewController from the UITabbarController like so:
var tbViewControllers = new List<UIViewController> (TabBarController.ViewControllers);
tbViewControllers.RemoveAt (2); // remove whatever index you need.
TabBarController.ViewControllers = tbViewControllers.ToArray ();
but you will need to keep a reference to all the UIViewControllers you want and add it back in like so:
var tbViewControllers = new List<UIViewController> (TabBarController.ViewControllers);
tbViewControllers.Insert (2, new RemvoedViewController());
TabBarController.ViewControllers = tbViewControllers.ToArray ();

Handling bottom navigation bar on Windows 10 Mobile

In my Xamarin.Forms app I have a button at the bottom. This button is hidden on a Windows 10 Mobile phone. Isn't there a setting were the size of my page is adapting to the available size? In such a case the height of my page would decrease if the navigation bar is shown, and it would increase if the navigation bar is hidden.
I saw solutions suggesting to programmaticaly hidding the navigation bar. E.g.
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
Where should it be placed? I put it in the App.xaml.cs before rootFrame.Navigate in OnLaunched. If I run the app on my local machine it changed to fullscreen. On the mobile phone the navigation bar was hidden, but there remained a white area at the bottom.
Furthermore I tried
ApplicationView.GetForCurrentView().FullScreenSystemOverlayMode = FullScreenSystemOverlayMode.Minimal;
but I don't see something different.
How should a developer handle the navigation bar without hiding the content beneath it?
This seems to fix the issue:
ApplicationView.GetForCurrentView().SuppressSystemOverlays = true;
The result is that the navigation bar isn't shown when starting the app. The user has the possibility to show the navigation bar with a swipe up. Here the page automatically resizes as desired.
I put it in the App.xaml.cs in the OnLaunched method:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
Xamarin.Forms.Forms.Init(e);
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
ApplicationView.GetForCurrentView().SuppressSystemOverlays = true;
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// Ensure the current window is active
Window.Current.Activate();
}
You can also use:
ApplicationView.GetForCurrentView().SetDesiredBoundsMode(ApplicationViewBoundsMode.UseVisible);
In the App.xaml.cs file for UWP in the onLauched function like this:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
this.DebugSettings.EnableFrameRateCounter = true;
}
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
Xamarin.Forms.Forms.Init(e);
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
ApplicationView.GetForCurrentView().SetDesiredBoundsMode(ApplicationViewBoundsMode.UseVisible);
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// Ensure the current window is active
Window.Current.Activate();
}
This will make sure your app stays inside the visible area, thus with no overlay from the bottom navigation bar.

Resources