It’s funny how we spent about an hour trying to figure out why the Intent extras that we’re setting are not being sent back to onActivityResult(). If we only knew that the solution was as simple as moving one line of code. The call to super.onBackPressed() should be placed after setting Intent extras and after […]
Java
Android app dev: Missing import org.apache.commons.lang3.text.WordUtils;
I use a few instances of WordUtils.capitalizeFully() on an Android app that I’ve been working on. And there are times when the import org.apache.commons.lang3.text.WordUtils; breaks, sometimes after merging with some of my git branches. To fix this, what I do is open app/build.gradle and add the following line under dependencies:
1 |
compile 'org.apache.commons:commons-lang3:3.4' |
Hope this saves somebody […]
Something to avoid when using Android RecyclerView
When using Android’s RecyclerView, don’t use the same inflated XML layout file for items you intend to shuffle on the fly! Their contents will overlap. -CK
Java: method to convert degrees into directions (16-wind compass rose)
I’m working on a weather app for Android. The data for wind direction that I get from the weather API is in form of degrees (0 to 360). I want to display wind direction in a user-friendly format such as N (North), NNE (North-northeast), NE (Northeast), ENE (East-Northeast), E (East), ESE (East-Southeast), SE (Southeast), SSE […]
Android M runtime permissions utility class
Hello world, I prepared a PermissionsUtil.java class and it’s on GitHub. Check it out: AndroidPermissionsUtil Why use it? It will help by letting you write less code as you add conditions to your Android app that supports Android 6.0 (Marshmallow). The utility class defines helper methods that were used in the following example. Sample usage […]
Android Studio: add Google Map from empty activity (dev note)
//Update: I prepared a simple Android app that features a Google Map with markers that form clusters together when zoomed out. Check it out: Simple Android Map Clustering on GitHub. This “dev note” of mine will give you a rough idea on how to add a Google Map to your Android up from an empty […]
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 […]