0
votes

i am not using any layout so the all view is created by drawView class then how can i use scrollView in this scenario. so that i can able to scroll and view the whole line.

MainActivity.java

 public class MainActivity extends AppCompatActivity {
        DrawView drawView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            drawView = new DrawView(this);
            drawView.setBackgroundColor(Color.WHITE);
            setContentView(drawView);
        }
    }

    //this class will draw lines on screen.

    public class DrawView extends View {
        Paint paint = new Paint();
        Bitmap bmp;
        public DrawView(Context context) {
            super(context);
            paint.setColor(Color.BLACK);
            paint.setStrokeWidth(3f);
            bmp = BitmapFactory.decodeResource(getResources(),     R.drawable.station);

        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawLine(350,300,350,2500, paint);
            for (int i = 315; i< 995;i=i+20)
                canvas.drawLine(350,i,370,i,paint);
            canvas.drawLine(370,300,370,2500, paint);
            canvas.drawBitmap(bmp,350,300,paint);
            canvas.drawBitmap(bmp,300,500,paint);
        }
    }

//I also tried like this and when using this xml layout in that condition i have used

setContentView(R.layout.activity_main)

activity_main this is my xml layout file

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">

<com.example.shailendra.myapplication.DrawView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</ScrollView>  

Then i got following errors:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shailendra.myapplication/com.example.shailendra.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.shailendra.myapplication.DrawView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5233) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.shailendra.myapplication.DrawView at android.view.LayoutInflater.createView(LayoutInflater.java:616) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at android.view.LayoutInflater.inflate(LayoutInflater.java:365) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at com.example.shailendra.myapplication.MainActivity.onCreate(MainActivity.java:17) at android.app.Activity.performCreate(Activity.java:6001) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5233)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)  Caused by: java.lang.NoSuchMethodException: [class android.content.Context, interface android.util.AttributeSet] at java.lang.Class.getConstructor(Class.java:531) at java.lang.Class.getConstructor(Class.java:495) at android.view.LayoutInflater.createView(LayoutInflater.java:580) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)  at android.view.LayoutInflater.inflate(LayoutInflater.java:365)  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)  at com.example.shailendra.myapplication.MainActivity.onCreate(MainActivity.java:17)  at android.app.Activity.performCreate(Activity.java:6001)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5233)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)  **

1
add this view inside the scrollview like this stackoverflow.com/a/2799179/1576416Amrut Bidri
you cannot add view inside scrollview with using layout!Satish Silveri

1 Answers

-1
votes
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillViewport="true">
    Put draw View here
    </ScrollView>

You can add your drawView with yourDrawer.addView(yourDrawView)