Handling hardware Backbutton after a Button inside a Menu opens a Modal - ionic-framework

I have an app with a Sidemenu.
On this menu there's a div tappable that opens a Modal which serves me as a pseudo-select box
let modal = this.modalCtrl.create('SelectPage');
modal.onDidDismiss(option => {
this.updateSelection(option);
});
modal.present();
The problem is: If the user tap the Backbutton It doesn't (instantly) close the Modal. First it closes the Sidemenu (behind the modal) then If I tap again it closes de Modal.
I thought it should close the Modal first... Any help?

You could override the functionality of the Android back button. This can be done using this.platform.registerBackButtonAction but when you do so you need to add all the functionality of it yourself. This includes the closing of overlay portals (modals, toasts, alerts), popping pages off the nav stack, closing the side menu, closing the app, and going to previous tabs. I have included all of these except for previous tabs. If you want to attempt to do that see https://hackernoon.com/handling-android-back-button-in-ionic-33f7cfbba4b9
Another resource for getting the modals to close was https://github.com/ionic-team/ionic/issues/6982
// app.component.ts
exitToast: Toast;
lastTimeBackPress = 0;
timePeriodToExit = 2000;
constructor(public platform: Platform,
public toastCtrl: ToastController,
private ionicApp: IonicApp,
private menu: MenuController) {
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
this.platform.registerBackButtonAction(() => {
if (this.exitToast !== undefined) {
this.exitToast.dismiss().then(() => {
this.closeOpenItem();
});
} else {
this.closeOpenItem();
}
});
});
}
closeOpenItem() {
let activePortal = this.ionicApp._loadingPortal.getActive() ||
this.ionicApp._modalPortal.getActive() ||
this.ionicApp._toastPortal.getActive() ||
this.ionicApp._overlayPortal.getActive();
if (activePortal) {
activePortal.dismiss();
activePortal.onDidDismiss(() => {
});
} else if (this.menu.isOpen()) {
this.menu.close();
} else if (this.nav.canGoBack()) {
this.nav.pop();
} else {
if (new Date().getTime() - this.lastTimeBackPress <
this.timePeriodToExit) {
this.platform.exitApp();
} else {
this.exitToast = this.toastCtrl.create({
message: 'Press back button again to exit',
duration: 1000,
position: 'top'
});
this.lastTimeBackPress = new Date().getTime();
this.exitToast.present();
}
}
}

If i get it right the modal has a back button, but it is not the custom backbutton, right?
you can change this button for a custom button with a event (click) and in the modal.ts file use the viewController dismiss, istead of going back in navigation.
import { IonicPage, NavController, NavParams, ViewController } from "ionic-angular";
constructor(
public navCtrl: NavController,
public navParams: NavParams,
public viewCtrl: ViewController) {
}
dismiss() {
this.viewCtrl.dismiss();
}
in the html
<ion-navbar>
<ion-title>Title</ion-title>
<ion-buttons end>
<a (click)="dismiss()"><ion-icon class="fa fa-close fa-2x"></ion-icon></a>
</ion-buttons>
</ion-navbar>

Related

How to trigger a button click for deviceorientation in WebGL

I have a button to open the browser to fullscreen mode. This is the code:
private void ResizeButton_OnClick (UberButton obj)
{
if (!Screen.fullScreen)
Screen.fullScreen = true;
else
Screen.fullScreen = false;
}
How can I implement automatic button pressing when the screen orientation changes
private void Update()
{
if (Input.deviceOrientation == DeviceOrientation.LandscapeLeft)
{
// ...
}
}

Unity Banner is not showing

