Android

Use Contentstack to power content for your Android projects

Create Android-based applications and use the Android SDK to fetch and deliver content from Contentstack. The SDK uses Content Delivery APIs. 

Getting Started

This guide will help you get started with our Android SDK to build apps powered by Contentstack.

Prerequisites

To get started with Android SDK, you will need one of the following: Android Studio or Eclipse.

SDK Installation and Setup

To add the Contentstack Android SDK to your existing project, perform the steps given below:

  1. Download the Android SDK and extract the ZIP file to your local disk.
  2. Add references/dependencies using Eclipse/Android Studio:
  3. Android Studio:

    1. Copy the 'Contentstack-x.x.x.jar' file into your project's libs folder.
    2. Add the dependency code into your 'build.gradle' file.
    3. compile fileTree(dir: 'libs', include: ['*.jar'])
      		

    Eclipse:

    1. Copy the 'Contentstack-x.x.x-javadoc' folder and the 'Contentstack-x.x.x.jar' and 'Contentstack-x.x.x.jar.properties' files into your project's 'libs' folder.
    2. Open the 'Properties' window of the project. Select the 'Java Build Path' option on the left-hand side menu, click on the 'Libraries' tab, and add the JAR references there.
    3. Configure 'AndroidManifest.xml' with permissions and receivers using the following code:
    4. <!-- Allows applications to connect network (Required) -->
      <uses-permission android:name="android.permission.INTERNET" />
      <!-- Allows applications to access information about networks (Required) -->
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
      <receiver
          android:name="com.builtio.contentstack.ConnectionStatus"
          android:enabled="true" >
          <intent-filter>
              <action android:name="android.net.conn.CONNECTIVITY_CHANGE" >
              </action>
          </intent-filter>
      </receiver>
      <receiver
          android:name="com.builtio.contentstack.ClearCache"
          android:enabled="true">
          <intent-filter>
              <action android:name="StartContentStackClearingCache">
              </action>
          </intent-filter>
      </receiver>
      		

Initialize SDK

To initialize the SDK, specify application context, the API key, access token, and environment name of the stack as shown in the snippet given below:

Stack stack = Contentstack.stack(context, "siteApiKey", "accessToken", "enviroment_name");

Once you have initialized the SDK, you can query entries to fetch the required content.

Basic Queries

Get a Single Entry

To retrieve a single entry from a content type use the code snippet given below:

ContentType contentType = stack.contentType("content_type_uid");
Entry blogEntry = contentType.entry("entry_uid");
blogEntry.fetch(new EntryResultCallBack() {
    @Override
    public void onCompletion(ResponseType responseType, Error error) {
        if (error == null) {
            // Success block
        } else {
            // Error block  
        }
    }
});

Get Multiple Entries

To retrieve multiple entries of a particular content type, use the code snippet given below: 

//stack is an instance of Stack class
Query blogQuery = stack.contentType("content_type_uid").query();
blogQuery.find(new QueryResultsCallBack() {
    @Override
    public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
        if(error == null){
            //Success block
        }else{
            //Error block
        }
    }
});

These were example of some of the basic queries of the SDK. For advanced queries, refer to our API reference documentation by visiting the link given below.

API Reference

Go through our SDK API Reference guide to know about the methods that can be used to query your content in Contentstack.

Read Android API Reference Guide

Download SDK

The Android SDK interacts with and uses our Content Delivery APIs to fetch content from Contentstack. Download the SDK to deliver content to your Android projects.

Download Android SDK

Example Apps

To help you get started, we have created some sample applications that are powered by Contentstack Android SDK. Click on any of the links below to read the tutorials of the app, view app demo, or download the code from GitHub.

Was this article helpful?
top-arrow