Navigation back between two fragment xamarin android - xamarin

MainActivity
ActionBar.SetHomeAsUpIndicator (Resource.Drawable.ic_menu_white_24dp);
ActionBar.SetDisplayHomeAsUpEnabled (true);
drawerLayout = FindViewById<DrawerLayout> (Resource.Id.drawer_layout);
navigationView = FindViewById<NavigationView> (Resource.Id.nav_view);
Event click on navigatonView
CreateFragments();
LoadInditialFragment ();
navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;
Onefragment
Twofragment
Navigation back from second Fragment to first fragment(navigation back on toolbar).??? MainActivity I have a button menu on toolbar.
I want to replace menu icon into back icon when It is standing at second fragment.

Download back arrow and save as navBack.png
Then in SwitchFragment method where we navigate between fragments
switch (FragmentId) {
case Resource.Id.nav_home:
ActionBar.SetHomeAsUpIndicator (Resource.Drawable.ic_menu_white_24dp);
transaction.Show (homeFragment);
transaction.Commit ();
break;
case Resource.Id.nav_genre:
ActionBar.SetHomeAsUpIndicator (Resource.Drawable.navBack);
transaction.Show (genresFragment);
transaction.Commit ();
break;
}

In Your MainActivity do this in OnCreate:
SupportFragmentManager.BackStackChanged += HandleBackstackChanged;
Implement the HandleBackstackChanged method like this
private void HandleBackstackChanged(object sender, EventArgs e)
{
ActionBar.SetDisplayHomeAsUpEnabled (SupportFragmentManager.BackStackEntryCount > 0);
}
What this will do is toggle between the Menu button and the Back button based on the BackStack count of the FragmentManager.
NOTE:
I am assuming you are using the Support library for backwards compatibility of Fragments. If you aren't using the support library, just use FragmentManger instead of SupportFragmentManager.

Related

Detect Back Arrow Press Of The NavigationPage in Xamarin Forms

Is there any way to detect the press of the back button of the Navigation Page in Xamarin forms?
You can override your navigation page "OnBackButtonPressed" method:
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async () =>
{
if (await DisplayAlert("Exit?", "Are you sure you want to exit from this page?", "Yes", "No"))
{
base.OnBackButtonPressed();
await App.Navigation.PopAsync();
}
});
return true;
}
If you are using the shell, you can override the Shell's OnNavigating event:
void OnNavigating(object sender, ShellNavigatingEventArgs e)
{
// Cancel back navigation if data is unsaved
if (e.Source == ShellNavigationSource.Pop && !dataSaved)
{
e.Cancel();
}
}
Update:
OnBackButtonPressed event will get fired ONLY on Android when user press the Hardware back button.
Seems like you are more interested to implement when any page get disappeared you want to do something!
In that case:
You have the page's two methods -
protected override void OnAppearing()
{
base.OnAppearing();
Console.WriteLine("Hey, Im coming to your screen");
}
protected override void OnDisappearing()
{
base.OnDisappearing();
Console.WriteLine("Hey, Im going from your screen");
}
You can override those 2 methods on any page to track when they appear and disappear.
Recent updates to Xamarin forms mean you can now do this in an application made with Shell Navigation for navigation back arrow on both platforms.
Use the Shell.SetBackButtonBehavior method, for example running this code in the constructor of your page object will allow the back navigation to take place only when the bound viewmodel is not busy:
Shell.SetBackButtonBehavior(this, new BackButtonBehavior
{
Command = new Command(async() =>
{
if (ViewModel.IsNotBusy)
{
await Shell.Current.Navigation.PopAsync();
}
})
});
In the body of the Command you can do whatever you need to do when you are intercepting the click of the back button.
Note that this will affect only the navigation back button, not the Android hardware back button - that will need handling separately as per the answers above. You could write a shared method called from both the back button pressed override and the command on shell back button behaviour places to share the logic.
You must override native navigationbar button behavior with custom renderer. OnBackButtonPressed triggers only physical device button. You can read good article how to achive this here

How to create a custom WebView Control Xamarin forms

