public class ChatActivity extends AppCompatActivity {
private EditText edit_chat;
private ImageView image_go_message,imageView_dialog;
private Dialog chatdialog;
private DatabaseReference databaseReference;
private EditText textget;
private FirebaseAuth mAuth;
private RecyclerView recycle;
private ArrayList<String> messages=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
edit_chat=(EditText) findViewById(R.id.edit_chat);
imageView_dialog=(ImageView) findViewById(R.id.imageView_dialog);
image_go_message=(ImageView) findViewById(R.id.image_go_message);
mAuth = FirebaseAuth.getInstance();
textget=(EditText) findViewById(R.id.textget);
databaseReference = FirebaseDatabase.getInstance().getReference().child("User").child("d");
Adapter adapter=new Adapter(this,messages);
recycle=(RecyclerView) findViewById(R.id.recycle);
recyclemanwoman.setLayoutManager(new LinearLayoutManager(this));
///adapter///
recycle.setAdapter(adapter);
chatdialog=new Dialog(ChatActivity.this);
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
String msg=snapshot.getValue(String.class);
messages.add(msg);
adapter.notifyDataSetChanged();
recycle.smoothScrollToPosition(messages.size());
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
imageView_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chatdialogShow();
}
});
image_go_message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ValidateAndSaveOnlyInfo();
}
});
}
private void chatdialogShow() {
chatdialog.setContentView(R.layout.dialog_exit);
chatdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Button button_chat_end=chatdialog.findViewById(R.id.button_chat_end);
Button button_back_chat=chatdialog.findViewById(R.id.button_back_chat);
button_back_chat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chatdialog.dismiss();
}
});
button_chat_end.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
chatdialog.show();
}
private void ValidateAndSaveOnlyInfo() {
if (TextUtils.isEmpty(edit_chat.getText().toString())) {
Toast.makeText(this, "Введите текст", Toast.LENGTH_SHORT).show();
} else {
HashMap<String, Object> userMap = new HashMap<>();
userMap.put("uid", mAuth.getCurrentUser().getUid());
userMap.put("text", edit_chat.getText().toString());
databaseReference.child(mAuth.getCurrentUser().getUid()).updateChildren(userMap);
edit_chat.setText("");
}
}
}