0

Been trying for almost a week to achieve a trivial scrollbar in a textview. I asked here several times and nobody produces working code. When I try this I get an NPE.

Want a scrollbar? Then we're going to force you to program XML, we're going to force you to wrap it in layout and we force you to use a scrollview and it still won't work. What happened to keeping it simple?

Place the TextView inside a ScrollView

https://developer.android.com/reference/android/widget/ScrollView.html

xml

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
</LinearLayout>

And then in the activity:

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

TextView textView = new TextView(this); textView.setText("You are in a dungeon."); textView.setMaxLines(4);

lin.addView(textView);

E/AndroidRuntime: FATAL EXCEPTION: main Process: dev.game.adventure, PID: 12085 java.lang.RuntimeException: Unable to start activity ComponentInfo{dev.game.adventure/dev.game.adventure.FullscreenActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference at dev.game.adventure.FullscreenActivity.onCreate(FullscreenActivity.java:157) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  Disconnected from the target VM, address: 'localhost:8785', transport: 'socket'

Then I try the following to get a bloody IllegalStateException when all I'm trying to do is achieve a simple scrollbar to a text.

  this.setContentView(R.layout.activity_fullscreen);

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

        TextView textView = new TextView(this);
        textView.setText("You are in a dungeon.");
        textView.setMaxLines(4);

        lin.addView(textView);

java.lang.RuntimeException: Unable to start activity ComponentInfo{dev.game.adventure/dev.game.adventure.FullscreenActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:4915)

Then I try this code and naturally it is not working. My Android game starts with out the simple scrollbar and my textview overflows the game image. Why would anybody want this behavior as default? Why would anybody need overflow as default behavior? I want my money back for Android when it can't even render a simple scrollbar.

enter image description here

Then I try this.

LinearLayout lin = new LinearLayout(this);

tbl.addView(tr);
TableRow tr2=new TableRow(this);


TextView textView = new TextView(this);
textView.setText("You are in a dungeon.");
textView.setMaxLines(4);

lin.addView(textView);
ScrollView sv = new ScrollView(this);

tr2.addView(sv);
TableRow.LayoutParams params = (TableRow.LayoutParams) sv.getLayoutParams();
params.span = 6; //amount of columns you will span
sv.setLayoutParams(params);

tbl.addView(tr2);

And now I can prove that it doesn't work either. Now there is no textview at all. And no comment can change this.

enter image description here

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • 2
    what exactly you want ? you want scroll to be enabled in a textview or you want the linear layout to be scroll-able? – Preethi Rao Nov 21 '17 at 06:39
  • @PreethiRao I'm updating a textview and I want it to scroll instead of destroying my entire layout. Destroying the entire layout is the default faulty behavior of this textview. Scrolling should be default behavior. – Niklas Rosencrantz Nov 21 '17 at 06:40
  • are you wrapping your layout inside scrollview ? – Preethi Rao Nov 21 '17 at 07:08
  • @PreethiRao I tried that too. Nothing works. Just look at how many questions on this site try to solve this problem. I can't use XML. – Niklas Rosencrantz Nov 21 '17 at 14:45

1 Answers1

0

Want a scrollbar? Then we're going to force you to program XML, we're going to force you to wrap it in layout and we force you to use a scrollview and it still won't work. What happened to keeping it simple?

What I understand from this is that you simply want to make your TextView scrollable with this complicated solution which is not working in your case.

You don't need to do all that to add scroll in your TextView just refer this answer.

My implementation,

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:text="@string/very_long_text"
        android:maxLines="3"
        android:layout_centerInParent="true"
        android:scrollbars="vertical" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView myTextView = findViewById(R.id.text_view);
        myTextView.setMovementMethod(new ScrollingMovementMethod());
    }
}

Screen Shot

Hope this helps.

Rohit
  • 103
  • 4