Category: Coding

  • 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

  • Android app dev: background image sizes for different screen sizes

    Here are the recommended background image sizes in pixels for Android app development:

    xxxhdpi: 1280×1920 px
    xxhdpi: 960×1600 px
    xhdpi: 640×960 px
    hdpi: 480×800 px
    mdpi: 320×480 px
    ldpi: 240×320 px

    …as suggested in this StackOverflow page: Android: Background Image Size (in Pixel) which Support All Devices

  • Android ImageView has unwanted padding: layout troubleshooting


    If you use ImageView in your android app layout and you get that unwanted padding around the image, here’s an Android XML attribute that helped me solved the layout issue:

    Add android:adjustViewBounds="true" attribute to your ImageView XML tag and you’ll probably see the unwanted padding go away.

    According to the ImageView docs:

    android:adjustViewBounds

    Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

    You might also want to check the related Java method: setAdjustViewBounds(boolean)

  • Android app dev: image sizes for variety of screen sizes

    As a mobile app developer, we need to consider various screen sizes for a wide range of Android devices.

    Here’s a quick note of recommended image sizes for Android app icons:

    36×36 for low-density (LDPI)
    48×48 for medium-density (MDPI)
    72×72 for high-density (HDPI)
    96×96 for extra high-density (XHDPI)
    180×180 for extra extra high-density (XXHDPI)
    192×192 for extra extra extra highv-density (XXXHDPI)

    Image Scaling

    Source: Units and measurements – Layout – Google design guidelines

    Images can be scaled to look the same across different screen resolutions by using these ratios:

    Screen resolution dpi Pixel ratio Image size (pixels)
    xxxhdpi 640 4.0 400 x 400
    xxhdpi 480 3.0 300 x 300
    xhdpi 320 2.0 200 x 200
    hdpi 240 1.5 150 x 150
    mdpi 160 1.0 100 x 100

    For our reference. 🙂

    You may read more about Supporting Multiple Devices.

    I also want to share MakeAppIcon. Use that online tool to quickly generate resized images for various Android screen sizes for you.