Dismiss the Android SoftKeyboard Programmatically in a NativeScript App

Be Gone You Damn KeyBoard

If you’re like me you get really frustrated with apps that leave the soft keyboard open when you’ve completed some action that should have closed it. Maybe you completed a form and tapped a ‘Submit’ button, well that keyboard is pointless now so hide the thing please. There are some use cases where you don’t want to but oh well I’m going to tell you how to get rid of it when you want to in your code.


import * as app from 'application';

import { isAndroid } from 'platform';

/**
* Hide the keyboard with a conditional Android platform check.
*/
private hideKeyboard() {

   if (isAndroid) {
      try {
         let activity = app.android.foregroundActivity;
         let Context = app.android.currentContext;
         let inputManager = Context.getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), android.view.inputmethod.InputMethodManager.HIDE_NOT_ALWAYS);
      } catch (err) {
         console.log(err);
      }
}
}

The code is pretty simple and just uses the Android InputMethodManager service and calls the hideSoftInputFromWindow() method. Has worked great in my apps and hopefully it helps you out if you ever need to get rid of the keyboard in your Android NativeScript app.

 

Do NativeScript Plugins Add Weight to My App?

Intro

Something that comes up all the time during app development is the overall app size for the application. Well simple logic, the more you add the larger the app will be. Typically you want your app to remain light weight. In this post I just want to cover plugins and how they will affect your final app size.

I’m assuming you have some familiarity with NativeScript. If not, I encourage you to check it out for mobile app development. It’s in the same boat as React Native and Xamarin, not the same as Cordova based frameworks like PhoneGap or Ionic. At any rate, the NativeScript site can provide much better information about what it is and how it works than I can and it’s outside the scope of this post. So lets dig into a couple plugins and see how they will impact our app size.

We will look at two plugins for NativeScript, one that I built and maintain and another from Nathanael Anderson.

My plugin contains native Android and iOS dependencies, Nathanael’s plugin is just plain javascript and it contains many nice utility methods for accessing UI components in a NativeScript app. So with those two plugins you’re adding something extra to your app so it will simply increase the app size, but exactly how much weight does it add?

Plugin with Native Dependencies

Well there are a few ways to find out but I’m going to show a quick and simple route for both. Lets start with the plugin that is adding native android/iOS dependencies to our application first (color-picker plugin). If you look at the plugin source and find the library listed in the include.gradle located in the platforms/android/ folder here. You’ll find the dependency listed on line 11. You can copy the dependency `com.pavelsikun:vintage-chroma:1.2` and paste it into MethodsCount website and it will give you the stats on the library. Which should give you the following on your screen and tell you approximately how much ‘weight’ will be added to your Android application if you add this plugin (which consumes this native library).

colorpickermethodscount

Now for the iOS side, which is even easier if the library is coming from CocoaPods. This NativeScript plugin uses a library from CocoaPods and you’ll find that listed in the  PodFile here. If you go to the CocoaPods site and search for that library you’ll find it and the information you’re looking for. Here is the page for this library. On the right side bar toward the bottom you’ll find the size of the library. Below is a screenshot.

mscolorpickerpod

That’s it for the native dependencies, now we can look at the plugin that’s just adding some javascript, which in most cases you can expect to be fairly small in added size.

Plugin with javascript only

While we’re on this topic, a good plugin distributed to NPM should only have published the necessary files to NPM. The source files, package.json, and a license file if desired. Images and junk files should not be published to npm and if so, I suggest making a PR to the repo’s .npmignore file so those files aren’t published. This plugin from Nathanael does a good job of not shipping wasted files and the README, LICENSE, and changelog.md files are really small in size so it won’t add much. What I’m most curious about is the actual javascript source file for the code, thanks to GitHub for easily finding that info out for any file. Just open the file on GitHub and in the top you’ll see the file size. Only 11.4 kb.

nsdomgithub

Reducing the Size

There are several ways to reduce the overall footprint of the plugins. You can manually delete files from the node_modules folders that you don’t need. Webpack is another option. If you want a simple hook to delete modules from the build there is a hook plugin for NativeScript from Markosko here.

Notes

Keep in mind there are other ways, you could open your file explorer and find the node_modules folder of your app and see how large the folder is for the plugin, this works only for the plugin itself. If the plugin is adding native libraries compiled into your app, this won’t work the same. You can use Android Studio to find out however, but I’m not going over that here.

Swift Runtime Size

I’ve been told by Nathanael Anderson that if the Pod for iOS uses Swift, it will add 6+ MBs for the Swift runtimes.