My Banner Ad does show in test mode but without testmode not.
I used a code template from the unity website.
I also asked the unity ads support but they couldn't find the error.
they send me a video of my application,and in this video the banner ad worked.
Here is my code:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
public class newadsystem : MonoBehaviour
{
// For the purpose of this example, these buttons are for functionality testing:
[SerializeField] Button _loadBannerButton;
[SerializeField] Button _showBannerButton;
[SerializeField] Button _hideBannerButton;
[SerializeField] BannerPosition _bannerPosition = BannerPosition.BOTTOM_CENTER;
[SerializeField] public string _androidAdUnitId = "Banner_Android";
[SerializeField] string _iOsAdUnitId = "Banner_iOS";
public string _adUnitId = "Banner_Android";
void Start()
{
Advertisement.Initialize("4395157",false,true);
// Disable the button until an ad is ready to show:
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
// Set the banner position:
Advertisement.Banner.SetPosition(_bannerPosition);
// Configure the Load Banner button to call the LoadBanner() method when clicked:
_loadBannerButton.onClick.AddListener(LoadBanner);
_loadBannerButton.interactable = true;
}
// Implement a method to call when the Load Banner button is clicked:
public void LoadBanner()
{
// Set up options to notify the SDK of load events:
BannerLoadOptions options = new BannerLoadOptions
{
loadCallback = OnBannerLoaded,
errorCallback = OnBannerError
};
// Load the Ad Unit with banner content:
Advertisement.Initialize("4395157", false, true);
Advertisement.Banner.Load(_adUnitId, options);
Advertisement.Initialize("4395157", false, true);
}
// Implement code to execute when the loadCallback event triggers:
void OnBannerLoaded()
{
Debug.Log("Banner loaded");
// Configure the Show Banner button to call the ShowBannerAd() method when clicked:
_showBannerButton.onClick.AddListener(ShowBannerAd);
// Configure the Hide Banner button to call the HideBannerAd() method when clicked:
_hideBannerButton.onClick.AddListener(HideBannerAd);
// Enable both buttons:
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
}
// Implement code to execute when the load errorCallback event triggers:
void OnBannerError(string message)
{
Debug.Log($"Banner Error: {message}");
// Optionally execute additional code, such as attempting to load another ad.
}
// Implement a method to call when the Show Banner button is clicked:
void ShowBannerAd()
{
// Set up options to notify the SDK of show events:
BannerOptions options = new BannerOptions
{
clickCallback = OnBannerClicked,
hideCallback = OnBannerHidden,
showCallback = OnBannerShown
};
// Show the loaded Banner Ad Unit:
Advertisement.Initialize("4395157", false, true);
Advertisement.Banner.Show(_adUnitId, options);
}
// Implement a method to call when the Hide Banner button is clicked:
void HideBannerAd()
{
// Hide the banner:
Advertisement.Banner.Hide();
}
void OnBannerClicked()
{
Debug.Log("Banner Clicked!");
}
void OnBannerShown()
{
Debug.Log("Banner Shown!");
}
void OnBannerHidden()
{
Debug.Log("Banner Hidden");
}
void OnDestroy()
{
// Clean up the listeners:
_loadBannerButton.onClick.RemoveAllListeners();
_showBannerButton.onClick.RemoveAllListeners();
_hideBannerButton.onClick.RemoveAllListeners();
}
}
I looked into the android device log and there was: "Unity : Banner Error: UnityAds is not initialized."
but i call several times the initialize function.
PS: i am not very good at english.
Have a look at Start (), your code:
Advertisement.Initialize("4395157",false,true);
// Disable the button until an ad is ready to show:
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
And then at OnBannerLoaded ()
// Enable both buttons:
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
But those button are already interactable = true.
Maybe you need to change Start () like this:
Advertisement.Initialize("4395157",false,true);
// Disable the button until an ad is ready to show:
_showBannerButton.interactable = false;
_hideBannerButton.interactable = false;
Update 1
Try to change your code the following way:
void Start()
{
_showBannerButton.interactable = false;
_hideBannerButton.interactable = false;
Advertisement.Initialize("4395157",false,true);
_loadBannerButton.onClick.AddListener(LoadBanner);
_showBannerButton.onClick.AddListener(ShowBannerAd);
_hideBannerButton.onClick.AddListener(HideBannerAd);
_loadBannerButton.interactable = true;
}
public void LoadBanner()
{
BannerLoadOptions options = new BannerLoadOptions
{
loadCallback = OnBannerLoaded,
errorCallback = OnBannerError
};
if (Advertisement.isInitialized) {
Advertisement.Banner.Load(_adUnitId, options);
} else Debug.LogWarning ("Adverisement not initialized! Try again later");
}
void OnBannerLoaded()
{
Debug.Log("Banner loaded");
BannerOptions options = new BannerOptions
{
clickCallback = OnBannerClicked,
hideCallback = OnBannerHidden,
showCallback = OnBannerShown
};
Advertisement.Banner.SetPosition(_bannerPosition);
_showBannerButton.interactable = true;
_hideBannerButton.interactable = true;
}
void ShowBannerAd()
{
Advertisement.Banner.Show(_adUnitId, options);
}

gui text box disappear when click on gui button in unity3d

In my game I am displaying a textbox; I want it to disappear when i click the button. The code I have tried until now is:
private var isclick: boolean=true;
function OnGUI() {
if (isclick==true){
GUI.Label(new Rect(Screen.width/7,Screen.height/7,Screen.width,Screen.height),word);
words=word;
if (GUI.Button(Rect(Screen.width/5,(Screen.height/4)+320,Screen.width/2,Screen.height/12),"remove")){
isclick=false;
}
}
word is text which is displaying on screen. When I click the button the text has to disappear, but it doesn't.
As I can see you're having problem with indent and close bracers..
private var IsClicked : boolean = false;
function OnGUI() {
if(!IsCliked) DrawWordGUI();
}
function DrawWordGUI() {
words = GUI.Label(labelRect, words);
if(GUI.Button(buttonRect, "Remove")) {
IsClicked = true;
}
}

Softkeyboard is shown only on calling method from button click

I am programatically showing the soft keyboard when a layout is loaded on a button click.
I am showing the softkeyboard only when the textfield has focus. It works fine. But when I call the same method at another place in the code(not a button click) then the soft keyboard does not show up. Below is my code. Please point out where I went wrong.
public void showNewView() {
setContentView(R.layout.activity_main);
this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
isRegisterScreen = true;
final EditText text1 = (EditText) findViewById(R.id.label1);
final EditText text2 = (EditText) findViewById(R.id.label2);
final InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
text1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
inputManager.showSoftInput(labelText, InputMethodManager.SHOW_IMPLICIT);
}else{
inputManager.hideSoftInputFromWindow(text1.getWindowToken(), 0);
}
}
});
text2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
inputManager.showSoftInput(text2, InputMethodManager.SHOW_IMPLICIT);
}else{
inputManager.hideSoftInputFromWindow(phoneText.getWindowToken(), 0);
}
}
});
text1.requestFocus();
}

Open hidden view

I have a perspective with two views, at the beginning of the application shows a viewA and hides the viewB. It´s possible the user select an item from the table in viewA and that selection open viewB which at the beginning is hidden, while hiding viewA?
supouse you have a TableViewer, on your View, you do this:
this.yourTableViewer.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
if (selection .isEmpty()) {
MessageHelper.openWarning("Select something");
return;
}
try {
//opens a Editor instead a view
getSite().getPage().openEditor(new UsuarioEditorInput((Usuario) selecao.getFirstElement()), "br.com.germantech.ecf.usuarioEditor");
}
catch (PartInitException e) {
e.printStackTrace();
}
}
});