Posts

Facebook login

Earlier for facebook login, session callback was used, now in latest facebook sdk, it is deprecated and LoginManager, AccessToken & CallbackManager are introduced. Sample code snippet is, private CallbackManager mCallbackManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(this.getApplicationContext()); mCallbackManager = CallbackManager.Factory.create(); LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback() { @Override public void onSuccess(LoginResult loginResult) { Log.d("Success", "Login"); } @Override public void onCancel() { Toast.makeText(MainActivity.this, "Login Cancel", Toast.LENGTH_LONG).show(); } @Override public void onError(Face…

Utility method to sort HashMap

A Simple utility method to sort values or keys in a hashMap. Here in the example, I am sorting by values, we can also sort by keys. For that in Compartor, user getkey() instead of getValue() method. Here in the method parameter, boolean value order true means ASC and false means DESC

private static Map sortByComparator(Map unsortMap, final boolean order) { List<Map.Entry<String, Integer>> list = new LinkedList<>(unsortMap.entrySet()); // Sorting the list based on values Collections.sort(list, new Comparator(Map .entry<String, Integer> o1, Map.Entry<String, Integer> o2) { public int compare(Map.Entry o1, Map.Entry o2) { if (order) { return o1.getValue().compareTo(o2.getValue()); } else { return o2.getValue().compareTo(o1.getValue()); } } }); // Maintaining insertion order with the help of LinkedList Map sortedMa…

How to check valid Email address

The code snippet to check whether the entered email address is valid is as follows,

Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ if (emailPattern.matcher(enteredEmailId).matches()) { // do your stuff }
Happy Coding
Team Androidians

Algorithm for Airplane Seat Reservation System

A airplane has N rows and it has 10 seats numbered from A to K as A,B,C,D,E,F,G,H,J,K, such that ABC form one column, DEFG form second column and HJK form third column

A B C      D E F G      H J K

Given a string S and S represents the reserved seats. S is represented as 1A 1B etc. Seats allocated are represented in S as 1A 1E 1K separated by space.

Given number of rows as N and S represents the seats reserved in the above mentioned format, find the max number of ways a family of 3 members can be allocated. Family members should be allocated consecutively. C and D are not consecutive seats, same for G and H. S can be empty also (no seats are reserved).

class Solution {
  public int solution(int N, String S) {
  }
}



Solution for this problem is,


public static int airPlaneSeatReservation(int N, String S) { int seatAllocated = N * 3; if (S.contains(" ")) { String[] splitSeatReservation = S.split(" "); if (splitSeatReservation …

Dismiss or Cancel dialog by swipe gesture

Dismiss dialog by all four swipe gestures as swipe right to left, left to right, top to bottom, bottom to top First, create a inner class in custom dialogFragment class for the the SimpleGestureListener as, class MyGestureDetector extends GestureDetector.SimpleOnGestureListener { private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_THRESHOLD_VELOCITY = 200; public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { // swipe right to left dismiss(); } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { // swipe left to right dismiss(); } else if (e1.ge…

Add Button and onClick on Custom Notification in Android

Add a broadcast receiver with action and pass the Intent with action to PendingIntent The code snippet is as follows, Intent intent = new Intent("action.cancel.notification"); PendingIntent pi = PendingIntent.getBroadCast(this, id, intent, PendingIntent.FLAG_UPDATE_CUREENT); Also add the receiver in manifest <receiver android:name="CancelNotificationReceiver"> <intent-filter> <action android:name="action.cancel.notification"/> </intent-filter> </receiver> After that, to the pictureView add the pendingIntnet as, pictureView.setOnClickPendingIntent(R.id.button, pi); The last part is define the receiver class as, public class CancelNotificationReceiver extends Broadcastreceiver { public void onReceive(Context context, Intent intent) { if ("action.cancel.notification".equalsIgnoreCase(intent.getAction())) { // .. do what ever you want } } }

How to find default sms messaging app

From api level 19 onwards, its just a one line call to get the packageName of default sms application.
Telephony.Sms.getDefaultSmsPackage(context) Before API level 19, there is no concept of Default Sms, so what we can do is get the list of messaging apps.
private Object[] getMessagingAppPackageNames(Context context) { List messagingAppPackgeNameList = new ArrayList<>(); final PackageManager pm = context.getPackageManager(); //get metaData for installed apps List packages = pm.getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo appInfo : packages) { String packageName = appInfo.packageName; if (packageName != null && (packageName.contains("sms") || packageName.contains("mms") || packageName.contains("message") || packageName.contains("SMS") || packageName.contains("MMS") || packageName.contains("Message") || packageName…