-1

Programmatically created TextViews aren't appearing when I run my app. I am trying to download messages and the person that sent them from Firebase and display them in separate TextViews with the sender TextView on top. Sorry for the lack of information, but I don't know what I've done wrong.

db.collection("messagesIT")
                .orderBy("Timesent", Query.Direction.DESCENDING)
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot>task) {
                        if (task.isSuccessful()) {
                            ArrayList<MessageObj> messages = new ArrayList<>();
                            for (QueryDocumentSnapshot document : task.getResult()) 
                                String message = document.getString("Message");
                                String sender = document.getString("Sender");
                                Timestamp timesent = document.getTimestamp("Timesent");
                                MessageObj mess = new MessageObj(message, sender, timesent);
                                messages.add(mess);
                                Log.d(TAG, "m: " + mess.getMessage());
                                Log.d(TAG, "s: " + mess.getSender());
                                Log.d(TAG, "t:" + mess.getTimesent());
                                showToast("b");
                            }


                            TableLayout tl = findViewById(R.id.TableLayoutSubjectGroupPage);

                            //reading and displaying messages
                            for (int i = 0; i < messages.size(); i++) {
                                TableRow trMessage = new TableRow(SubjectGroupPage.this);
                                TableRow trSender = new TableRow(SubjectGroupPage.this);
                                TextView txtSender = new TextView(SubjectGroupPage.this);
                                TextView txtMessage = new TextView(SubjectGroupPage.this);
                                Log.d(TAG, "uv8iygv iyrtgcvfi8ytgvcrcccccccccccccccccccccccccccc");

                                LinearLayout.LayoutParams Weight = new LinearLayout.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1);

                                txtSender.setText(messages.get(i).getSender());
                                txtSender.setLayoutParams(Weight);


                                txtMessage.setText(messages.get(i).getMessage());
                                txtMessage.setLayoutParams(Weight);
                                txtMessage.setTextSize(20);
                                txtMessage.setTextColor(Color.parseColor("#000000"));

                                if (messages.get(i).getSender().equals(currentUser.getUid())) {
                                    txtMessage.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
                                    txtSender.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
                                } else {
                                    txtMessage.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
                                    txtSender.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
                                }

                                //creating message display
                                trSender.addView(txtSender);
                                trMessage.addView(txtMessage);
                                tl.addView(trSender);
                                tl.addView(trMessage);
                            }


                        } else {
                            Log.d(TAG, "Error getting documents: ", task.getException());
                        }
                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

                showToast("There has been an error, please try again later.");
                Log.d(TAG, "Error: " + e);

            }
        });

Layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/txtUsernameAdminStudentPage"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SubjectGroupPage">

<ScrollView
    android:id="@+id/scrollviewMessages"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@+id/linearLayoutMessageInput"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <TableLayout
        android:id="@+id/TableLayoutSubjectGroupPage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        //these were tests for how I want the TextViews

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/txtExample"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView"
                android:textColor="#000000"
                android:textSize="20dp" />
        </TableRow>


        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView"
                android:textAlignment="textEnd" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/txtExample2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView"
                android:textAlignment="textEnd"
                android:textColor="#000000"
                android:textSize="20dp"
                tools:layout_editor_absoluteY="675dp" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView7"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView"
                android:textAlignment="textEnd" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView"
                android:textAlignment="textEnd"
                android:textColor="#000000"
                android:textSize="20dp" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView"
                android:textColor="#000000"
                android:textSize="20sp" />
        </TableRow>

    </TableLayout>


</ScrollView>

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

What the page looks like when run

What it should look like when run

Database

S.Wilko
  • 11
  • 6

2 Answers2

0

I think you forget to add layout params for table row. BTW you should use recycler view with two view holders one for sender and 2nd for the receiver. checkout if you want to improve your code How to create RecyclerView with multiple view type? or https://medium.com/@droidbyme/android-recyclerview-with-multiple-view-type-multiple-view-holder-af798458763b

Salman
  • 9
  • 4
0

You explictly set the width of your TextViews to zero, using:

public LinearLayout.LayoutParams (int width, int height, float weight)

...where (see LinearLayout.LayoutParams):

int: the width, either ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT or a fixed size in pixels

MATCH_PARENT has a value of -1 and WRAP_CONTENT a value of -2. So this code sets a fixed width of 0:

LinearLayout.LayoutParams Weight = new LinearLayout.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1)

Instead you should use:

TableLayout.LayoutParams tableParams_forRow = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);

and

TableRow.LayoutParams rowParams_forTextView = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);

See this answer, for an example

Minding
  • 1,383
  • 1
  • 17
  • 29