Author Archives: arunv2000

Fragment isAdded() vs getActivity()

In Android fragments, its a common practice to check if the fragment is still attached to the activity before performing some action, to avoid IllegalStateException. There seem to be several similar ways to achieve this. Checking if getActivity() == null is one way. However a more reliable way seems to be to use isAdded(). This internally checks if the activity is not null, as well as checks if the current fragment is added to the activity.

Advertisements

Settings: Show Layout Bounds

While implementing a complex UI layout in Android, it is very useful to get a good picture of the bounds (the border) of different components within the UI. For instance, if a relative layout’s position is modified programmatically, getting the bounds of the relative layout is useful. This helps in figuring out if a layout’s final position in the screen is in harmony with the expected position of the layout in the UI design. This also gives a good visual indicator of the distance between components when (say) RelativeLayout.below is specified.

In order to do this, turn on developer mode in Android. Then select Settings -> Developer Options -> Show layout bounds. This will draw blue rectangles on every UI component for all applications.

Spin up the file chooser UI and select an image.

source: Android SDK Sample code
/**
* Fires an intent to spin up the “file chooser” UI and select an image.
*/
public void performFileSearch() {

// BEGIN_INCLUDE (use_open_document_intent)
// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system’s file browser.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

// Filter to only show results that can be “opened”, such as a file (as opposed to a list
// of contacts or timezones)
intent.addCategory(Intent.CATEGORY_OPENABLE);

// Filter to show only images, using the image MIME data type.
// If one wanted to search for ogg vorbis files, the type would be “audio/ogg”.
// To search for all documents available via installed storage providers, it would be
// “*/*”.
intent.setType(“image/*”);

startActivityForResult(intent, READ_REQUEST_CODE);
// END_INCLUDE (use_open_document_intent)
}

Failed to find provider info for com.example.provider.UniqueContentProvider

The above error arises when the content provider has been declared incorrectly in the manifest.
1. Make sure that this is defined WITHIN the tags

In the above, android:authorities is just the name of a string that should be unique. Its different from the android:name, which is the fully qualified name of your ContentProvider implementation that implements android.content.ContentProvider

2. Although the actual content provider implementation may reside in a library project, the definition should be reside in the androidmanifest.xml of the main project.

Viewing Contextual Android source code from the Developer documentation – Chrome plug in

If you want to look at Android source code in a contextual manner, use these 2 Chrome plug-ins:
AD (Android SDK Search) and ARN (Android resource navigator).

After installing these,

  • Open Chrome
  • Type AD followed by Class (say) TextView
  • The android developer site’s TextView page opens.

Notice the “View Source” link right next to the TextView title. Click on it to go to the class implementation.
Follow similar steps to look at resource source code. The beauty of this is that the tool will navigate you to whichever repo the source code is located in – you don’t have to do this yourself.

I found this when trying to see how they implemented the InputFilter class for the EditText component.