# Get Started - Android

## Quick Start

TapTalk.io helps you to implement real-time chat with any type of your client app with speed and efficiency. Our Android SDK provides you with various methods to initialize, configure, and build the chat from the client-side - no server-side implementation is required because our reliable infra management service is delivered with the SDK. This page presents a brief overview of the SDK’s structure and abilities, then lets you go through the preliminary steps of implementing the SDK in your own app. We will be using **Android Studio** for the native side implementation.

## Configure and Initialize TapTalk.io Android SDK

#### **Step 1: Create a new application from your dashboard**

**1.** Login to [TapTalk.io Dashboard](https://taptalk.io/), then choose **Development** -> **Apps**

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHKsq4vOlouRa03adbb%2F-MHKvQJYwokZj2SvRd6U%2F08a4ad0-Screenly_-_9.0_Development_-_Apps_-_Empty_State2x.png?alt=media\&token=6f629f44-d932-400f-984d-2fb1a1f0e408)

**2**. Click **New App** Button, input **App Name** and choose **Platform**, and then click **Create New App** Button.

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHKsq4vOlouRa03adbb%2F-MHKxC_cD1-kv3lPxiSk%2F58201a8-Screenly_-_9.1.0_Development_-_Apps_-_New_App_-_Empty_State2x.png?alt=media\&token=ab2fe069-a0b6-4d22-86d3-fdd8e51c235f)

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHKsq4vOlouRa03adbb%2F-MHKxGJV7RBtqLBu8UwT%2Ff2a1418-Screenly_-_9.1.1_Development_-_Apps_-_New_App_-_Filled2x.png?alt=media\&token=5b628aa3-05ec-47e9-98ef-dbc2c23de5bc)

**3.** A pop-up dialog will be shown with provided **App Key ID** & **App Key Secret**

