Displaying chats

Once you create or join some chats you can display them using the ChatListFragment provided by the Delivered SDK. This ready made component consists on a list of chats you can interact with. If you have not yet created a chat please refer to the Public group chats or One-to-one chats sections for instructions on how to do this.

The ChatListFragment

The ChatListFragment main function is to display chats you have created or joined as a list, as well as to provide interfaces to handle clicks on the chat list items. To add the ChatListFragment to your application you can use the following code snippet:

The ChatListFragment can be added to an Activity the following way:

public class ChatListActivity extends AppCompatActivity {
        ...
        
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    FragmentManager fragmentManager = getSupportFragmentManager();
	    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
	
	    ChatListFragment chatListFragment = new ChatListFragment();
	    fragmentTransaction.add(R.id.fragment_container, chatListFragment);
	    fragmentTransaction.commit();
	}
	
	...
	
}

Any chats you create or join will be automatically shown by the ChatListFragment.

Handling chat clicks

Once you have implemented the ChatListFragment you can listen for clicks on the elements of the chat list. To do this you must implement the OnChatClickListener interface in your chat list Activity. This interface listens for clicks on the chats in the list:

public class ChatListActivity extends AppCompatActivity 
                               implements OnChatClickListener {
                                       
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    // The ChatListFragment initialisation goes here...
	    
	    chatListFragment.setOnChatClickListener(this);
	}
	
	...
	
}

Next, you must override the onChatClick method:

@Override
public boolean onChatClick(@NonNull String chatId, @Nullable String chatName) {
    return true;
} 

Notice that the overridden method has a Boolean type return value. This has to do with the default user avatar click behaviour provided by the ChatListFragment, which opens a conversation Activity for that chat automatically. For this to happen you must first provide a conversation Activity. If you have not done it already, refer to the Initial setup section for instructions on how achieve this.

If you want to keep the default behaviour simply return true in the overridden method. If you wish to replace this behaviour with your own functionality, simply return false in the overridden method.