Category: Technology

  • Android: Basic WebView Tutorial

    A WebView lets you pull a web page and display it in your Android app. It doesn’t take long to implement an Android WebView. Read on. I’ll show you how.

    AndroidManifest.xml

    First, open your manifest file to add this permission for INTERNET:

    The web page won’t be loaded into your app without this permission.

    This is how my manifest file looks like:

    Path: ...\MyAndroidStudioProjects\WebView\app\src\main\AndroidManifest.xml

    activity_main.xml

    Then we’re adding a WebView element to the XML file. My XML file is named activity_main.xml

    Path: ...\MyAndroidStudioProjects\WebView\app\src\main\res\layout\activity_main.xml

    MainActivity.java

    I have MainActivity.java here, which loads activity_main.xml and then loads a web page (google.com) into the WebView.

    Path: ...\MyAndroidStudioProjects\WebView\app\src\main\java\net\catzie\webview\MainActivity.java

    The following line of code attaches the WebView whose id is “webview” to the variable “webView”:

    And this allows JavaScript in the WebView:

    This one is what actually loads a given URl into the WebView:

  • PHP dynamic variable names tutorial

    I think it’s very convenient for us developers, being able to name our variables dynamically. I’ll show you how to do it in PHP. 🙂

    For example, we have variable $foo with “bar” as value like this:

    If we want to create a variable named based on the “value” of variable $foo, this is what we write:

    The echo should display: My variable name is bar.

    Notice that the second variable that we declared was written as $$foo with 2 dollar symbols before the variable name. This makes it a variable variable — a variable whose name is a variable. What happens is that it first gets the value of $foo, which is “bar” and then uses that value as the variable name. And so, in the example above, defining the variable as $$foo = "My variable name is bar." is equal to defining it as $bar = "My variable name is bar.".

    Making the PHP variable name more dynamic

    Now, what if you want to make your variable name more dynamic? I’ll give you another example.

    Here we are generating a random 1-digit number, we’ll use it later:

    The way that we’ll define the dynamic variables will be a bit different from the example earlier.

    And here we have an array of strings:

    What if we want to create variables whose names are based on the value of strings in the array, like $uno, $dos, and $tres?

    We do it like in the code below. Notice that I defined my dynamically named variable like ${$v} inside the foreach:

    Now let’s use the $numbers I showed you earlier. Pay attention to what I put inside the foreach.

    The foreach let us define variables whose names are $uno1, $dos2 and $tres3, which are concatenated values from arrays $numbers and $words.

    This is where the magic happens: ${$words[$x] . $numbers[$x]}

    We combined elements from $words and $numbers arrays, then turned them into variable names by enclosing them in ${}

    And that’s it! Use variable variables at your convenience. 🙂

  • PHP: get full URL of current page with GET params or without

    The following string variable’s value will get the full URL of the current page, including GET arguments/params, if present.

    To get the full URL of current page but WITHOUT the GET args/params, do this instead:

    The strtok function will return only the part of the URL string up to the character before question mark “?” which is used by GET args/params.

    strtok was suggested in this StackOverflow answer: http://stackoverflow.com/a/4643312

  • PHP: avoid backslash on json_encode

    Hey! 🙂 If you are encoding your Arrays to JSON in PHP using json_encode() and the function adds backslash before your slashes, I’m gonna show you a quick fix.

    This is my original code:

    And the result is:
    {"success":true,"message":"Please enter email\/phone number."}

    See the added backslash before the slash? Ugly, right? To avoid that, simply add the JSON_UNESCAPED_SLASHES argument to your json_encode() function usage like this:

    Try it!

  • Android NetworkOnMainThreadException at StrictMode fix (with deprecated methods used)

    A JSON Parser developed by some members of the Android dev team. But a few weeks after completion of the parser, several methods became deprecated.

    And when we use that parser, we get errors. Part of the stack trace is as follows:

    Not sure if the NetworkOnMainThreadException is being thrown because of the deprecated methods but the following code is what prevents our app from getting the error.

  • Android/Java: get variable data type

    For some reason you might need to get the data type of your Java variable. Here’s an example of what you can try:

    Then do whatever you like with data_type, whose value should be “String”. 🙂

  • Get device’s IMEI in Android app development

    Here’s the content of my MainActivity class where I log the IMEI of my test device.

    You also need to add this permission to your AndroidManifest.xml file:

    Place it inside the <manifest></manifest> tag, but same level as <application></application>.

    Source: How to get the device’s IMEI/ESN programmatically in android?