Mvvmcross: launching an android dialog containing a ListView - android

First, I will like to give a big Kudos to Stuart Lodge for this awesome framework. Together with Xamarin's Visual Studio integration, this is one of the most productive cross platform frameworks I have laid my hands on.
What I want to achieve is launch a dialog containing a selectable ListView when a button is clicked. I need access to the selected item when the user closes this dialog. Is there a recommended way to do this using the Mvvmcross' dialog plugin while following the MVVM paradigm?
I am using the following Activity to create a dialog.
[Activity(Theme = "#android:style/Theme.Holo.Dialog")]
public class SearchResultDialogView : MvxActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.SearchResultView);
}
}
Navigating to SearchResultDialogViewModel from another view model brings up this view as modal. So it looks like I am heading in the right direction. However, the dialog is missing the OK and Cancel buttons and I will also like to get rid of the default header. Think I need an AlertDialog but so far I have had no success launching one with this code:
[Activity(Theme = "#android:style/Theme.NoTitleBar")]
public class SearchResultDialogView : MvxActivity
{
protected override Dialog OnCreateDialog(int id, Bundle args)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle("some title");
return builder.Create();
}
}
Apologies if this question is vague. I am new to Android UI development.
TIA.

There are several different uses of the word dialog here.
Android Dialogs are 'popup displays' and are covered in http://blog.ostebaronen.dk/2013/02/using-dialogs-in-mono-for-android.html
The MvvmCross Dialog plugin is a code-based form-builder forked from the existing MonoDroid.Dialog and MonoTouch.Dialog tools - see https://github.com/migueldeicaza/MonoTouch.Dialog
The Holo Dialog display is (actually I'm not sure) some theme-based skin on a normal Activity.
With these in mind...
If you want to display a general popup window to collect some data, then you can try using a fragment based dialog to collect data - this is demonstrated (with a little code behind) in Fragments HomeView.cs with NameDialogFragment.cs - for general background on fragments, watch N=26 in http://mvvmcross.wordpress.com/
If you want to use a separate activity for data collection, then #gschackles wrote this article on one way of returning data from child viewmodels - http://www.gregshackles.com/2012/11/returning-results-from-view-models-in-mvvmcross/ - I'm sure other schemes could also be used.
If you do want to learn about the Mvx Dialog plugin, see N=23 in http://mvvmcross.wordpress.com/

You can do it with the builder.
http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList
The code is:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color);
.setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}
and you can get your element by returning the which value to your caller.

Related

AlertDialog displays briefly and disappears

I have an android app written with Xamarin which needs to notify from a class that does not have good access to an activity or context, so I am using a System Alert dialog to display the message.
In Android 4.4, the pop-up appears and the user must tap the OK button to clear it. The rest of the screen is dim and the user cannot interact with any other UI elements until the pop-up is cleared. This is the desired behavior.
In Android 5.0 (tested on a Galaxy S6 and a tablet), the pop-up will appear for about one second and then disappear without requiring any interaction whatsoever. I have done a number of Google and SO searches to no avail.
private static void ShowSystemAlert(Context context, string message)
{
var dialog = new AlertDialog.Builder(context, Android.Resource.Style.ThemeHoloLightDialog)
.SetNeutralButton("OK", (alertSender, args) => {})
.SetMessage(message)
.Create();
dialog.SetCanceledOnTouchOutside(false);
dialog.Window.SetType(WindowManagerTypes.SystemAlert);
dialog.Show();
dialog.Window.DecorView.SetBackgroundResource(Android.Resource.Color.Transparent); //remove strange small border around dialog
}
How can I get the pop-up to work like it does in Android 4.4? The best answer will work in Lollipop, Marshmallow, and Nougat.
I would also very much appreciate an explanation as to why this happened, or any background information you can give. Thanks!
Is it xamarin? Create your alertdialog with just the context, don't use the theme.

How to make a alert dialog button left and right instead of both align right without using custom view in android

Here is the general way I build an alert dialog. It is not crash just the outcome doesn't suit my expectation.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
builder.create().show();
Outcome:
But what I want is the cancel and fire button use up all the space of the dialog box rather than both align at the right side.
Just like the cancel and ok button. Ignore the check box what I need is only those button position.
Since I saw a lot of other app such as vine,twitter,foursquare etc display the alert dialog with the button which I desire so is there a way or method I can assign to the alert dialog while create it, I know there is a lot of way which can achieve this but I'm wondering is there a way without to use custom view or edit the LayoutParams of the button?
So recently I been talking to one of twitter developer what he said is every device have difference way to manage their user interface. Even there is a way better to use a custom dialog to make sure that on all device it is presenting the way what you want it to look like.
Final outcome use a custom dialog if you really want it to look like which way you want to.

Android AlertDialog.Builder displays blank unless a theme is supplied

I have recently begun work on an existing android app and noticed that one of the modals displays as a white blank square. I did some research and someone suggested that supplying a theme should fix it. I tried this and it does fix the issue but i don't understand why this was working without it and now its not. The code we use to initialize the AlertDialog looks like this
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setItems(R.array.media_resume_options, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//handle selection logic here
//......
}
});
builder.create().show();
I can fix it by changing the instantiation line to
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_DARK );
I don't see any changes to this part of the code that might have caused it. Could it be affected by something else? whats the potential risk that it might affect other dialogs?
It can't be blank,the textcolor and background color both are white that's why you think it is blank.
How to change default text color of dialogbox is explained here
Change dialog text color on 5.0+

