Created
July 3, 2023 14:07
-
-
Save Teepheh-Git/4d28e8a41a5bed24e10544577a9632d8 to your computer and use it in GitHub Desktop.
chat eg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- item_chat_message.xml --> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="8dp"> | |
<TextView | |
android:id="@+id/text_message" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textSize="16sp" | |
android:background="@drawable/message_background" | |
android:padding="8dp" | |
android:layout_marginStart="8dp" | |
android:layout_marginEnd="8dp"/> | |
</LinearLayout> | |
<!-- message_background.xml --> | |
<shape xmlns:android="http://schemas.android.com/apk/res/android"> | |
<solid android:color="#EEEEEE"/> | |
<corners android:radius="8dp"/> | |
</shape> | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import androidx.annotation.NonNull; | |
import androidx.recyclerview.widget.RecyclerView; | |
import java.util.List; | |
public class ChatMessageAdapter extends RecyclerView.Adapter<ChatMessageAdapter.ViewHolder> { | |
private List<String> chatMessages; | |
public ChatMessageAdapter(List<String> chatMessages) { | |
this.chatMessages = chatMessages; | |
} | |
@NonNull | |
@Override | |
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_message, parent, false); | |
return new ViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { | |
String message = chatMessages.get(position); | |
holder.textMessage.setText(message); | |
} | |
@Override | |
public int getItemCount() { | |
return chatMessages.size(); | |
} | |
public static class ViewHolder extends RecyclerView.ViewHolder { | |
TextView textMessage; | |
public ViewHolder(View itemView) { | |
super(itemView); | |
textMessage = itemView.findViewById(R.id.text_message); | |
} | |
} | |
} | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.recyclerview.widget.LinearLayoutManager; | |
import androidx.recyclerview.widget.RecyclerView; | |
import android.os.Bundle; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ChatActivity extends AppCompatActivity { | |
private RecyclerView recyclerView; | |
private ChatMessageAdapter adapter; | |
private List<String> chatMessages; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_chat); | |
recyclerView = findViewById(R.id.recycler_view); | |
recyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
chatMessages = new ArrayList<>(); | |
// Add some sample chat messages to the list | |
chatMessages.add("Hello!"); | |
chatMessages.add("How are you?"); | |
chatMessages.add("I'm doing great. Thanks for asking!"); | |
adapter = new ChatMessageAdapter(chatMessages); | |
recyclerView.setAdapter(adapter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment