I have a DrawView class that inherits from View, where a red line is drawn. How to make, what after pressing the button one more line would appear (canvas.drawLine (200, 500, 500, 500, paint))?

DrawView class:

public class DrawView extends View { Paint paint = new Paint(); public DrawView(Context context) { super(context); paint.setColor(Color.RED); } @Override public void onDraw(Canvas canvas) { canvas.drawLine(200, 500, 200, 800, paint); } } 

MainActivity class:

 public class MainActivity extends AppCompatActivity { DrawView drawView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawView = new DrawView(this); drawView.setBackgroundColor(Color.WHITE); LinearLayout container = (LinearLayout) findViewById(R.id.conteiner); container.addView(drawView); Button important = (Button) findViewById(R.id.important); important.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(getApplicationContext(), "Button 1 clicked", Toast.LENGTH_LONG).show(); } }); } } 

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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.attracti.redline.MainActivity"> <LinearLayout android:id="@+id/conteiner" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/important"> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Draw" android:id="@+id/important" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> </RelativeLayout> 

    2 answers 2

    1) Add the drawLine method to DrawView and call invalidate () at the end of the method ; From the documentation:

    To force a view to draw, call invalidate ().

    2) When pressing the button, call DrawView.drawLine ();

    And so the link is a good example.

       public class DrawView extends View { Paint paint = new Paint(); public DrawView(Context context, int x1, int y1, int x2, int y2) { super(context); paint.setColor(Color.RED); } @Override public void onDraw(Canvas canvas) { canvas.drawLine(x1, y1, x2, y2, paint); } } 

      Add:

       important.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { drawView = new DrawView(this, 200, 500, 500, 500); drawView.setBackgroundColor(Color.WHITE); container.addView(drawView); } }); 
      • Mmm ... Your method is not very usable and scalable. This constantly creates a new instance of the class DrawView - vanyamelikov
      • @vanyamelikov mm, agree) - Android Android