// RedSquareActivity.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2012-10-11 File created. // 2015-03-16 Latest modification. /* See the comments in SimpleIntentsActivity.java to find out how to use this file. Notes: setPadding() did not work for a View ???? */ package simple.intents ; import android.app.Activity; import android.os.Bundle; import android.view.View ; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; import android.content.Context ; import android.graphics.* ; // Classes Canvas, Color, Paint, RectF, etc. public class RedSquareActivity extends Activity { class RedSquareView extends View // An inner class. { public RedSquareView( Context context ) { super( context ) ; } protected void onDraw( Canvas canvas ) { Paint filling_paint = new Paint() ; filling_paint.setStyle( Paint.Style.FILL ) ; filling_paint.setColor( Color.RED ) ; int view_width = getWidth() ; int square_height = getHeight() - 40 ; canvas.drawRect( view_width / 2 - square_height / 2, 20, view_width / 2 + square_height / 2, square_height + 20, filling_paint ) ; } } public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ) ; ScrollView scroll_view = new ScrollView( this ) ; LinearLayout linear_layout = new LinearLayout( this ) ; linear_layout.setOrientation( LinearLayout.VERTICAL ) ; scroll_view.addView( linear_layout ) ; TextView textview = new TextView( this ) ; textview.setText( "We are inside a RedSquareActivity ..." ) ; linear_layout.addView( textview ); RedSquareView red_square_view = new RedSquareView( this ) ; red_square_view.setMinimumHeight( 240 ) ; linear_layout.addView( red_square_view ) ; Button button = new Button( this ) ; button.setText( "Finish this activity!" ) ; button.setOnClickListener( new View.OnClickListener() { public void onClick( View view ) { RedSquareActivity.this.finish() ; } }); linear_layout.addView( button ) ; this.setContentView( scroll_view ) ; } }