You are on page 1of 3

Code for Activity_main.

xml:
?
1
2
3
<?xml version="1.0" encoding="utf-8"?>
4 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
5 android:orientation="vertical"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent">
8
9 <TextView
android:id="@+id/textView"
10 android:layout_width="match_parent"
11 android:layout_height="wrap_content"
12 android:layout_margin="30dp"
13 android:gravity="center"
android:text="Hello World!"
14 android:textSize="25sp"
15 android:textStyle="bold" />
16
17 <Button
18 android:id="@+id/button1"
19 android:layout_width="match_parent"
android:layout_height="wrap_content"
20 android:layout_margin="20dp"
21 android:gravity="center"
22 android:text="Change font size"
23 android:textSize="25sp" />
<Button
24
android:id="@+id/button2"
25 android:layout_width="match_parent"
26 android:layout_height="wrap_content"
27 android:layout_margin="20dp"
28 android:gravity="center"
android:text="Change color"
29 android:textSize="25sp" />
30 </LinearLayout>
31
32
33

Code for MainActivity.java:


?
1 package com.example.exno1;
2
import android.graphics.Color;
3
import android.support.v7.app.AppCompatActivity;
4 import android.os.Bundle;
5 import android.view.View;
6 import android.widget.Button;
7 import android.widget.TextView;
8
9 public class MainActivity extends AppCompatActivity
{
10
int ch=1;
11 float font=30;
12 @Override
13 protected void onCreate(Bundle savedInstanceState)
14 {
super.onCreate(savedInstanceState);
15 setContentView(R.layout.activity_main);
16 final TextView t= (TextView) findViewById(R.id.textView);
17 Button b1= (Button) findViewById(R.id.button1);
18 b1.setOnClickListener(new View.OnClickListener() {
19 @Override
public void onClick(View v) {
20 t.setTextSize(font);
21 font = font + 5;
22 if (font == 50)
23 font = 30;
}
24 });
25 Button b2= (Button) findViewById(R.id.button2);
26 b2.setOnClickListener(new View.OnClickListener() {
27 @Override
28 public void onClick(View v) {
switch (ch) {
29 case 1:
30 t.setTextColor(Color.RED);
31 break;
32 case 2:
33 t.setTextColor(Color.GREEN);
break;
34 case 3:
35 t.setTextColor(Color.BLUE);
36 break;
37 case 4:
t.setTextColor(Color.CYAN);
38 break;
39 case 5:
40 t.setTextColor(Color.YELLOW);
41 break;
42 case 6:
t.setTextColor(Color.MAGENTA);
43 break;
44 }
45 ch++;
46 if (ch == 7)
ch = 1;
47
}
48 });
49 }
50 }
51
52
53
54
55
56
57
58
59
60

You might also like