Change toolbars according to the condition - xamarin.android

I am working with xamarin android. I have a layout where initially a toolbar with edit will be visible, when user selects edit the current tool bar should be replaced with another toolbar. I have written code as following.
public override bool OnCreateOptionsMenu(IMenu menu)
{
_localMenu = menu;
var toolbar = _isMultiSelect ? Resource.Menu.MultipleFormsDeleteToolBarMenu : Resource.Menu.FormToolbarMenu;
MenuInflater.Inflate(toolbar, menu);
return base.OnCreateOptionsMenu(menu);
}
And when Edit option in first toolbar is selected, Iam calling OnCreateOptionsMenu
else if (item.ItemId == Resource.Id.edit)
{
_isMultiSelect = true;
OnCreateOptionsMenu(_localMenu);
}
Here the problem is,instead of showing second toolbar... the Edit option in first toolbar is replaced by three dots and while tapping on those three dots Edit and Delete options are dislaying.
How can I change those toolbars acconding to the requirements?

Related

Xamarin.Android, top bar, how add shortcut button ON/OFF

enter image description hereI can't find way to add button with icon to top screen menu. I don't even know name of this bar. I mean swiping down of top screen menu however your are, there are buttons like wifi, bluetooth, screen etc.(picture attached). Maybe someone know name of this bar, link to sdk or how add this button or something connected to it.
It is the Tile Service for the quick settings in the Android, you can check the official document about this.
In addition, I have done a sample to test this api and it worked well.
Just declare a class to extend the TileService, such as:
[Service(Permission = "android.permission.BIND_QUICK_SETTINGS_TILE", Icon = "#drawable/icon", Label = "mymenu", Enabled = true, Exported = true)]
[IntentFilter(new[] { "android.service.quicksettings.action.QS_TILE" })]
public class DemoTitle : TileService
{
public override void OnClick()
{
refresh();
}
public void refresh()
{
var state = QsTile.State;
state = (state == TileState.Active)?TileState.Inactive:TileState.Active;
QsTile.State = state;
QsTile.UpdateTile();
}
}
After this, you can find and add the your custom button to the quick settings when you click the editing button in the bar.

UI testing a tab bar controller

I have built a simple tab bar with 3 tabs.
I want to run a UI test to make sure that if the user clicks a tab bar item, the correct view controller shows. How would I go about doing that? Below is the code I would start with, just don't know how to write my assertion.
func testTabBarMyProfileButton() {
let tabBarsQuery = XCUIApplication().tabBars
tabBarsQuery.buttons["My Profile"].tap()
}
func testTabBarGraphsButton() {
let tabBarsQuery = XCUIApplication().tabBars
tabBarsQuery.buttons["Graphs"].tap()
}
func testTabBarAboutButton() {
let tabBarsQuery = XCUIApplication().tabBars
tabBarsQuery.buttons["About"].tap()
}
You can access the tabbar button by its position:
app.tabBars.buttons.element(boundBy: 2).tap()
If you have different controls in each view controller shown on each tab bar, you can make assertions if they exist or not (what is expected).
For example if the first tab bar has UILabel named "First name" you can assert if it exists by writing
Let theLabel = app.staticTexts["myValue"]
XCTAssert(theLabel.exists).to(beTrue)
And on the other screens do the same thing for the different controls.
If anyone finds this looking to UI test the contents of another app, I just found a solution..
The tab bar item is a lazy variable and needs to be touched before you can reference a tab bar button by value. Add this line:
tabBarItem.accessibilityIdentifier = "my-snazzy-identifier"
to the viewDidLoad method and you should be able to do this in your UI tests:
app.tabBars.buttons["Button Title"].tap()
You can test the title of the navigation bar.
XCTAssert(app.navigationBars["Graphs"].exists)
See my GitHub repo for a more detailed UI Testing example.

How do I set the title for the back button on a Nav Bar in Xamarin.Forms?

I have to remove the title on the back button in several pages. I tried with this code and it works fine.
private void OnBtn ()
{
var nav = (Page)ViewFactory.CreatePage<DebugViewModel, DebugPage> ();
NavigationPage.SetHasNavigationBar (nav, true);
NavigationPage.SetHasBackButton (nav, true);
NavigationPage.SetBackButtonTitle (nav, "");
((MasterDetailPage)App.Current.MainPage).Detail.Navigation.PushAsync (nav);
}
But in some other place of the code I have this and it doesn't set the title to an empty string.
this.SettingsPageCommand = new Command (() => {
this.IsBusy = true;
NavigationPage.SetHasNavigationBar (settingsPage, true);
NavigationPage.SetHasBackButton (settingsPage, true);
NavigationPage.SetBackButtonTitle(settingsPage, "");
((MasterDetailPage)App.Current.MainPage).Detail.Navigation.PushAsync (settingsPage);
//((MasterDetailPage)App.Current.MainPage).IsPresented = false;
this.IsBusy = false;
}, () => !this.IsBusy);
I'm trying to avoid making a renderer as I want to keep as much shared code as possible.
Any ideas?
Thanks!
#hrishi's answer is correct, however if you wish to have different text shown in the Title Bar (this.Title) than shown for the Back button text on subsequent pages you can alternatively call the following method. You would call this in the constructor for the page whose title should be different on the back button:
NavigationPage.SetBackButtonTitle(this, "CustomText");
Typically I only set Title unless it needs to be different when shown for back button (ex: shorter text) in which case I set both Title and call the above method with the alternative back button text.
Title property of content page is used on next page as title of back button.
Try Setting this.Title="" on previous page of settings page

Disable user interaction on tabBar

I am displaying an image right after the app didFinishLaunchingWithOptions, the app consists in a tab bar, and in the first view i have some buttons.
The user can only continue to use the app after he press the button in that first image, the problem is, some users can interact with the tab bar, and the buttons in the first view even with the image above all.
How i can completely disable the user interaction on those buttons and in the tabBar, and enable then only when the button is pressed and the image disapear?
To disable:
UITabBarController.tabBar.userInteractionEnabled = NO;
To enable:
UITabBarController.tabBar.userInteractionEnabled = YES;
if let items = self.tabBarController?.tabBar.items {
for i in 0 ..< items.count {
let itemToDisable = items[i]
itemToDisable.isEnabled = false
}
}
somehow i didn't fully understand what you described,but have you tried disabling your buttons using following code?
button1.Enabled=false;
also you can disable images click function using
image1.Enabled=false;

Accessing the Selected property on TitleBar tabs

I am using the Application Layout control and have tabs in the TitleBar. I want to change the style of the tab if it is selected. I am currently doing it by comparing the value of the tab to a sessionScope variable I am setting when the tab is clicked.
I saw something (though I can't find it now) about using the Selected property of the Basic Node I am using for the tab. How would I access that in SSJS so that I can do something like this?
if(thisnode.selected) {
return "lotusTabs liActive";
} else {
return "lotusTabs li";
}
Thanks.
You can also access the tabs programmatically:
var layout = getComponent("layoutId");
var selectedTab = null;
var tabs = layout.getConfiguration().getTitleBarTabs();
for (var tab in tabs) {
if (tab.getSelected()) {
selectedTab = tab;
}
}
The following CSS rule will target the selected title tab:
div.lotusTitleBar ul.lotusTabs li.lotusSelected {
// your code here
}

Resources