I am trying to create a custom control for webview, i am trying to get a checkbox inside a webview Reason :- we have a bunch of text to be displayed and unless the user reaches the end of the scroll he cannot move to the next page and at the end of the scroll there is a checkbox where user has to check the the checkbox and then he can process. here i have tried putting the checkbox and webview inside the stacklayout but the issue is webview have its own scroll bar and and stacklayout scroll bar does not work when a user try to scroll as the webview scroller scrolls out also when i try to close the Webview Page with back button the webview gets close and not the page
i am not sure what approach should i apply here.
i am getting my html data from my webapi.
anyone with some solution would be appreciable
here is my custom renderer which i have wrote but the piece missing here is how can i add another xamarin control inside this
public class CustomPdfViewRenderer : WebViewRenderer
{
public CustomPdfViewRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.Settings.BuiltInZoomControls = true;
Control.Settings.DisplayZoomControls = false;
Control.Settings.LoadWithOverviewMode = true;
Control.Settings.UseWideViewPort = true;
}
}
}
You can try the following approach:
Add checkbox to HTML
When the user check checkbox call some JavaScript function
When JavaScript function is called, call C# function (Xamarin) which will enable the user to process to the next page (or some other Xamarin side stuff)
Here is how you can call C# function from JavaScript :HybridWebView

Xaml Button is "clicked" if it's pressed-and-hold on a touch screen

I am developing a Windows UWP App with C++. I want to add a button that can either be left-clicked or right-clicked and do corresponding task.
The button works perfectly on normal devices with mouse. Unfortunately on touch screen, if users press-and-hold the button, both left-click and right-click events are triggered.
I am using "Click" to handle the left click event and "RightTapped" to handle the right click event. Any suggestion on how to disable the left-click event when users press-and-hold the button? Thank you!
The XAML code is short:
<Button x:Name="m_NewPageButton"
Content="New Page"
Height="48"
Width="199"
Click="OnClick"
RightTapped="OnRightClick"/>
And here's the cpp code:
void MainPage::OnClick()
{
// Left click task
}
void MainPage::OnRightClick()
{
// Right click task
}
Edit:
Thanks guys, the solution is to change the left click handler to , which will not be triggered by press-and-hold.
Hacky but works...
private bool rightClicked;
private async void Button_Click(object sender, RoutedEventArgs e)
{
if (rightClicked)
return;
await new MessageDialog("left").ShowAsync();
}
private async void Button_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
rightClicked = true;
await new MessageDialog("right").ShowAsync();
rightClicked = false;
}
I'm guessing this is a flaw in the design.. :(
Actually... If you use the button's Tapped and RightTapped events it works as it should. So in this case just replace Clicked with Tapped and all is well... Then you can ignore the hack I put above.
You can use the Holding event to detect a tap and hold. Use the Tapped event for a single tap (or click). The Tapped event should only fire for touch on a quick tap and release. The mouse specific click will raise Tapped but a touch tap-and-hold will not.

Can we override NAVIGATION BACK BUTTON press in Xamarin.forms?

Can we override navigation back button pressed in Xamarin.forms?
I have one navigation back button and the one save button in navigation bar.Save button hits the web service and saves in asynchronous way. While saving although i used progressing bar, navigation back button can be pressed and hence the app crashes due to index out of range exception on navigation stack.I tried using OnDisappearing() , did not work. I wanna cancel the PopUpAsync(),if the save is not done completely, but failed to achieve that. Is there any solution for this scenario? Can we override the navigation back button press event using any custom renderer ?
For controlling the back button to do what I want, I used this method in Xamarin:
public override bool OnKeyDown(Keycode HWkeyCode, KeyEvent e)
{
if (HWkeyCode == Keycode.Back)
{
StartActivity(typeof(FrontPageActivity));
return true;
}
return false;
}

Sticky button in GWT

can i create a button in gwt that onClick stays pressed and if it looks pressed onClick releases it?
and the button has a different styles in each state?
any idea?
That sounds like a ToggleButton.
This is a pretty basic approach that should give you the toggle that you want. Basically the first click will put a 'clicked style' that you've created that will give the thing the look you want for when it looks pressed. Clicking it a second time will revert back to your normal button style so it will look not pressed again.
final Button button = new Button();
button.setStyleName("NormalButtonStyle");
button.addClickHandler( new ClickHandler() {
private boolean clickedStyle = false;
public void onClick( final ClickEvent clickEvent ){
clickedStyle = !clickedStyle;
if( clickedStyle ){
button.setStyleName("ClickedButtonStyle");
}
else {
button.setStyleName("NormalButtonStyle");
}
}
});

Resources