-1

I want to build an app that will generate several textview which will all fit in the designated layout. but once the line is full instead of continue in new line it not show the rest of the textviews

my MainActivity.java:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayList<String> urls=new ArrayList<String>(); //to read each line
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.sell);
    // Add textview 1
    for (int i=0;i<10;i++){
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams1.setMargins(10, 10, 10, 10);
        TextView textView1 = new TextView(this);
        textView1.setLayoutParams(layoutParams1);
        textView1.setText("TextView1\n2");
        textView1.setBackgroundColor(0xFF3EB427); // hex color 0xAARRGGBB
        textView1.setPadding(10, 10, 10, 10);// in pixels (left, top, right, bottom)
        linearLayout.addView(textView1);

    }
    LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.buy);
    LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    for (int i=0;i<2;i++){

        layoutParams2.setMargins(10, 10, 10, 10);
        TextView textView1 = new TextView(this);
        textView1.setLayoutParams(layoutParams2);
        textView1.setText("TextView2");
        textView1.setBackgroundColor(0xFFB60612); // hex color 0xAARRGGBB
        textView1.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
        linearLayout2.addView(textView1);
    }
}
}

my xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="@+id/mymain"
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"
tools:context="com.example.ohad.tradertracker.MainActivity"
android:weightSum="1">

<LinearLayout
    android:id="@+id/stats"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="101dp">

</LinearLayout>


<LinearLayout
    android:id="@+id/sell"
    android:layout_weight=".5"
    android:fillViewport="true"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

and it looks like : enter image description here

How can i generate unknown amount of textview and use all layout size???

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ohad Gerrassy
  • 116
  • 11
  • This is posibble duplicate question [Check](https://stackoverflow.com/q/2827079/6099173). If that is not your question check this another [This!](https://stackoverflow.com/a/31855704/6099173) – K. Jorge Sep 22 '17 at 22:08
  • The inflating the layout from Java would be a bad performance if the number of contents is going up. The RecyclerView is the recommended way. I don't know whose this blog is, but I think enough to start. https://android--code.blogspot.com/2015/12/android-recyclerview-grid-layout-example.html Official: https://developer.android.com/training/material/lists-cards.html?hl=ja – neonankiti Sep 22 '17 at 23:24

1 Answers1

0

I think there is a problem in your xml layout. Do it like this and tell me if it works. You can do two things :

  1. Just remove the weightSum from the main layout and do it like this and mention the layout_weight in your both linear layout.
  2. Or you can mention the weightSum as 2 . and in both the layout ensure that you have mentioned the layout_weight as 1.

Alok
  • 8,452
  • 13
  • 55
  • 93