I want to set an onClick listener for custom view in sketchware - sketchware

{final Button tab3button2 = view findViewById(R.id.button2); View.OnClickListener() { #Override pubilc void onClick (View v)
I have tried searching for answers but all to no avail.

Just copy the code for on click on source code, and you must have a section like bindview or webview to get at the id of widget. Then just enter I'd, make sure it's different then all others on same page.

Here is what you need, I edited your code:
final Button tab3button2 = view findViewById(R.id.button2);
tab3button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View _view) {
here put the blocks that will be executed when the button gets clicked
}
});
it's recommended to add this code in the onCreate event, but if this Button is in a ListView custom view then add it in the onBindCustomView event.

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 override the functionality of Back button when using Navigation Controller in Xamarin.ios?

I'm working on Xamarin.iOS.When i move from one view controller to another a navigation bar is added to the view on which i just moved and a back button appears. On clicking the back button it returns me to the parent view. But i want some different functionality rather than returning to the parent view.
Can anyone help me out!
This can be accomplished by creating a custom button, and then setting that button as the back button for the view controller, such as:
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
UIBarButtonItem backButton = new UIBarButtonItem("title", UIBarButtonItemStyle.Bordered, handleBack);
this.NavigationItem.LeftBarButtonItem = backButton;
}
public void handleBack(object sender, EventArgs e)
{
Console.WriteLine("back!");
}
I hope this helps!

GWT: Event fired multiple times onClick

I need to get an event fired after clicking on a grid cell. It works but fires multiple events.
My Code:
private void gridClickHandler(final boolean cardDeterminer) {
gridClickHandler = new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
int cellIndex = view.getGrid().getCellForEvent(event)
.getCellIndex(); // get clicked cell of grid
if (cardDeterminer)
oasisCardRPC(cellIndex); //rpc based on clicked cell
else
desertCardRPC(cellIndex); //rpc based on clicked cell
}
};
view.getGrid().addClickHandler(gridClickHandler);
}
The method gridClickHandler is called in an onSuccess of a RPC and calls a new RPC using a boolean passed. (it works like this: click on some widget, when success then click on grid. Grid should only fire event, when this some widget was clicked directly before)
I don't know how to create a new ClickHandler only once for the grid and still make its clickHandler only fire events, when needed.
Thanks in advance!
Use a boolean : isClickHandlerAttached
Initially false, first time you add the clickhandler put it on true. Only attach if boolean is false.

GWT Propagate event through AbsolutePanel

I have a CellTable with ClickableCell. When I click on it, a popup opens and show the clicked cell table as exepected. The CellTable is added in a AbsolutePanel(Parent).
For some developpment, an another AbsolutePanel(Over) is located over the CellTable.
How can I propagate the AbsolutePanel(Over) click event on the celltable ?
I had an handler on the AbsolutePanelHandler(Over) and fired the clicked event :
absolutePanelOver.addDomHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
NativeEvent nativeEvent = Document.get().createClickEvent(0, -event.getScreenX(), event.getScreenY(),
event.getClientX(), event.getClientY(), event.isControlKeyDown(), event.isAltKeyDown(),
event.isShiftKeyDown(), event.isMetaKeyDown());
DomEvent.fireNativeEvent(nativeEvent, cellTable);
}
}, ClickEvent.getType());
Unfortunately the popup cell table is not shown. But I know that the cellTable handles the event.
Regards
Maybe I did not understand your question, but the solution seems to be much simpler.
absolutePanelOver.addDomHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
doSomething();
}
}, ClickEvent.getType());
In your CellTable you invoke the same doSomething() method when a cell is clicked. Now it does not matter whether a cell or an absolute panel is clicked - the same method will be executed.

How to load MainPage when goBack(); form SecondPage?

I use NavigationService.Navigate("..."). But I want to load MainPage again when I click the Back button by event goBack();
In your goBack method you can use the NavigationService.GoBack() call to move from SecondPage back to the MainPage. You can view all the Navigation methods available to you on MSDN.
You can override the method OnBackKeyPress, and inside the method write code shown below, it ll navigate to MainPage.xaml
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
If you are using binding do this:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
App.ViewModel.LoadData();
}
And in MainViewModel.cs
public void LoadData()
{
Items.Clear();
this.Items.Add(new ItemViewModel() { LineOne = "runtime one", LineTwo = "Data info 2" });
this.IsDataLoaded = true; }
Like Nigel said, you can use NavigationService.GoBack() however this will only work if you went from the MainPage to the second page.
The only other way to do this is how you have mentioned using NavigationService.Navigate(uri).
Override the navigated to event
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (PhoneApplicationService.Current.State["msg"].ToString() == "from page2")
{
// do something when its comming from page2
}
base.OnNavigatedTo(e);
}
Now that will trigger every time its navigated to but you could send some parameters so you know what to depending on what page it came from.
PhoneApplicationService.Current.State["msg"] = "from page2";
NavigationService.Navigate(new Uri("/MainPage.xaml?msg=", UriKind.Relative));
There are two ways to get back to your MainPage.
(First Method) Override the OnBackKeyPress() and write the following snippet inside it.
NavigationService.GoBack();
Note :
This will move to the MainPage only if the MainPage is the previous page in your Navigation history.
(Second Method) Override the OnBackKeyPress() and write the following snippet inside it.
NavigationService.Navigation(new Url("/MainPage.xaml",UriKind.Relative));
Note :
This will create a New MainPage and navigates to it. So if you navigated from MainPage to SecondPage and if you navigate again to MainPage using the above snippet, you will be having two MainPage in your Navigation history.
So, use any one of the snippet according to your scenario.
if WP7 or WP8 : NavigationService.GoBack()
if WP8.1 : Frame.GoBack();
and you can Override the navigated to event
protected override void
OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
// do nothing }
I don't recommend overriding the OnBackKeyPress method and calling NavigationService.Navigate method within it as it may mess up your page back stack.
I assume your app has more than 2 pages. Otherwise you should just call NavigateService.GoBack() as others have recommended.
Example
Start on MainPage (backstack: empty)
Navigate to SecondPage (backstack: MainPage)
Navigate to ThirdPage (backstack: SecondPage, MainPage)
Click the back button (overridden) and you are taken to MainPage (backstack: ThirdPage, SecondPage, MainPage)
Click the back button and you are taken to the ThirdPage (backstack: SecondPage, MainPage). This is not the expected behaviour as the application should have exited.
Now you're stuck in a back button loop.
Solution
Override OnNavigatingFrom and remove any of the backstack entries you don't want the user to go to. You can either allow the user to press the back button to get back to the MainPage from the ThirdPage or you can call NavigationService.GoBack().
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
// Remove all backstack entries except for the first one
while(NavigationService.BackStack.Count() > 1)
{
NavigationService.RemoveBackEntry();
}
}

Resources