Tag: programming

  • Android RecyclerView notifyItemChanged() IllegalStateException “Cannot call this method while RecyclerView is computing a layout or scrolling”


    If your Android app is crashing with error IllegalStateException “Cannot call this method while RecyclerView is computing a layout or scrolling”, you may try placing your notifyitemchanged inside a Runnable.

    Assuming you have a call to notifyItemChanged() inside onBindViewHolder() that is making your app crash with IllegalStateException, you may try something like this:

  • Setup/update Ruby on OS X El Capitan 10.11.5

    I need to learn Ruby on Rails for my new job. I’m a little thrilled about this chance to learn a new language, and I’m trying to get started with it on my Macbook which runs OS X El Capitan 10.11.5. I already have Ruby here, I don’t remember if pre-installed though. It’s out of date and I wanted to upgrade, as this resource Getting Started with Rails recommends version 2.2.2 or higher.

    First step I took was to get RVM (Ruby Version Manager):
    curl -sSL https://get.rvm.io | bash -s stable --ruby

    Then I received this message:

    ” * To start using RVM you need to run source /Users/catzie/.rvm/scripts/rvm
    in all your open shell windows, in rare cases you need to reopen all shell windows.”

    And so I ran:
    source /Users/catzie/.rvm/scripts/rvm

    Then:
    rvm install 2.4

    Which returned this message:

    Already installed ruby-2.4.0.
    To reinstall use:

    rvm reinstall ruby-2.4.0

    Finally, I ran this:
    rvm use 2.4 --default

    My next step was to install SQLite3 on my OS X as per instructions found here on SQLite – Installation.

    I got this message after following the instructions:

    SQLite version 3.17.0 2017-02-13 16:02:40
    Enter “.help” for usage hints.
    Connected to a transient in-memory database.
    Use “.open FILENAME” to reopen on a persistent database.
    sqlite>

    More on this in the future.

    Reference: Can I upgrade to the current version of Ruby (2.2.3) on OS X v10.6.8?

  • Simple SMS Reader app in Android using BroadcastReceiver class

    Before I began coding Android apps, I was amazed how verification processes go like this: The Android app tells you they’ll send you an SMS containing verification code, and that you need to enter such code into a text field on the Android app. But when you receive the SMS, the verification code appears on the text field before you even type! I was like, whoa, how do they do that?

    Now that I am able to develop simple Android apps, I already have an idea how it’s done.

    We extend a class called BroadcastReceiver and implement its onReceive() method. This receiver is then registered either in the AndroidManifest.xml file, or registered as needed in a .java file (e.g., Activity)

    I’m registering mine in my MainActivity file. I’ll provide the link to my project files on Github later on this page, but for now here’s what it looks like:

    I first created a class called SmsReceiver which extends the BroadcastReceiver class that I mentioned earlier. Inside it, there’s an implemented method onReceive(), where we first check if the Intent’s action is android.provider.Telephony.SMS_RECEIVED, otherwise we do nothing. We read message(s) that the Android devices just received and get the originating address (e.g. phone number of sender) and message body. Then lastly, the TextViews in the XML file are updated with the originating address and message body so you could see that thea app works.

    By the way, I used a Handler in this app so that I could set the values of TextViews inside the MainActivity’s XML file. A new Handler object is passed from MainActivity.onCreate() to the SmsReceiver constructor. And later on once the message originating address and message body are available in BroadcastReceiver.onReceive(), we use the handler to “post” and set the TextView values.

    Now, for the source files of this Android app…

    Feel free to download my Android project files here: Android-Incoming-SMS-Reader

    Resource:
    http://stackoverflow.com/a/14648933

  • Android app dev: Add ActionBar to Preference Activity

    When I converted an activity into PreferenceActivity, I noticed that its ActionBar disappeared. I’ll show you how I brought the ActionBar back to my PreferenceActivity.

    In this guide I will assume that the name of the file that contains your is SettingsActivity.java, and that the view assigned to your SettingsActivity.java is named .

    First, create settings_toolbar.xml under /app/src/main/res/layout/ and place the following code into it:

    Then add the following overridden method to SettingsActivity.java:

    The Java method programmatically creates a Toolbar object from the XML toolbar at settings_toolbar.xml.

    Reference(s): Creating a Preference Screen with support (v21) Toolbar

  • Laravel 5: activate API token guard and print “Unauthorized” instead of login redirect

    In Laravel 5, I wrap my API routes in a route group like this:

    Notice that I use auth:api and throttle on my middleware array.

    auth:api turns on the auth middleware with API token as guard, while throttle activates rate limiter for APIs.

    To get started with API token authentication, you may read https://gistlog.co/JacobBennett/090369fbab0b31130b51.

    I followed the same tutorial to set mine up, but had this issue: Upon using invalid api_token or no token at all, I am redirected to the /login route, which in my case is a currently nonexistent route.

    As instructed by the guide, I already edited my app/Http/Middleware/Authenticate.php file to add the || $request->wantsJson() condition, but my API routes still redirected to /login route upon detection of invalid api_token.

    The solution

    What I did was add another condition: || $guard == "api"

    And now, my app/Http/Middleware/Authenticate.php file looks like this:

  • Android app dev: ShareActionProvider in Activity & Fragment

    I’ve been taking the Developing Android Apps course on Udacity, and yesterday I encountered a difficulty in solving the Quiz: Share Intent.

    One of the course’s goal is to build the Sunshine App – an Android app with weather forecast.

    In that quiz I had to implement ShareActionProvider on my DetailsFragment so that a share icon becomes available on my app’s ActionBar.

    One of the main difficulties was the error that stated ActionProvider cannot be cast into ShareActionProvider. And at some point there was also an error saying that the methods/classes I’m using require API 14, while my minimum supported API is 9. The Udacity course required that I set it to 9 so I couldn’t change that.

    Now, the sharing in my app is working file, and I will share how I did it via Gist. I actually ended up taking a peek at the code provided by Developing Android Apps course on Udacity because I didn’t want to waste any more time doing trial-and-error (I have another project to work on! lol), but I still had to edit their code for ShareActionProvider here and there to make it bug-free.

    And so, take a look at how my Sunshine app codes ended up:

  • Laravel 5 RESTful API with Basic Auth: PHPUnit Testing

    I’ve been using PHPUnit to test my RESTful API. PHPUnit is ready to use in Laravel.

    In my Laravel-based RESTful API, I have an endpoint /api/users which returns JSON data when you send a GET request. The API will only return such data if the client sends the corrent username and password (Basic Authentication). The JSON data looks like this:

    I’m gonna show you how to create a PHPUnit test for this kind of JSON API data in Laravel 5.

    To create a test case, use this command:

    This will create a new test case named ApiUsersTest.php in your directory /tests

    Paste the following code in it.

    Edit the test case as needed.