java – Click on the window marker and go to the ChatFragment of the user who put the marker

When I click on the marker and after click on the pop-up window I need to be able to go into the chat of who put the marker and then start chatting and the marker of the logged user must not be clickable. How can I modify my code to make this thing work?

I understand that I need to use onInfoWindowClick but I don’t know how to use it. I should go to ChatFragment

ChatFragment

public class ChatsFragment extends Fragment {

    private RecyclerView recyclerView;
    private ChatAdapter chatAdapter;
    private List<User> mUsers;

    private FirebaseUser firebaseUser;
    private DatabaseReference reference;

    private Stack<Chatlist> usersList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chats, container, false);

        recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        usersList = new Stack<>();

        
        if (firebaseUser != null) {
            Query reference1 = FirebaseDatabase.getInstance().getReference("Chatlist").child(firebaseUser.getUid()).orderByChild("time");
            // Get every user from chatlist reference
            reference1.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    usersList.clear();
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        Chatlist chatlist = snapshot.getValue(Chatlist.class);
                        usersList.push(chatlist);
                    }
                    Collections.reverse(usersList);
                    chatList();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
        FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
            @Override
            public void onComplete(@NonNull Task<String> task) {
                if (task.isSuccessful()) {
                    String mtoken = task.getResult();
                    updateToken(mtoken);
                    Log.d("TAG", mtoken); //Check the generated token.
                } else {
                    Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
                }
            }
        });

        registerForContextMenu(recyclerView);

        return view;
    }

    private void updateToken(String token) {
        if (firebaseUser != null) {
            DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Tokens");
            Token token1 = new Token(token);
            reference.child(firebaseUser.getUid()).setValue(token1);
        }
    }

    // Show users with whom you have talked
    private void chatList() {
        mUsers = new ArrayList<>();
        reference = FirebaseDatabase.getInstance().getReference("Users");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mUsers.clear();
                // For every user check their id and if it matches with chatlist id add them to the list
                for (Chatlist chatlist : usersList) {
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        User user = snapshot.getValue(User.class);
                        if (chatlist.getId().equals(user.getId())) {
                            mUsers.add(user);
                        }
                    }
                }
                // Show them with the recycler view
                chatAdapter = new ChatAdapter(getContext(), mUsers);
                recyclerView.setAdapter(chatAdapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        int position = -1;
        try {
            position = ((ChatAdapter) recyclerView.getAdapter()).getPosition();
        } catch (Exception e) {
            Log.d(TAG, e.getLocalizedMessage(), e);
            return super.onContextItemSelected(item);
        }
        final User user = mUsers.get(position);
        final FirebaseUser fuser = FirebaseAuth.getInstance().getCurrentUser();
        switch (item.getItemId()) {
            case R.id.del_friend:
                FirebaseDatabase.getInstance().getReference("Friends").child(fuser.getUid()).child(user.getId()).removeValue();
                FirebaseDatabase.getInstance().getReference("Friends").child(user.getId()).child(fuser.getUid()).removeValue();
            case R.id.deleting:
                DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Chats");
                reference.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        for (DataSnapshot deleteSnapshot: dataSnapshot.getChildren()) {
                            Chat chat = deleteSnapshot.getValue(Chat.class);
                            assert chat != null;
                            if (chat.getReceiver() != null && chat.getSender() != null) {
                                if (chat.getReceiver().equals(fuser.getUid()) && chat.getSender().equals(user.getId()) ||
                                        chat.getReceiver().equals(user.getId()) && chat.getSender().equals(fuser.getUid())) {
                                    if (chat.getDeletedfrom().equals("none")) {
                                        HashMap<String, Object> hashMap = new HashMap<>();
                                        hashMap.put("deletedfrom", fuser.getUid());
                                        deleteSnapshot.getRef().updateChildren(hashMap);
                                    } else if (chat.getDeletedfrom().equals(user.getId())) {
                                        deleteSnapshot.getRef().removeValue();
                                    }
                                }
                            }
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            case R.id.archiving:
                FirebaseDatabase.getInstance().getReference("Chatlist").child(fuser.getUid()).child(user.getId()).removeValue();
                break;

        }
        return super.onContextItemSelected(item);
    }

Maps with marker

 public class TrovamicoActivity extends FragmentActivity implements 
  OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener {

public static final String TAG = "TrovamicoActivity";

private FragmentsTrovamicoBinding binding;
LatLng latLng;

private DatabaseReference reference;
private FirebaseUser firebaseUser;

List<String> addresses, genders;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = FragmentsTrovamicoBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    Log.d(TAG, "String address: " + addresses);
    addresses = new ArrayList<>();
    genders = new ArrayList<>();

    firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    Log.d(TAG, "String User: " + firebaseUser.getUid());
    reference = FirebaseDatabase.getInstance().getReference().child("Users");

    ValueEventListener eventListener = new ValueEventListener() {
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot a : snapshot.getChildren()) {
                String address = a.child("address").getValue(String.class);
                addresses.add(address);

                String gender = a.child("gender").getValue(String.class);
                genders.add(gender);
            }
            genders.removeIf(Predicate.isEqual(null));
            addresses.removeIf(Predicate.isEqual(null));

            Log.d(TAG, "onDataChange: address: " + addresses);
            Log.d(TAG, "onDataChange: genders: " + genders);

            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(TrovamicoActivity.this);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            throw error.toException();
        }
    };


    reference.addListenerForSingleValueEvent(eventListener);

   

}

@Override
public void onMapReady(GoogleMap googleMap) {


    //Toast.makeText(this, "size" + addresses.size(), Toast.LENGTH_SHORT).show();
    try{
        for (int i = 0; i < addresses.size(); i++) {
            String user_address = addresses.get(i);
            Log.d(TAG, "onMapReady: position: " + i);
            if (user_address == null) continue;
            if (!user_address.equals("To be added")) {
                String user_gender = genders.get(i);
                latLng = getLocationFromAddress(user_address);
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(latLng);
                markerOptions.title("You");
                markerOptions.icon(BitmapFromVector(getApplicationContext(), (user_gender.equals("Male")) ? R.drawable.male : R.drawable.female));
                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 5));
                googleMap.addMarker(markerOptions);
            }
        }
    }catch (Exception e){
        e.printStackTrace();
    }

    googleMap.setOnInfoWindowClickListener(this);


public LatLng getLocationFromAddress(String strAddress) {

    Geocoder coder = new Geocoder(this);
    List<Address> address;
    LatLng p1;

    try {
        address = coder.getFromLocationName(strAddress, 5);
        if (address == null) {
            return null;
        }
        Address location = address.get(0);
        p1 = new LatLng(location.getLatitude(), location.getLongitude());

        return p1;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}


@Override
public void onInfoWindowClick(@NonNull Marker marker) {
  
}
}

Leave a Comment