0

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.

enter image description here

this is what I get(dark background image)

enter image description here

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>
Akshay Rajput
  • 250
  • 4
  • 11

1 Answers1

1

You have only two TextViews (sender, receiver).

In loadNewMessage() you are just overwriting sender TextView value.

You might fix that by for example adding new TextView dynamically to your layout with next String from your array.

For example:

LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("test");
yourLayout.addView(tv);

Edit: I will add update here as you update your post and added comment.

I'll try to point you in the right direction. For list like chat you should not use ConstraintLayout as this might start getting really long, so you want your view to be scrollable, fast and not keep views that are not visible in memory. You should learn about RecyclerView -> https://developer.android.com/guide/topics/ui/layout/recyclerview

This will allow you to display all your messages in form of a scrollable list. Then you might want to work on displaying messages for receiver/sender in a appropriate places left/right. For that you might want to create RecyclerView with multiple view types.(One type for receiver, one type for sender). Here is stackoverflow post on how to do that. -> How to create RecyclerView with multiple view type?

Happy coding :)

If it helped please mark as answered.

Sebastian M
  • 629
  • 7
  • 17
  • 2
    hello sir, Thanks for answering I just edit my question so that you see my XML file on which I want to add the new message. please take a look again And guide me for showing the new text message with that XML file – Akshay Rajput May 21 '20 at 13:54
  • 1
    @Ashkaj Rajput I eddited my post above. To much characters for a comment. – Sebastian M May 21 '20 at 14:20
  • 1
    thank you so much I try everything you suggest to me but I don't have any idea how to put items of array into a recycler view I try my best. and I hope if I don't find how to do that you will help me. thanks – Akshay Rajput May 21 '20 at 15:01
  • Checkout this tutorial -> https://guides.codepath.com/android/using-the-recyclerview This is realy detailed version on how to run use RecyclerView. – Sebastian M May 21 '20 at 15:44