Search code examples
androidandroid-layoutheighttextviewandroid-textattributes

Android: can't change textView height value to bigger one on runtime


I have a Fragment which layout contains a TextView in a LinearLayout. I want on Runtime to change dynamically the height of the textView, and i get successfully to change it to a lower value than the originally, it seems to work. But when I put an upper value, the textView only adjust its height at most to parent height. (i've checked parent height and its value is always 592)

fragment_layout.xml

<LinearLayout
        android:id="@+id/af_content"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/mTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:singleLine="false"
            android:text="@string/text"
            android:textSize="@dimen/tv_size" />

    </LinearLayout>

FrgmentClass.java

TextView mTv = (TextView) rootView.findViewById(R.id.mTextView);

int newHeight = value;

mTv.setHeight(value);

If value is a low value like 200 it works, but for a bigger value like 1000 it doesn't, textview only resizes to parent height at most.

I've tried this too but not results with any value, although lower or upper than original value.

mTv.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);

// New measure values
int widthSpec = View.MeasureSpec.makeMeasureSpec(LinearLayout.LayoutParams.WRAP_CONTENT,  View.MeasureSpec.UNSPECIFIED);

int heightSpec = View.MeasureSpec.makeMeasureSpec(1200, View.MeasureSpec.EXACTLY);

mTv.measure(widthSpec, heightSpec);

i've tried using invalidate(), requestLayout() and refreshDrawableState() but the problem presists.

EDIT 1

I've setting up textView height value static in xml file, longer than his parent and it works (although i would like to set it up dynamically)

android:layout_height="1000dp"

So textView looks like this the layout:

fragment_layout.xml

<LinearLayout
        android:id="@+id/af_content"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/mTextView"
            android:layout_width="wrap_content"
            android:layout_height="1000dp"
            android:gravity="center"
            android:singleLine="false"
            android:text="@string/text"
            android:textSize="@dimen/tv_size" />

    </LinearLayout>

But now on runtime i set up scrolling method to textview and it does nothing, and not scrolling:

FrgmentClass.java

TextView mTv = (TextView) rootView.findViewById(R.id.mTextView);

mTv.setMovementMethod(new ScrollingMovementMethod());

Why it not scrolls?


Solution

  • Put the linearLayout in scrollview like this:

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true" >
    
        <LinearLayout
            android:id="@+id/af_content"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/mTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:singleLine="false"
                android:text="@string/text"
                android:textSize="@dimen/tv_size" />
        </LinearLayout>
    
    </ScrollView>