Overriding Activities and Fragments

Overriding Activities and Fragments

You can customize the UI by subclassing UI elements and then injecting them into the framework.

You should always do this just after Chat SDK activation.

Check out the example here.

Override Activity

For example, to override the chat activity you would subclass the PostRegistrationActivity then use:

ChatSDKUI.setPostRegistrationActivity(CustomPostRegistrationActivity.class);

There is a method available for each of the activities in the app.

When you override the activity you can then override any of the functions in that activity. You can easily override the layout file like this:

@Override
protected int getLayout() {
return R.layout.activity_custom_post_registration;
}

Note: Don't forget to register the activity in your AndroidManifest.xml

Override Fragment

ChatSDKUI.setPrivateThreadsFragment(new CustomPrivateThreadsFragment());

Using the Fragments in your app

Usually, the fragments are embedded in the MainActivity. The main activity reloads the fragments when the activity resumes. If you want to use the fragments in your own activity, you should either inherit from MainActivity or make sure that you call loadData() in your activity onResume().

Override the ProfileFragment

The profile fragment is hosted within the profile activity. It is defined withinactivity_profile.xml. To override this fragment, you will need to subclass it and update the layout file:

<fragment
class="sdk.chat.ui.fragments.ProfileFragment"
android:id="@+id/profileFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</fragment>

Replace the standard profile fragment with your custom profile fragment.

Override the ChatActivity

If you are overriding the ChatActivity you will also need to define the main activity for your app like this:

ChatSDK.ui().setMainActivity(YourMainActivity.class);

When the user clicks back from the ChatActivity they will be taken to the app's main activity.

Override ChatView

To do this first you need to override the ChatActivity. Create a new subclass of the ChatActivity

public class CustomChatActivity extends ChatActivity {
protected @LayoutRes
int getLayout() {
return R.layout.custom_activity_chat;
}
}

Also, make a new xml layout file and copy the contents of activity_chat into it. Make sure you give this layout file a new name like custom_activity_layout

As you can see above, we override the getLayout() method and provide our custom layout file.

After Chat SDK initialization, register this subclass:

ChatSDK.ui().setChatActivity(CustomChatActivity.class);

Make a new subclass of the ChatView called CustomChatView.

public class CustomChatView extends ChatView {
public CustomChatView(Context context) {
super(context);
}
}

Open your custom_activity_layout file and find the section:

<sdk.chat.ui.views.ChatView
android:id="@+id/chatView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/messageInputLinearLayout"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>

Change this to:

<sdk.chat.ui.views.CustomChatView
android:id="@+id/chatView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/messageInputLinearLayout"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>

Now the CustomChatView will be inflated into the ChatActivity and you can customize it as necessary.