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>