Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Providing a Link to Other Published Apps in the Google Play Market

Published? true
FormatLanguage: WikiFormat

Problem:

Your developed App is running on a device; you want a link to your other Apps on the Android Market to encourage users to try them.

Solution:

Use an Intent and URI that contains your Publisher Name or Package Name.

Discussion:

Android's Intent system is a great way for your application to leverage functionality that has already been written by other developers. The Android Market application, which is used to browse and install Apps, can be called from an application by using an Intent. This allows an existing App to have a link to other Apps on the Android Market, thus allowing an App developer or publisher to encourage users to try their other Apps.

To search via the Android Market App the standard Intent mechanism is used, as described in the recipe Opening a Web Page, Phone Number or anything else with an Intent. The Uniform Resource Indentifier (URI) used is market://search?q=search term where search term is replaced by the appropriate text, such as the program name or keyword. The Intent Action is ACTION_VIEW.

The URI can also point directly to the Android Market details page for a package by using market://details?id=package name where package name is replaced by the unique Package Name for the App.

The program shown here will allow a text search of the Android Market or show the details page for a given App. Here is the layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <EditText android:id="@+id/etSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:singleLine="true"/>
    <RadioGroup android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton android:id="@+id/rdSearch"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:checked="true"
           android:text="search"
           android:textSize="20sp"/>
        <RadioButton android:id="@+id/rdDetails"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:text="details"
           android:textSize="20sp"/>
    </RadioGroup>
    <Button android:id="@+id/butSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Search Android Market"/>
</LinearLayout>

An EditText allows entry of the search term, a RadioButton can be used to do a straight search or show an Apps details page (provided the full Package Name is known). The Button starts the search.

The important point to notice in the code is that the search term is encoded.

public class MarketSearchActivity extends Activity {

    RadioButton publisherOption;    //Option for straight search or details

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Search button press processed by inner class
        findViewById(R.id.butSearch).setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) {
                String searchText;
                // Reference search input
                EditText searchFor=(EditText)findViewById(R.id.etSearch);
                try {
                    // URL encoding for spaces and punctuation in search term
                    searchText =
                         URLEncoder.encode(
                         searchFor.getText().toString(),"UTF-8");
                } catch (UnsupportedEncodingException e) {
                    searchText = searchFor.getText().toString();
                }
                // Get search option
                RadioButton searchOption=
                    (RadioButton)findViewById(R.id.rdSearch);
                Uri intentUri;
                if (searchOption.isChecked()) {
                    intentUri=Uri.parse("market://search?q=" + searchText);
                } else {
                    intentUri=Uri.parse("market://details?id=" + searchText);
                } 
                Intent intent = new Intent(Intent.ACTION_VIEW, intentUri);
                try {
                    MarketSearchActivity.this.startActivity(intent);
                } catch (ActivityNotFoundException anfe) {
                    Toast.makeText(MarketSearchActivity.this, 
                        "Please install the Android Market App",
                        Toast.LENGTH_SHORT);
                }
            }
        });
    }
}

A straight text search is simply the text appended to the URI market://search?q=. To search by publisher name use the pub: qualifier, i.e. append the publishers name to market://search?q=pub:. However, at the time of writing a bug exists in some versions of Android Market that causes publisher names of more than one word to return no results. So whilst market://search?q=pub:IMDb works, market://search?q=pub:O'Reilly+Media does not. The work around is to use the straight text search for publisher names of two words or more. For example market://search?q=oreilly+media.

The pub: search qualifier is also case sensitive, thus market://search?q=pub:IMDb returns a result but market://search?q=pub:imdb does not.

It is also possible to search for a specific application if the Package Name is known by using the id qualifier. So if a App has Package Name of com.example.myapp the search term will be market://search?q=id:com.example.myapp. Even better go straight to the Apps details page with market://details?q=id:com.example.myapp. For example O'Reilly has a free App the details of which can be show using market://details?id=com.aldiko.android.oreilly.isbn9781449388294.

Using the above techniques it is very easy to put a button or menu option on a screen to allow users to go directly to other Apps that you have published.

See Also:

[1]