Adding an “options menu” to an Android app’s Activity is quite simple. This is an example of what an Android options menu looks like. Look at the icon that looks like 3 dots vertically stacked. My dev environment status as of writing this Android Studio version: v. 2.1 OS: Mac OS X El Capitan v. […]
mobile development
Android app development: click a ListView item programmatically
I’m going to share a code snippet that you may use to programmatically click a ListView item in your Android app. This will not simply highlight the desired ListView item, but also trigger the onItemClick listener. This code is what I placed in a Fragment’s onLoadFinished() method:
1 2 |
int default_position = 2; listView.performItemClick(listView.getChildAt(default_position), default_position, listView.getItemIdAtPosition(default_position)); |
Simply replace the default_position value with the […]
Android app dev: Add ActionBar to Preference Activity
When I converted an activity into PreferenceActivity, I noticed that its ActionBar disappeared. I’ll show you how I brought the ActionBar back to my PreferenceActivity. In this guide I will assume that the name of the file that contains your is SettingsActivity.java, and that the view assigned to your SettingsActivity.java is named . First, create […]
Android app dev: ShareActionProvider in Activity & Fragment
I’ve been taking the Developing Android Apps course on Udacity, and yesterday I encountered a difficulty in solving the Quiz: Share Intent. One of the course’s goal is to build the Sunshine App – an Android app with weather forecast. In that quiz I had to implement ShareActionProvider on my DetailsFragment so that a share […]
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 app codes for Google Maps with clustering
I prepared Android app codes with Google Maps and clustering feature. Feel free to use these as basis for your new projects. They are on GitHub: [1] Default Map Markers: catzie/SimpleAndroidMapClustering [2] Custom Map Markers: catzie/Simple-Android-Map-Clustering-with-Custom-Markers I simply took the necessary classes and res files from Android Maps Utility demo, and then placed […]
Android Maps: Get current location coordinates with Google API Client Builder
To get your current location via Google Map in an Android app, this was how we do it:
1 2 3 4 |
@Override public void onMapReady(GoogleMap googleMap) { Location location = googleMap.getMyLocation(); } |
Stop. Don’t use that! The method GoogleMap.getMyLocation() is deprecated. Instead, we need to use FusedLocationApi and the callback onLocationChanged(). Here’s an example code that works for me ( consider copying the contents of onResume() into onStart() […]
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 […]
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: 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: […]