Mono Droid Modal Popup

I am wondering how I go about (if it is possible) creating a modal popup in a mono for droid application.
Scenario: The application talks to the customers hosted web server (so this location will be different customer to customer). To use the app the user must specify the connection string of their web server. So when the application starts and it hits the main activity, the first task I do is check if there is a connection string set in the devices application settings. If not I want to throw up a simple modal popup that allows the user to specify a connection to their server.
I dont really want to start a normal activity because the user will be able to click the back button and just go back to the main menu and the app is than in an invalid state because it doesnt know what server to talk to.
Any ideas on how I go about this?
Or should I be structuring the activity chain so that the connection string is entered on the first activity so that if they click back it actually goes out of the app?
Im a little confused.
Thanks in advance
This is possible with AlertDialog. It can create dialogs for simple input with lists, checkboxes, yes/no buttons and custom views.
There is a sample in the Xamarin Sample Repository for different type of dialogs and in the bottom you can find one where a custom view with a username and password field has been added.
So first define your custom view you want to put in the AlertDialog. alert_dialog_connection_entry.xml and is a Layout:
Somewhere in your activity add the code:
var connection_string_view = LayoutInflater.Inflate(Resource.Layout.alert_dialog_connection_entry, null);
var builder = new AlertDialog.Builder(this);
builder.SetTitle("Connection String");
builder.SetView(connection_string_view);
builder.SetPositiveButton("OK", OkClicked);
builder.SetNegativeButton("Cancel", CancelClicked);
builder.Create();
builder.Show();
Add some handlers for the buttons:
private void CancelClicked(object sender, DialogClickEventArgs dialogClickEventArgs)
{
//Todo
}
private void OkClicked(object sender, DialogClickEventArgs dialogClickEventArgs)
{
var dialog = sender as AlertDialog;
if (null != dialog)
{
var connectionEdit = dialog.FindViewById(Resource.Id.connectionstring_edit) as EditText;
if (null != connectionEdit)
Console.WriteLine("Connection String: {0}", connectionEdit.Text);
}
}
That should be it. You should be able to put any kind of custom view in the dialog.
If you just want to display a modal popup for letting users put their connection string, you could try this.
First, you need to have a simple layout for how the dialog is presented. In this case, a TextView displaying something like "Connection string:" and an EditText to let the user put connection string is probably enough to go.
Then, you can put this code somewhere in your MainActivity, like after checking application settings or something similar.
var builder = new AlertDialog.Builder(this);
var view = LayoutInflater.Inflate(Resource.Layout.ModalDialog, null);
builder.SetView(view);
string connectionString = view.FindViewById<EditText>(Resource.Id.ConnectionString).Text;
AlertDialog alert = builder.Create();
alert.SetCancelable(false); //This prevents the dialog from being dismissed by either hit back button or hit out side of the dialog
alert.SetButton("OK", (s,e)=> ToDo(connectionString)); //Now you have the connection string, to do whatever you want.
alert.Show();
As you said, the alternative could be allowing user specify the connection string in the first screen. This is a good approach too. I assume you know how to do it, so I didn't post code here.

Android - How to implement a listbox as a modal dialog/popup

In my Android app, when a user clicks on a button, I want a list box to show up as a modal dialog, just like the way a Spinner works, except that the list box can also allow multiple choices. I tried using ListView as described in the android hello-listview tutorial (http://developer.android.com/resources/tutorials/views/hello-listview.html). Unfortunately it doesn't seem to work the way I had expected it to. It doesn't show up as a modal dialog like the Spinner. I tried looking at what the Android browser does when a listbox is to be displayed. I browsed to www.functionx.com/html/lesson14.htm in the browser on my Android device and saw the following behavior (and this is exactly the behavior I want in my app):
When a dropdown/combobox in HTML is clicked, a spinner comes up as seen in the image here:
http://img842.imageshack.us/img842/803/htmlcombobox.png
When a single select listbox is clicked, again a spinner comes up as seen here:
http://img13.imageshack.us/img13/3355/listboxsingle.png
And when a multi-select listbox is clicked, a multi-select spinner / listview shows up in a dialog as seen here:
http://img835.imageshack.us/img835/711/listboxmulti.png
So my questions are:
What is this widget (in the last image above) that allows multi-select in a modal dialog. I'm sure this must be a component already available on the Android platform since it's being displayed in the browser.
Even the Spinners (in the first 2 images) in the browser look different than the default Spinner I'm seeing in my app. Would the browser be applying custom skinning / colors to the background and text of the Spinners that it displays?
Here's the code in case anyone's interested:
new AlertDialog.Builder(this)
.setMultiChoiceItems(R.array.select_dialog_items,
new boolean[]{false, true, false, true, false, false, false},
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,
boolean isChecked) {
/* User clicked on a check box do some stuff */
}
})
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Yes so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked No so do some stuff */
}
})
.show();
Check the ApiDemos sample project, the AlertDialogSamples activity. There is a button labeled "Repeat alarm" that invokes a modal dialog with a multiple choice list.
In any case, a good place to start would be AlertDialog.Builder.

Categories

Resources