![](https://4266585843-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LfVupFSqh_qZAY9OiCO%2F-MHKsq4vOlouRa03adbb%2F-MHKxQLyhxLOunGM05hx%2Ff869a40-Screenly_-_9.1.3_Development_-_Apps_-_New_App_-_Your_App_Key_-_Dismiss2x.png?alt=media\&token=b7bfb1cb-65ff-47e6-beda-69011f5addb8)

{% hint style="warning" %}
**Note:** Please remember to save your **App Key ID** & your **App Key Secret** because it will only be shown once and will be used in TapTalk.io initialization
{% endhint %}

#### Step 2: Install TapTalk.io SDK

To start, open the **android** folder inside your Flutter project root folder as a project using Android Studio. Then add the following repositories to your **top-level build.gradle** file.

{% code title="build.gradle (project)" %}

```javascript
allprojects {
   repositories {
      ...
      maven { url "https://jitpack.io" }
      maven { url "https://s3.amazonaws.com/repo.commonsware.com" }
      jcenter()
   }
}
```

{% endcode %}

Then add the following dependency to your **app-level** build.gradle:

{% code title="build.gradle (:app)" %}

```javascript
dependencies {
    implementation 'com.github.taptalk-io:taptalk.io-android:2.18.3'
}
```

{% endcode %}

{% hint style="info" %}
You can check a more updated version release notes of the SDK [here](https://github.com/taptalk-io/taptalk.io-android/releases).
{% endhint %}

In the app build.gradle file, make sure that your project is using the supported Java version like below.

{% code title="build.gradle (:app)" %}

```javascript
android {
    
    ...

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
}
```

{% endcode %}

After adding the required dependency, sync your project and begin the next step by writing custom platform-specific code using platform channels.

#### Step 3: Create Flutter Platform Client

If you have finished this step from the [iOS side](https://docs.taptalk.io/powertalk-chat-sdk-documentation/get-started-ios#step-6-create-flutter-platform-client), you can skip to the [next step](#step-3-add-an-android-platform-specific-implementation).

In this example, we will add the following inside the MyHomePageState class:

1. Method Channel to connect to the native Android host
2. `openPowerTalkUI()` function to open the chat room list view
3. A button to call the `openPowerTalkUI()` function

{% tabs %}
{% tab title="Dart" %}
{% code title="main.dart" %}

```dart
class _MyHomePageState extends State<MyHomePage> {
  
  // Create the MethodChannel
  static const powerTalkMethodChannel = MethodChannel('io.taptalk.powertalk');
  
  // Function to open the chat room list UI
  Future<void> _openPowerTalkUI() async {
    try {
      // Initialize PowerTalk SDK
      final bool isPowerTalkInitialized = await powerTalkMethodChannel.invokeMethod('initPowerTalk');
      if (isPowerTalkInitialized) {
        // Authenticate PowerTalk SDK
        final bool isPowerTalkAuthenticated = await powerTalkMethodChannel.invokeMethod('authPowerTalk');
        if (isPowerTalkAuthenticated) {
          // Open chat room list
          await powerTalkMethodChannel.invokeMethod('openChatRoomList');
        }
      }
    }
    on PlatformException catch (e) {
      print(e.message);
    }
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      ...
      body:
        ...
          // Sample button to call openPowerTalkUI
          ElevatedButton(
            onPressed: _openPowerTalkUI,
            child: Text(
              'Get Started with PowerTalk',
            ),
          ),
      ...
    );
  }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

Please note the name parameter in the MethodChannel constructor that was used (`io.taptalk.powertalk`), and the method name in the `invokeMethod()` parameter (`initPowerTalk`, `authPowerTalk` & `openChatRoomList`). We will use the same names later to configure Flutter engine in the native side.

#### Step 4: Add an Android platform-specific implementation <a href="#step-3-add-an-android-platform-specific-implementation" id="step-3-add-an-android-platform-specific-implementation"></a>

Next, we will configure the native side of the Android host implementation. Go back to previously opened Android Studio project, and open **MainActivity** file. We will do the following to the Activity:

1. Add a MethodChannel Result variable to handle asynchronous call
2. override `configureFlutterEngine` to handle calls from the MethodChannel that we previously created.
3. Add native functions to initialize & authenticate PowerTalk SDK and open the chat room list view

{% tabs %}
{% tab title="Java" %}
{% code title="MainActivity.java" %}

```java
import io.taptalk.TapTalk.Helper.TapTalk;
import io.taptalk.TapTalk.Listener.TapCommonListener;
import io.taptalk.TapTalk.Listener.TapListener;
import io.taptalk.TapTalk.Manager.TapUI;

public class MainActivity extends FlutterActivity {

    // MethodChannel Result to handle asynchronous call
    private MethodChannel.Result powerTalkMethodChannelResult = null;

    // Override configureFlutterEngine
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        
        // Handle MethodChannel calls
        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), "io.taptalk.powertalk")
            .setMethodCallHandler((call, result) -> {
                // Save result to variable
                powerTalkMethodChannelResult = result;
                switch (call.method) {
                    // Call native methods
                    case "initPowerTalk":
                        initPowerTalk();
                        break;
                    case "authPowerTalk":
                        authPowerTalk();
                        break;
                    case "openChatRoomList":
                        openChatRoomList();
                        break;
                    default:    
                        result.notImplemented();
                }
            }
        );
    }
    
    // Method to initialize PowerTalk SDK
    private void initPowerTalk() {
        TapTalk.init(
            getApplicationContext(),
            POWERTALK_APP_KEY_ID,
            POWERTALK_APP_KEY_SECRET,
            R.drawable.your_app_icon,
            getString(R.string.your_app_name),
            POWERTALK_APP_BASE_URL,
            TapTalkImplementationType,
            new TapListener() {
                // You can override TapListener callbacks here
            }
        );
        if (powerTalkMethodChannelResult != null) {
            powerTalkMethodChannelResult.success(true);
        }
    }
    
    // Method to authenticate PowerTalk SDK
    private void authenticatePowerTalk() {
        TapTalk.authenticateWithAuthTicket(
            POWERTALK_AUTH_TICKET,
            true,
            new TapCommonListener() {
                @Override
                public void onSuccess(String successMessage) {
                    if (powerTalkMethodChannelResult != null) {
                        powerTalkMethodChannelResult.success(true);
                    }
                }

                @Override
                public void onError(String errorCode, String errorMessage) {
                    if (powerTalkMethodChannelResult != null) {
                        powerTalkMethodChannelResult.error(errorCode, errorMessage, null);
                    }
                }
            }
        )
    }

    // Method to open chat room list view
    private void openChatRoomList() {
        TapUI.getInstance().openRoomList(this);
        if (powerTalkMethodChannelResult != null) {
            powerTalkMethodChannelResult.success(true);
        }
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code title="MainActivity.kt" %}

```kotlin
import io.taptalk.TapTalk.Helper.TapTalk
import io.taptalk.TapTalk.Listener.TapCommonListener
import io.taptalk.TapTalk.Listener.TapListener
import io.taptalk.TapTalk.Manager.TapUI

class MainActivity: FlutterActivity() {

    // MethodChannel Result to handle asynchronous call
    private var powerTalkMethodChannelResult: MethodChannel.Result? = null

    // Override configureFlutterEngine
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        // Handle MethodChannel calls
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "io.taptalk.powertalk")
            .setMethodCallHandler {
                call, result ->
                powerTalkMethodChannelResult = result
                when (call.method) {
                    "initPowerTalk" -> initPowerTalk()
                    "authPowerTalk" -> authenticatePowerTalk()
                    "openChatRoomList" -> openChatRoomList()
                    else -> result.notImplemented()
                }
        }
    }
    
    // Method to initialize PowerTalk SDK
    private fun initPowerTalk() {
        TapTalk.init(
            applicationContext,
            POWERTALK_APP_KEY_ID,
            POWERTALK_APP_KEY_SECRET,
            R.drawable.your_app_icon,
            getString(R.string.your_app_name),
            POWERTALK_APP_BASE_URL,
            TapTalkImplementationType,
            object : TapListener() {
                // You can override TapListener callbacks here
            }
        )
        powerTalkMethodChannelResult?.success(true)
    }
    
    // Method to authenticate PowerTalk SDK
    private fun authenticatePowerTalk() {
        TapTalk.authenticateWithAuthTicket(
            POWERTALK_AUTH_TICKET,
            true,
            object : TapCommonListener() {
                override fun onSuccess(successMessage: String) {
                    powerTalkMethodChannelResult?.success(true)
                }

                override fun onError(errorCode: String, errorMessage: String) {
                    powerTalkMethodChannelResult?.error(errorCode, errorMessage, null)
                }
            }
        )
    }

    // Method to open chat room list view
    private fun openChatRoomList() {
        TapUI.getInstance().openRoomList(this)
        powerTalkMethodChannelResult?.success(true)
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
You can obtain **POWERTALK\_AUTH\_TICKET** by following the guide in [Authentication  page](https://docs.taptalk.io/powertalk-chat-sdk-documentation/powertalk-android/authentication).
{% endhint %}

#### Step 5: Enable Chat Features

Please follow the steps in [Enable Chat Features](https://docs.taptalk.io/powertalk-chat-sdk-documentation/powertalk-android/enable-chat-features) page to enable TapTalk.io's chat features, such as contact sync and sending media, document, and location messages.

You can now try running the app. Pressing the sample button will invoke the `openPowerTalkUI()` method that we previously created, which will initialize & authenticate PowerTalk SDK, and open the chat room list view once the initialization is completed. You may also try a different implementation and separate the **init**, **authenticate**, and the **open view** methods. You can also add more calls to other native methods with the same approach using the same MethodChannel.

{% hint style="info" %}
**Note**: You can check a more complete implementation guide in the [PowerTalk Android](https://docs.taptalk.io/powertalk-chat-sdk-documentation/powertalk-android) section.
{% endhint %}
