Search code examples
androidandroid-layouthorizontalscrollview

linear layout not scrolling horizontally when populated programatically


I am working on a layout where i have to populate textView programmaticaly inside a vertical linear layout . But some of my textView contents are too wide for my screen size and they are not being visible . This is my layout

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

    <HorizontalScrollView
        android:layout_width="320px"
        android:layout_height="match_parent">


        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/diff_layout"
            android:layout_width="320px"
            android:layout_height="match_parent"
            android:orientation="vertical">

        </LinearLayout>
    </HorizontalScrollView>
</ScrollView>

And here is the code where i am programatically populating the linear layout :-

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.diff_layout);

    String[] splitLines = content.split("\n");
    for(int i=0;i<splitLines.length;i++){
        TextView textView = new TextView(this);
        textView.setSingleLine(true);
        textView.setLines(1);
        //textView.setHorizontallyScrolling(true);
        textView.setText(splitLines[i]);
        //textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT ));
        textView.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        /*textView.setMovementMethod(new ScrollingMovementMethod());
        textView.setHorizontallyScrolling(true);*/
        linearLayout.addView(textView);
    }

Please tell me where am i doing things wrong


Solution

  • After much hit and trial , i came up with the right solution :-

    <android.support.v4.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
    
        <HorizontalScrollView android:layout_height="match_parent"
            android:layout_width="match_parent"
            xmlns:android="http://schemas.android.com/apk/res/android">
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/diff_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
            </LinearLayout>
    
        </HorizontalScrollView>
    
    </android.support.v4.widget.NestedScrollView>
    

    and my java snippet :-

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.diff_layout);

        String[] splitLines = content.split("\n");
        for(int i=0;i<splitLines.length;i++){
            TextView textView = new TextView(this);
            textView.setText(splitLines[i]);
            textView.setMaxLines(1);
            textView.setMovementMethod(new ScrollingMovementMethod());
            linearLayout.addView(textView);
        }