Implementation
The Bold360 SDK requires the INTERNET permission for all implementations. In your AndroidManifest.xml ensure there is a line like this inside the <manifest> tag.
<uses-permission android:name="android.permission.INTERNET" />
Activity
The simplest way to get started with launching a chat session for your application is to start the BoldChatActivity that is included as part of the SDK. Please be aware that this activity does not use the AppCompat library, so in Android 2.3 (Gingerbread) some visual elements such as input fields will not be in the Holo theme.
-
In
AndroidManifest.xml we need to register the
BoldChatActivity class so we can start it.
Inside the <application> tag add:
<activity android:name="com.boldchat.sdk.BoldChatActivity" android:configChanges="keyboardHidden|orientation|screenSize" />
-
Start by adding a button to your existing layout to launch a
chat.
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Chat" android:onClick="startBoldChat" />
-
Add a method to your activity to launch the
BoldChatActivity, be sure to replace
api_key with the API key you generated in the
Bold360 Admin Center.
<com.boldchat.sdk.BoldChatView xmlns:boldchat="http://schemas.android.com/apk/res-auto" android:id="@+id/boldchat" android:layout_width="match_parent" android:layout_height="match_parent" boldchat:apiKey="98765431342:1234567890:XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
Subview
If you want to embed a Bold360 session into an existing layout, or use it in a fragment it can be accomplished by following these steps.
-
In your layout add the
BoldChatView tag where you want the chat to
appear. The
BoldChatView extends
LinearLayout so it supports all the same
attributes as that layout type, as well as an attribute for setting the API
key.
<com.boldchat.sdk.BoldChatView xmlns:boldchat="http://schemas.android.com/apk/res-auto" android:id="@+id/boldchat" android:layout_width="match_parent" android:layout_height="match_parent" boldchat:apiKey="98765431342:1234567890:XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
-
Inside your initialization code get a reference to the
BoldChatView and create a
BoldChatSession. This is usually done inside of
an
onCreate() method, but can be done any time
after the layout has been inflated.
private BoldChatSession mBoldChat; private void inializeBoldChat() { BoldChatView boldChatView = (BoldChatView)findViewById(R.id.boldchat); mBoldChat = new BoldChatSession.Builder(boldChatView).build(); }
-
When the time is appropriate (and the view is visible on the
screen) you can start the chat session. At this point we also add a listener
that will receive the events related to the chat session. For now we are only
going to handle the
chatSessionClosed() event, and hide the bold
chat view.
private void startBoldChat() { mBoldChat.setListener(new BoldChatSession.BoldChatSessionListener() { @Override public void chatSessionCreated() { } @Override public void chatSessionStarted() { } @Override public void messageArrived(String message, String sender, Date sent) { } @Override public void operatorTyping() { } @Override public void chatSessionEnded() { } @Override public void chatSessionClosed() { // This should be customized depending on how you want to handle the close action. findViewById(R.id.boldchat).setVisibility(View.GONE); } }); mBoldChat.start(); }
-
If the chat session needs to be interrupted or the UI is
being destroyed there is some cleanup that needs to occur by calling
removeListener(). This will remove the UI
listeners associated with the Bold360 session, and if there is an active chat
session it will be persisted in a static variable to be resumed the next time
mBoldChat.start() is called.
protected void stopBoldChat() { mBoldChat.removeListener(); }
-
If you would like the End Chat and Email Transcript
actions to appear in the overflow menu of the action bar you need to make the
appropriate calls to add the menu items, and respond to the menu items being
selected. By default the End Chat will appear as a button at the top of the
chat form, and the email transcript will appear as an icon near the text input
field.
@Override public boolean onCreateOptionsMenu(Menu menu) { mBoldChat.addMenuItems(getMenuInflater(), menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if(mBoldChat.menuItemSelected(item)) { return true; } else { return super.onOptionsItemSelected(item); } }
Checking Chat Online Status
-
In your initialization code where you want to check for chat
availability create a new BoldChatSession instance. Notice this instance does
not contain the BoldChatView argument, this instance will not be dependent on
any UI elements.
private BoldChatSession mBoldChat; private Button mChatButton; private void initializeBoldChat() { mChatButton = (Button)findViewById(R.id.chat_button); mBoldChat = new BoldChatSession.Builder(this, "98765431342:1234567890:XXXXXXXXXXXXXXXXXXXXXXXXXXXXX").build(); }
-
When you are ready to check for the chat availability make
the asynchronous call and handle the result. In this sample we will be hiding
the chat button. Availability results will be cached by the API for up to 60
seconds, so checking for availability more often than that will not yield
different results.
private void checkAvailability() { mBoldChat.getChatAvailability(new ChatAvailabilityListener() { @Override public void onChatAvailable() { // This should be customized based on how you want to handle chat being available. mChatButton.setVisibility(View.VISIBLE); } @Override public void onChatUnavailable(UnavailableReason unavailableReason) { // This should be customized based on how you want to handle chat being unavailable. mChatButton.setVisibility(View.GONE); } @Override public void onChatAvailabilityFailed(int failType, String message) { // This should be customized based on how you want to handle a failure to determine chat availability onChatUnavailable(UnavailableReason.Unknown); } }); }
AppCompat and ActionBarSherlock (Android 2.3 Support)
The AppCompat and ActionBarSherlock compatibility libraries can be used with Bold360, to use one of these libraries you can copy the BoldChatActivity.java from the Bold360 SDK library project into your own project, and modify it to extend ActionBarActivity (for AppCompat) or SherlockActivity (for ActionBarSherlock).