I want to set array items in a chat type interface but I don't know how to do that. I want to load data on a view I created for the sender and receiver just like any chatting application every time I clicked anywhere on the screen. But every time I click on the screen the previous value change with new one but I want every new message load on a new view. please note that I have to load the message from the array I created In code. I hope I'm able to explain the problem.
this is what I get(dark background image)
this is what I want(light background image),
here is my code
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.FragmentManager;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;
import Fragment.InternetTest;
public class ParentActivity extends AppCompatActivity {
private ConstraintLayout parentLayout;
private String checker = "";
String[] messageArrayOne = {"Hello", "How are you.", "I am doing fine.", "What about you.",
"Hello","How are you.", "I am doing fine.", "What about you.","Hello", "How are you.",
"I am doing fine.","What about you.","Hello", "How are you.", "I am doing fine.",
"What about you.","Hello","How are you.","I am doing fine.", "What about you.",
"Hello", "How are you.", "I am doing fine.","What about you.","Hello",
"How are you.", "I am doing fine.", "What about you."};
private TextView sender, receiver;
int i = 0;
int j = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parent);
sender = findViewById(R.id.messageTv);
receiver = findViewById(R.id.messageTvRec);
parentLayout = findViewById(R.id.parent_layout);
parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkConnectivity();
if (checker.equals("notConnected")) {
FragmentManager fragmentManager = getSupportFragmentManager();
InternetTest internetTest = new InternetTest();
fragmentManager.beginTransaction().replace(R.id.parent_layout, internetTest).commit();
}
else if (checker.equals("connected")) {
loadNewMessage();
}
}
});
}
private void loadNewMessage() {
if (i < messageArrayOne.length) {
sender.setText(messageArrayOne[i]);
i++;
}
else {
Snackbar snackbar=Snackbar.make(parentLayout,"No new Message.", Snackbar.LENGTH_SHORT);
snackbar.show();
}
}
private void checkConnectivity() {
ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
checker = "notConnected";
} else {
checker = "connected";
}
}
}
This is my XML design file with name row_chat_left and I want to add new next on it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/messageLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/messageTvRec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/bg_sender"
android:padding="15dp"
android:text="Received Message "
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>