Think twice before buying anything from Lazada. They can send you the wrong item, have you return the wrong item with a promise of refund, give you no refund, and never get back to you again even when you already paid! Long story short: I purchased an item last year (already paid) and Lazada sent […]
Author: Catzie
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[...] E/AndroidRuntime: Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1148) at java.net.InetAddress.lookupHostByName(InetAddress.java:405) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:251) at java.net.InetAddress.getAllByName(InetAddress.java:229) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) [...] |
Not sure if the NetworkOnMainThreadException is being thrown because of the deprecated […]
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:
|
1 2 3 |
String date_created = "2016-02-04"; String data_type = date_created.getClass().getSimpleName(); |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package net.catzie.playground; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.telephony.TelephonyManager; import android.util.Log; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); String deviceID = telephonyManager.getDeviceId(); Log.d("Device IMEI", deviceID); } } |
You also need to add this permission to your AndroidManifest.xml file:
|
1 |
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> |
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:
|
1 2 3 4 5 |
private void goToUrl (String url) { Uri uriUrl = Uri.parse(url); Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl); startActivity(launchBrowser); } |
Just pass the URL of the web page you’d like to open to this method. Source: How to open a website when a […]
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 — […]
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.
|
1 2 3 |
TextView myTextView = (TextView) findViewById(R.id.myTextViewID); String htmlString = "<u>" + myTextView.getText() + "</u>"; myTextView.setText(Html.fromHtml(htmlString)); |
Disadvantages in this method: […]
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#78000000" android:padding="@dimen/activity_margin" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:textColor="#ffffff" android:text="This is your dialog with transparent background." android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_close_overlay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:textColor="#ffffff" android:text="Dismiss"/> </LinearLayout> |
Then add the following code into your activity’s Java […]