0

How to create multiple text views at run time in multiple rows and column? I have inflated a linear layout and created the text views using for loop. Text Views were created successfully, but i'm facing the issue that all created text views are only in single row. I tried to set it with the LayoutParams also, but cant fixed it. How to fix this? Below is my code

 LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    appointmentSlotList = appointmentSlot.getAppointmentSlots();
    if(appointmentSlotList != null && appointmentSlotList.size()>0){
        for(int i = 0; i<appointmentSlotList.size(); i++){
            View appointmentInflater = layoutInflater.inflate(R.layout.appointment_time, null);


            TextView lblDate = (TextView) appointmentInflater.findViewById(R.id.appointmentTime);
            lblDate.setText(appointmentSlotList.get(i));
            lblDate.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            //lblDate.setLayoutParams(lparams);
            lblDate.setOnClickListener(onclickTime);
            try {
                //if(previousSelected!=i)
                lnrDateContainer.addView(appointmentInflater);


            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

and my output is shown below:

Output for the inflated text views shown below the date picker

Viswa Sundhar
  • 121
  • 3
  • 16

4 Answers4

0

Set the textview android layout gravity to fill

android:layout_gravity="fill"

If above wont work then refer to this question : Android multi line linearlayout

Community
  • 1
  • 1
Hassan Munir
  • 419
  • 7
  • 24
0

As you want to add TextView in rows and columns, you can use TableLayout and add TextView in it pragmatically

Check this answer : https://stackoverflow.com/a/16939325/5345482

Community
  • 1
  • 1
Akshay Panchal
  • 695
  • 5
  • 15
0

To achieve these I think FlowLayout library might be fit to your requirements. With this lib you can create your TextViews in a single line and when doesn't have space then auto inserts the view in the next line.

The link: https://github.com/ApmeM/android-flowlayout

I hope helps you!

Kabuki
  • 147
  • 4
  • 10
0

Specify the orientation in your Linear Layout as VERTICAL

<LinearLayout
----
android:orientation="vertical"
----  />
MUKUND U
  • 44
  • 6