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.
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.