-2

Basically, I read messages from firebase database one by one in a loop after which, I want to add each message to the linear layout.

I have a Linear Layout XML file like this called my_linear_layout IMAGE SAMPLE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/layout2"
android:layout_marginTop="20dp"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_weight="6"
    android:background="@drawable/rounded_corner"
    android:padding="16dp"
    android:text="MESSAGE GOES HERE. IT CAN BE A LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG MESSAGE"
    android:textColor="#000" />


<ImageView

    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginTop="0dp"
    android:layout_weight="3"
    android:src="@drawable/senduser" />

</LinearLayout>

Now, want to inflate this layout in my Activity's Relative Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
tools:context=".BuyerChat">

<ScrollView
    android:layout_width="match_parent"
    android:layout_weight="20"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">


    </RelativeLayout>
</ScrollView>

<include
    layout="@layout/type_message_area"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:gravity="bottom" />
</LinearLayout>

I can not just add the code here because I want to have n number of linear layouts, depending on how many messages I have. Basically, I read messages from firebase database after which, I want to add each message to the linear layout.

So, I want a code snippet that will allow me to add a linear layout everytime I call it and place a new message in linear layout every time.

I have been at this for quite some time, please help.

theBrainyGeek
  • 584
  • 1
  • 6
  • 17
  • What had you tried so far? – Selvin Feb 09 '17 at 02:39
  • @Selvin I tried the method shown here:http://androidexample.com/Dynamically_Create_View_Elements__-_Android_Example/index.php?view=article_discription&aid=115 But, I don't know how to set layout and text view width as wrap_content or match_parent through java. – theBrainyGeek Feb 09 '17 at 02:41

2 Answers2

1

You can do like this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:context=".BuyerChat">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_weight="20"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/dynamic_holder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">


        </LinearLayout>
    </ScrollView>

    <include
        layout="@layout/type_message_area"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="bottom" />
    </LinearLayout>

And in your activity:

LinearLayout dynamicHolder = (LineraLayout) findViewById(R.id.dynamic_holder);

for(int i = 0; i<your_message_length; i++){
    View dynamicView = LayoutInflater.from(context).inflate(R.layout.my_linear_layout, null, false);
    TextView yourTextView = (TextView) dynamicView.findViewById(R.id.your_tv_id);//give id to your textview in my_linear_layout
    youtTextView.setText(your_each_message_from_db);

    dynamicHolder.addView(dynamicView);
}
0

Why are you using linear layout instead of listview.
ListView is strongly recommended in this case.

See here how efficient are listViews compared to linearLayout.
Plus its easier to use.
ListViews do not inflate all items, they inflate however many can fit on a page at a time. Your fastest option, with or without paging is a ListView.

Please see here how to get started with the listView for chat application.
Simple chat application using listview in android

I would suggest you to create Custom adapter for ListView. You just need to check the condition to change the background of layout/views inside getViews() method.

Some thread may be helpful to you:

The World of List View
Android Implementing Chat Bubble in ListView

Community
  • 1
  • 1
Salmaan
  • 3,543
  • 8
  • 33
  • 59