Mono Droid Modal Popup - android

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.

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.

android- How to prevent enter key/click coming from barcode scanner

Hello I have an application which I got entries by barcode scanner. I use Zebra TC56 as testing device.
I need to show a warning message to the user and that is why I have a custom dialog box.
Dialog box is being showed when user gets an error. Picture of my dialog box can be seen here :
Below red part is a button and when button is clicked , dialog box will be closed and user will turn the latest screen.
Everything works fine but there is somethin I dont want. When user scans a barcode(enters data) button is trigered and dialog box is closed.
I want dialog box to be closed only by clicking the button(TAMAM) from the screen. But when I scan anything, dialog box is closed.
Here is the code of the dialog box class :
public class ViewDialog {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.customdialog);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
I tried to get focus to somewhere except the button(TAMAM) but it didnt help. If someone knows how to dismiss enter key from the barcode scanner, I really need some help and will be appreciated. (I already set the device to send Enter key because I need in other screens)
Few ways to do this that I can think of.
Firstly, I presume you are using DataWedge to automatically append the enter key to scanned data, you could dynamically switch to a profile which did not send the enter key but was identical in every other way using the SWITCH_PROFILE API: http://techdocs.zebra.com/datawedge/6-3/guide/api/switchtoprofile/. This is presuming that you still need to be able to scan when the dialog is visible, if you wish to disable scanning entirely you could use the SCANNER_INPUT_PLUGIN API: http://techdocs.zebra.com/datawedge/6-3/guide/api/scannerinputplugin/.
Secondly, you could use the EMDK profile API to change the parameters of the KeyStroke output plugin (http://techdocs.zebra.com/emdk-for-android/6-3/mx/data-capture/keystroke/#keystrokeoutput) then apply that newly modified profile. I've never tried that myself but it should work - check out the following sample for the principles behind that: http://techdocs.zebra.com/emdk-for-android/6-3/samples/data-capture/
Thirdly, you could use the Java SDK for the scanner which gives you more control over how the scanner behaves (http://techdocs.zebra.com/emdk-for-android/6-3/api/)

Mvvmcross: launching an android dialog containing a ListView

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.

Android pop up spinning loader

I'm creating an android app where user loads some data from an online server when he hits a button
I need to add the following :
when user hits the button a pop-up screen shows on top of current screen & shows something like
"loading" "spinning loader" or anything like this
any thoughts ?
Have a look at this Android Developer page at the section "Creating a ProgressDialog". Android offers such a dialog out of the box.
You can try a function like showProgressDialog(Activity activity)with this context:
if ((mySpinnerDialog== null) || (!mySpinnerDialog.isShowing())) {
mySpinnerDialog= new Dialog(activity);
mySpinnerDialog.getWindow().getCurrentFocus();
mySpinnerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mySpinnerDialog.setContentView(R.layout.some_layout);
mySpinnerDialog.setCancelable(false);
mySpinnerDialog.setOwnerActivity(activity);
mySpinnerDialog.show();
} else {
mySpinnerDialog.setOwnerActivity(activity);
}
and dismiss this with mySpinnerDialog.dismiss();. Handle Illegalargumentexception on dismiss()

How to accept dialog in unit testing?

I have a unit test that opens up a custom dialog and enters some text. This works, but I am unable to accept the dialog or get hold of the ok button. Please can anyone tell me how to automate the dialog acceptance using junit.
ActivityMonitor activityMonitor = instrumentation.addMonitor(
EditItem.class.getName(), null, false);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
instrumentation.invokeContextMenuAction(gridList, R.id.add, 0);
Activity activity = instrumentation.waitForMonitorWithTimeout(
activityMonitor, 10);
assertNotNull("Make sure the edit item activity was called", activity);
assertEquals("Make sure the edit item activity was called",
EditItem.class, activity.getClass());
final TextView nameView = (TextView) activity.findViewById(R.id.name);
// this opens the dialog
TouchUtils.clickView(this, nameView);
// this adds some text
for (int i = 0; i < 3; i++)
{
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_X);
}
// here I would like to accept the ok button on the dialog
OK I seem to have worked round this with a combination of key presses
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
The problem is now I can't tell when the dialog has finished being dismissed and returned to the parent Activity to continue the test with a
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
Go a work around for this too now:
instrumentation.waitForIdleSync();
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
Seems a bit clunky, is this how you are supposed to use these tools?
I'm having the same problem. What it looks like is that you will have to create a custom dialog. This will allow you to retrieve the buttons you added using the findViewById(). Here is a link that I found that might get you on the right path.
http://www.mkyong.com/android/android-custom-dialog-example/

Categories

Resources