1

I've created a class which i use to draw some graphs, the class extends View, and it looks just like here How to draw a line in android.

Inside an activity, I show it with this

    drawView = new DrawView(this);
    drawView.setBackgroundColor(Color.WHITE);
    setContentView(drawView);

But when I add the ScrollView in the LinearLayout in xml of the activity, it doesn't work, I mean I can't scroll down, like there is no ScrollView. What may be the problem?

And this is the xml code

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_image"
android:orientation="vertical" >

</LinearLayout>
</ScrollView>
Community
  • 1
  • 1
Vladimir
  • 1,193
  • 5
  • 17
  • 23

2 Answers2

1

This is because you're adding two views using different methods: the one that the system inflates it from xml layout and the one that's created inside the activity.
This way, the content from xml layout is discarded, when you invoke setContentView(drawView);
Better said, is replaced with the DrawView created in the OnCreate method.
You should inflate the xml layout in your activity:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ScrollView container = getLayoutInflater().inflate(R.layout.main_layout, null));
setContentView(drawView);
drawView = container.findViewById(R.id.my_draw_view);
drawView.setBackgroundColor(Color.WHITE);  
}

And the activity layout should look like this:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<com.yourcompany.DrawView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>
</ScrollView>


If it helps, don't be shy to accept it or +1 it.

bazyle
  • 705
  • 6
  • 13
  • I have problems with drawView = container.findViewById(R.id.my_draw_view); I mean I can't define my DrawView in xml correctly – Vladimir Dec 27 '12 at 15:45
  • @Vladimir have you created the class DrawView as in this [link](http://stackoverflow.com/questions/3616676/how-to-draw-a-line-in-android) ? – bazyle Jan 09 '13 at 07:47
0

You can create a ScrollView programmatically and add the DrawView as a child to it. And later set the ScrollView as content for the Activity. Something like this:

drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);

ScrollView sv = new ScrollView(this);
sv.setLayoutParams( new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT ) );
scrollView.addView( drawView );

setContentView(sv);
STT
  • 264
  • 4
  • 11