Category: Technology

  • 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?

  • Android: open web page in a browser

    To open a web page in an Android app, we need to create a new Intent and start a new Activity with it.

    Here’s a Java method that we can re-use:

    Just pass the URL of the web page you’d like to open to this method.

    Source: How to open a website when a Button is clicked in Android application?

  • Laravel 5: How to change Auth URL from “/auth/login” to just “login”:

    Just sharing a link telling us how to remove the “auth” in the URL for Laravel’s pre-made login system.

    You need to modify App\Http\Middleware\Authenticate::handle() method and change auth/login to /login.

    Then you need to add $loginPath property to your \App\Http\Controllers\Auth\AuthController class.

    Source: How to change default redirect URL of Laravel 5 Auth filter?

  • Android / Java: Get the name of method being executed

    If you want to trace the name of the method that’s being executed, the Java code snippet in this page might help you.

    This is especially handy when you are debugging by tracking every method that is executed. You can enclose these lines of codes in a function that you call in every method — that’s easier than typing the name for each method you’re tracking, right? 🙂

    The String variable methodName will contain the name of the method where you place the following lines of code:

  • Android: underline text in your app

    When I need to underline text in an app that I’m developing, I like doing it programmatically. The code is in the Java file, not in the XML.

    Maybe we should make a function for this where we pass the ID of the TextView whose content we’d like to underline.

    Disadvantages in this method:

    • It’s done in the Java file, not in UI files such as XML. It makes more sense to add codes for styling in XML files to me.
    • This underlines all text in the TextView. What if we need to underline only one text in a paragraph?

    If you have any suggestion, feel free to comment. 🙂

    // EDIT:

    Here’s a method I prepared to make underlining TextView values easier:

    public void setTextViewUnderline(TextView myTextView){
    String htmlString = “” + myTextView.getText() + ““;
    myTextView.setText(Html.fromHtml(htmlString));
    }

  • Android: dimensions cheat sheets for graphic designers AND developers

    Just sharing the link to a “cheat sheet” that I found:

    Android Cheatsheet for Graphic Designers

    I think that this resource should have been said to be for developers too, because developers need to know about dimensions of graphics in the Android apps they develop.

  • Android transparent overlay without View

    We’re not going to use ViewOverlay here. We’ll use Dialog instead.

    The overlay does not show on button click. What I did was let the overlay appear when the activity is loaded.

    First, create the XML file for the contents of that dialog overlay.

    \app\src\main\res\layout\popup_content.xml

    Then add the following code into your activity’s Java file. Please edit as needed in your project.

    Notes:
    You might need to add lines of “import” codes yourself for the classes I used.
    You might need to fill in missing @color and @dimen values yourself.

    app\src\main\java\com\yourpackagename\yourappname\activities\MainActivity.java