Conclusion

This should show you one approach to determine the weight that a plugin could potentially add to your NativeScript application. There are other options to determine the size and several options to reduce the application size. Before going to production you should strongly consider reducing the application size.

Why you should never sell your home to someone with a VA loan.

This is really just me venting and a warning to anyone with real estate in the US who receives or considers accepting a VA loan. If I could do it over again, I wouldn’t have accepted the offer until the appraisal was done and the lender had completed their job. You’ll see why that’s so important if you keep reading.

In early July I decided to sale my home and move closer to schools and work to cut down on my commute time considerably. Things moved pretty fast, we found a home, made an offer, put our home up for sale and within 24 hours were under contract. Everything was great at this point. The offer came in and I was told it’s going to be a VA loan. At first I wasn’t sure why I would even need to know or care about this. Well the fact is, the VA process is a total joke and full of scams and incompetent individuals who cannot do their job. It’s remarkably similar to how most Americans view their cable company, mainly Comcast.

Let’s look at a few things that I was told, I’m going to assume the points made to me were factual.

  1. VA appraisals have 10 business days to be completed.

This one just strikes me as ridiculous, 10 business days to do a simple job that conventional loans typically complete in half that time. What reason do you need in 2016 to require 10 business days? I guess etching stone and sending the reports via Pony Express does take time. Things got even more ridiculous for my home. The appraiser came out on a Thursday and in less than 24 hours my realtor contacted me to inform me that the appraisal was going to be a little lower than the agreed contract price. Which I was okay with that, but no one ever mentioned a number, ever. My realtor and I got together and submitted a rebuttal to get the best appraisal possible on that Friday, all within 24 hours of the appraiser coming out and leaving my home. The weekend came and went, the following week was filled with stress thinking what are we going to do if the appraisal comes back at X amount. I did all the numbers and scenarios to make sure we had it altogether. It literally was a waiting game. So the entire next week came and went, I was livid, it still makes me angry to think it takes anyone more than 5 days to do a simple appraisal. I honestly thought, maybe this guy died and no one knows and I’m sitting here and we won’t know until someone actually contacts him to see what is taking so long. Well we continued waiting, on the 9th business day (13th day) since the appraiser left our home we were finally told it was a measly $5,000.00 less than the agreed sales price. In a minute you’ll see why this appraisal was the beginning of a shit storm for all ages.

 

2. VA loans/lenders require that a seller pay for a termite inspection.

Sure it’s only $50 bucks but it’s another cost that can’t be rolled into closing costs or the buyer can’t pay only cause it’s a VA loan. No one could ever give me a valid reason for why that is the case, I guess things are tight with the government lately.

Conclusion

Okay so I realize I don’t have many points to talk about. The first one is the main reason that ruined my week and has my family sleeping on a mattress in our floor with a U-Haul in the driveway. We were supposed to close on our house today at 9:45am CST. Yesterday at 4:15pm CST, I received word that we aren’t going to be closing because the VA didn’t finish their job on time. The lender needs more time to complete their process was basically the message. What kind of crap is that? They’ve had a month to do everything, and why more time is needed is beyond the imagination. It’s 2016, we literally have rockets flying to the ISS and landing safely back on the Earth. I guess my frustration comes from the fact that I simply don’t understand it. How does it take any company/entity that long to do some paperwork and finish a task. Thankfully my appraisal and lender was done through Regions bank by competent individuals and they were done and ready for closing with two full weeks to spare. Everything I was required and asked to do was completed, so now I have to pay the price because I accepted a VA loan and they can’t complete their job on time.

Now I’m out $50/day for a U-Haul, our clothes are on the front of the truck so I’d have to unload the entire truck to get more clothing. We have a couple items in a suit case for all 5 of us. Then don’t get me going on Comcast customer service, I woke up and the internet was disconnected and it should have been because we had a move date for today. So I’ve called 3 times and spoken to people who can’t understand what I need done. I just need service turned back on at my current address and not the new one for another week. You’d think that wouldn’t be hard but I’ve been transferred numerous times and no one can resolve the issue.

AHHH! This NativeScript plugin doesn’t work

Keeping this short and to keep myself from having to repeat these instructions all the time for developers trying to understand the Android and iOS build process and how you need to  download and compile certain things with apps. Mainly since NativeScript is not running a webview and you can’t drop a javascript file and start working with third party libraries without a build step.

Continue reading “AHHH! This NativeScript plugin doesn’t work”