> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# ShortCut Formatter

> ShortCut Formatter — CometChat integration guide.

## Introduction

The ShortCutFormatter class extends the CometChatTextFormatter class to provide a mechanism for handling shortcuts within messages. This guide will walk you through the process of using ShortCutFormatter to implement shortcut extensions in your CometChat application.

## Setup

1. **Create the ShortCutFormatter Class**: Define the `ShortCutFormatter` class by extending the `CometChatTextFormatter` class.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
      public class ShortCutFormatter extends CometChatTextFormatter {
          // Class implementation
      private HashMap<String, String> messageShortcuts;
      private List<SuggestionItem> shortcuts;
      }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    class ShortCutFormatterKotlin : CometChatTextFormatter('!') {
        // Class implementation
        private val messageShortcuts: HashMap<String, String> = HashMap()
        private val shortcuts: MutableList<SuggestionItem> = ArrayList()
    }
    ```
  </Tab>
</Tabs>

2. **Constructor**: Initialize the `messageShortcuts` map and `shortcuts` list in the constructor.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
     public ShortCutFormatter() {
         super('!');
         messageShortcuts = new HashMap<>();
         prepareShortCuts();
         shortcuts = new ArrayList<>();
     }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
      init {
            prepareShortCuts()
        }
    ```
  </Tab>
</Tabs>

3. **Prepare Shortcuts**: Implement the `prepareShortCuts()` method to fetch shortcuts from the server using CometChat extension.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    private void prepareShortCuts() {
      // Implementation to fetch shortcuts from server
      CometChat.callExtension("message-shortcuts", "GET", "/v1/fetch", null, new CometChat.CallbackListener<JSONObject>() {
          @Override
          public void onSuccess(JSONObject responseObject) {

              Iterator<String> keysItr;
              try {
                  JSONObject shortcutObject = responseObject.getJSONObject("data").getJSONObject("shortcuts");
                  keysItr = shortcutObject.keys();

                  while (keysItr.hasNext()) {
                      String key = keysItr.next();
                      String value = shortcutObject.getString(key);
                      messageShortcuts.put(key, value);
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
          }

          @Override
          public void onError(CometChatException e) {
          }
      });
    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    private fun prepareShortCuts() {
            CometChat.callExtension(
                "message-shortcuts",
                "GET",
                "/v1/fetch",
                null,
                object : CometChat.CallbackListener<JSONObject>() {
                    override fun onSuccess(responseObject: JSONObject) {
                        try {
                            val shortcutObject =
                                responseObject.getJSONObject("data").getJSONObject("shortcuts")
                            val keysItr: Iterator<String> = shortcutObject.keys()

                            while (keysItr.hasNext()) {
                                val key = keysItr.next()
                                val value = shortcutObject.getString(key)
                                messageShortcuts[key] = value
                            }
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                    }

                    override fun onError(e: CometChatException) {}
                })
        }
    ```
  </Tab>
</Tabs>

4. **Override Search Method**: Override the `search()` method to search for shortcuts based on the entered query.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    @Override
    public void search(@NonNull Context context, String queryString) {
        // Implementation to search for shortcuts
          String query = getTrackingCharacter() + queryString;
        shortcuts.clear();
        if (messageShortcuts.containsKey(query)) {
            SuggestionItem suggestionItem = new SuggestionItem("", query + "  =>  " + messageShortcuts.get(query), null, null, messageShortcuts.get(query), null, null);
            suggestionItem.setHideLeadingIcon(true);
            shortcuts.add(suggestionItem);
        }
        setSuggestionItemList(shortcuts);
    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
      override fun search(context: Context, queryString: String?) {
            val query = trackingCharacter.toString() + queryString
            shortcuts.clear()
            if (messageShortcuts.containsKey(query)) {
                val suggestionItem = SuggestionItem(
                    "",
                    "$query  =>  ${messageShortcuts[query]}",
                    null,
                    null,
                    messageShortcuts[query],
                    null,
                    null
                )
                suggestionItem.isHideLeadingIcon = true
                shortcuts.add(suggestionItem)
            }
            suggestionItemList.value = shortcuts
        }
    ```
  </Tab>
</Tabs>

5. **Handle Scroll to Bottom**: Override the `onScrollToBottom()` method if needed.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
     @Override
     public void onScrollToBottom() {
         // Implementation if needed
     }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    override fun onScrollToBottom() {
            TODO("Not yet implemented")
        }
    ```
  </Tab>
</Tabs>

## Usage

1. **Initialization**: Initialize an instance of `ShortCutFormatter` in your application.

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    ShortCutFormatter shortCutFormatter = new ShortCutFormatter();
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
     val shortCutFormatter = ShortCutFormatter()
    ```
  </Tab>
</Tabs>

2. **Integration**: Integrating the `ShortCutFormatter` into your CometChat application involves incorporating it within your project to handle message shortcuts. If you're utilizing the [CometChatConversationsWithMessages](/ui-kit/android/v4/conversations-with-messages) component, you can seamlessly integrate the ShortCutFormatter to manage shortcut functionalities within your application.

<Tabs>
  <Tab title="XML">
    ```xml theme={null}
    <com.cometchat.chatuikit.conversationswithmessages.CometChatConversationsWithMessages
     android:id="@+id/conversationWithMessages"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Java">
    ```java theme={null}
    // Integrate ShortCutFormatter into your CometChat application
    CometChatConversationsWithMessages cometChatConversationsWithMessages = findViewById(R.id.conversationWithMessages);

    List<CometChatTextFormatter> cometChatTextFormatters = CometChatUIKit.getDataSource().getTextFormatters(this);
    cometChatTextFormatters.add(new ShortCutFormatter());
    cometChatConversationsWithMessages.setMessagesConfiguration(new MessagesConfiguration().setMessageComposerConfiguration(new MessageComposerConfiguration().setTextFormatters(cometChatTextFormatters)));
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    val cometChatConversationsWithMessages: CometChatConversationsWithMessages =
                findViewById<CometChatConversationsWithMessages>(R.id.conversationWithMessages)

    val cometChatTextFormatters = CometChatUIKit.getDataSource().getTextFormatters(this)
    cometChatTextFormatters.add(ShortCutFormatter())
    cometChatConversationsWithMessages.messagesConfiguration =
                MessagesConfiguration().setMessageComposerConfiguration(
                    MessageComposerConfiguration().setTextFormatters(cometChatTextFormatters)
                )
    ```
  </Tab>
</Tabs>

## Example

<Frame>
  <img src="https://mintcdn.com/cometchat-22654f5b/2U5tVIzH12dbbFtr/images/ae5495f0-shortcutextensionguidedemo-0a2507bbb4adf5139e284b8833961c7c.png?fit=max&auto=format&n=2U5tVIzH12dbbFtr&q=85&s=a16f7e94ecfc59466350190aeb6882b4" width="9566" height="6820" data-path="images/ae5495f0-shortcutextensionguidedemo-0a2507bbb4adf5139e284b8833961c7c.png" />
</Frame>
