2

Tried to build and add a customview on a ScrollView. But it wont Scroll.

While overriding onMeasure - it's not recognized.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

</ScrollView>
//tried above making as child.

Code:

ScrollView scrollView = findViewById(R.id.scrollView);
//LinearLayout linearLayout = findViewById(R.id.line);

myView gw = new myView(this,bitmap);
// ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(480,800);
scrollView.addView(gw);
scrollView.setFillViewport(true);

myView Class draws bitmap using this code:

public class myView extends SurfaceView implements View.OnTouchListener {

    private Bitmap bitmap;

    private SurfaceHolder holder;

    public myView(Context context, Bitmap bitmap) {

        super(context);

        holder = getHolder();

        this.bitmap = bitmap;

 }
         .....

    @Override

    public void draw(Canvas canvas) {

        super.draw(canvas);

  canvas.drawBitmap(bitmap, 10, 20, null);


        canvas.drawPath(path, paint);


        canvas.drawText("Second floor", x, y + 600, paint1);

        Log.d("test", "drawing....");


    }

View draws but wont scroll. How to make it scroll?

Rifat
  • 1,700
  • 3
  • 20
  • 51

1 Answers1

0

Wrap your view with some ViewGroup like LinearLayout or FrameLayout:

LinearLayout linear = new LinearLayout(this);
linear.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
linear.setOrientation(1);
scrollView.addView(linear);

myView gw = new myView(this,bitmap);
linear.addView(gw );

EDIT:

From your code I see that you are using SurfaceView. SurfaceView is very different and does not support scrolling from ScrollView. You can change the view from SurfaceView to something else or add:

implements View.OnTouchListener, SurfaceHolder.Callback

and implement scrolling by yourself.

See: Android SurfaceView scrolling

muminers
  • 1,190
  • 10
  • 19
  • I tried this but doesn't scroll. Also tried this using XML. but this canvas object wont scroll. – Rifat Mar 26 '18 at 13:46
  • It has this callback added to holder. Should I implement it? - holder.addCallback(new SurfaceHolder.Callback() {...} – Rifat Mar 26 '18 at 14:28
  • Yes exactly - as described in the quoted answer. – muminers Mar 26 '18 at 14:29
  • Tested with this, drawing the view and seeing the half image but still not scrolling. However onTouchListener working on this since begining. – Rifat Mar 26 '18 at 14:58
  • Sorry to say but using this solution not working scroll is just your implementation fault since you have to provide it by yourself. Linked question shows exactly how to do this. Are you sure you've done everything correctly? – muminers Mar 26 '18 at 15:01
  • I see the linked question. Its pointing a different approach. which is picking up x,y manually. – Rifat Mar 26 '18 at 15:11