//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 Development
Android Development: Permission denied when running ./gradlew in Mac
I’m using a Mac at the moment to work on my Android app projects. For some reason I need to run ./gradlew in the terminal, but I was getting this error: -bash: ./gradlew: Permission denied To fix this, what I did was run the following commands:
|
1 2 |
Mac-mini:projname moi$ chmod +x gradlew Mac-mini:projname moi$ ./gradlew |
After those commands, my Mac downloaded numerous files […]
Android Studio and the 64K methods limit (DEX error)
Hi! I’ll share a few tips with you on how to find out the number of methods that your Android app has. I’m gonna show you a few tools such as APK file methods counter, and an Android plugin that will show you the number of methods each imported class or dependency has. These will […]
Android: Switch into another activity
To switch into another activity, the method we use is startActivity(). This is what starts another Android activity:
|
1 2 |
Intent i = new Intent(this, NameOfActivity.class); startActivity(i); |
Replace NameOfActivity with the name of any activity you’d like to switch into.
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:
|
1 |
<uses-permission android:name="android.permission.INTERNET" /> |
The web page won’t be loaded into your app without this permission. […]
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 […]
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 — […]