/* 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.xml1; 

2.  

3. import android.app.Activity

4. import android.os.Bundle

5. import android.view.View

6. import android.view.View.OnClickListener

7. import android.widget.Button

8. import android.widget.TextView

9.  

10. public class MainActivity extends Activity

11.  

12.   Button bt; 

13.   TextView tv; 

14.    

15.   @Override 

16.   protected void onCreate(Bundle savedInstanceState) { 

17.     super.onCreate(savedInstanceState); 

18.     setContentView(R.layout.activity_main); 

19.  

20.     bt = (Button) findViewById(R.id.button1); 

21.     tv = (TextView) findViewById(R.id.textView1); 

22.      

23.     bt.setOnClickListener( new OnClickListener() { 

24.  

25.       @Override 

26.       public void onClick(View v) { 

27.         v.setBackgroundColor(0xff00ffff); 

28.         ( (View)bt.getParent() ).setBackgroundColor(0xffffff00); 

29.         bt.setEnabled(false); 

30.         tv.setText( String.valueOf( Math.random() )); 

31.       } 

32.     }); 

33.   } 

34. }    

35.  

36. ===== res/layout/activity_main.xml : ============ 

37. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

38.     xmlns:tools="http://schemas.android.com/tools" 

39.     android:id="@+id/LinearLayout1" 

40.     android:layout_width="match_parent" 

41.     android:layout_height="match_parent" 

42.     android:orientation="vertical" 

43.     android:paddingBottom="@dimen/activity_vertical_margin" 

44.     android:paddingLeft="@dimen/activity_horizontal_margin" 

45.     android:paddingRight="@dimen/activity_horizontal_margin" 

46.     android:paddingTop="@dimen/activity_vertical_margin" 

47.     tools:context=".MainActivity"

48.  

49.     <TextView 

50.         android:id="@+id/textView1" 

51.         android:layout_width="wrap_content" 

52.         android:layout_height="wrap_content" 

53.         android:text="TextView" 

54.         android:textAppearance="?android:attr/textAppearanceLarge" /> 

55.  

56.     <Button 

57.         android:id="@+id/button1" 

58.         android:layout_width="wrap_content" 

59.         android:layout_height="wrap_content" 

60.         android:text="Click me, please" /> 

61.  

62. </LinearLayout