/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. package com.javalessons.android.simplestandroid; 

2.  

3. import java.util.Random

4.  

5. import android.app.Activity

6. import android.graphics.Color

7. import android.os.Bundle

8. import android.view.Gravity

9. import android.view.View

10. import android.view.View.OnClickListener

11. import android.widget.Button

12. import android.widget.LinearLayout

13. import android.widget.Toast

14.  

15. public class MainActivity extends Activity

16.   LinearLayout  ll; 

17.   Button        bt; 

18.   int            nr  = 0

19.  

20.   @Override 

21.   protected void onCreate(Bundle savedInstanceState) { 

22.     super.onCreate(savedInstanceState); 

23.  

24.     ll = new LinearLayout(this); 

25.     ll.setOrientation(LinearLayout.VERTICAL); 

26.     ll.setBackgroundColor(0xFF00FF00); 

27.   

28.     bt = new Button(this); 

29.     bt.setText("Come on, click me, you rascal..."); 

30.     bt.setTextSize(20); 

31.  

32.     bt.setOnClickListener(  

33.      new OnClickListener() { 

34.       public void onClick(View v) { 

35.         Random rnd = new Random(); 

36.         int c = Color.argb( 255, rnd.nextInt(256), 0, rnd.nextInt(128) + 128 ); 

37.         ll.setBackgroundColor(c); 

38.         Toast tt =  

39.             Toast.makeText( getApplicationContext(), "Button clicked " + (++nr), 

40.                 Toast.LENGTH_SHORT ); 

41.         tt.setGravity(Gravity.TOP, 0, 80); 

42.         tt.show(); 

43.       } 

44.     }); 

45.  

46.     ll.addView(bt); 

47.     setContentView(ll); 

48.   } 

49.