Here is how you can add a simple login dialog to your Android app in two simple steps.
1. The “Show login dialog” function that creates and displays the actual login with AlertDialog (called on button/menu select?):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | private void showLoginDialog() { LayoutInflater li = LayoutInflater.from(this); View prompt = li.inflate(R.layout.login_dialog, null); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setView(prompt); final EditText user = (EditText) prompt.findViewById(R.id.login_name); final EditText pass = (EditText) prompt.findViewById(R.id.login_password); //user.setText(Login_USER); //login_USER and PASS are loaded from previous session (optional) //pass.setText(Login_PASS); alertDialogBuilder.setTitle("My Site LOGIN"); alertDialogBuilder.setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { if (!Utils.hasConnectivity(MainActivity.this,false)) //check for internet connectivity { Toast.makeText(MainActivity.this,"No internet access... please connect.", Toast.LENGTH_LONG).show(); showLoginDialog(); return; } String password = pass.getText().toString(); String username = user.getText().toString(); try { if ( username.length()<2 || password.length()<2) { Toast.makeText(MainActivity.this,"Invalid username or password", Toast.LENGTH_LONG).show(); showLoginDialog(); } else { password=MCrypt3DES.computeSHA1Hash(password); //password is hashed SHA1 //TODO here any local checks if password or user is valid //this will do the actual check with my back-end server for valid user/pass and callback with the response //new CheckLoginAsync(MainActivity.this,username,password).execute("",""); } }catch(Exception e) { Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show(); } } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); alertDialogBuilder.show(); if (Login_USER.length()>1) //if we have the username saved then focus on password field, be user friendly :-) pass.requestFocus(); } |
2. Add the login dialog layout xml that looks like this (login_dialog.xml file in res->Layout):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?xml version='1.0' encoding='utf-8'?> <LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='fill_parent' android:layout_height='fill_parent' android:orientation='vertical' android:padding='10sp' > <EditText android:id='@+id/login_name' android:layout_width='fill_parent' android:layout_height='wrap_content' android:hint='Username' android:singleLine='true' > <requestFocus /> </EditText> <EditText android:id='@+id/login_password' android:layout_width='match_parent' android:layout_height='wrap_content' android:hint='Password' android:ems='10' android:inputType='textPassword' > </EditText> </LinearLayout> |
Here is the internet connection check and the SHA1 digest (just in case you need them):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | /* needs <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET"></uses-permission> */ public static boolean hasConnectivity(Context context,boolean wifiOnly) { try { boolean haveConnectedWifi = false; boolean haveConnectedMobile = false; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfo) { if (ni.getTypeName().equalsIgnoreCase("WIFI")) if (ni.isConnected()) haveConnectedWifi = true; if (ni.getTypeName().equalsIgnoreCase("MOBILE")) if (ni.isConnected()) haveConnectedMobile = true; } if (wifiOnly) return haveConnectedWifi; else return haveConnectedWifi || haveConnectedMobile; } catch(Exception e) { return true; //just in case it fails move on, say yeah! we have Internet connection (hopefully) } } /** * SHA1 digest * @param stringToDigest * @return 40 len string */ public static String computeSHA1Hash(String stringToDigest) { MessageDigest mdSha1 = null; String SHAHash; try { mdSha1 = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e1) { return stringToDigest; } try { mdSha1.update(stringToDigest.getBytes("ASCII")); } catch (UnsupportedEncodingException e) { return stringToDigest; } byte[] data = mdSha1.digest(); SHAHash=bytesToHex(data); return SHAHash; } |
That’s all, give it a try!
You like this post? Be the first of my friends to give me a beer! click here. Thanks!
Excellent my friend, this post save me