Android Ex1, Practical
Android Ex1, Practical
Colors
BY DEVANG · FEBRUARY 27, 2016
Aim:
To develop a Simple Android Application that uses GUI components, Font and
Colors.
Procedure:
Creating a New project:
▪ Open Android Stdio and then click on File -> New -> New project.
3
android:layout_width="match_parent"
4 android:layout_height="match_parent">
6 <TextView
android:id="@+id/textView"
7
android:layout_width="match_parent"
8
android:layout_height="wrap_content"
9
android:layout_margin="30dp"
10 android:gravity="center"
11 android:text="Hello World!"
android:textSize="25sp"
12
android:textStyle="bold" />
13
14
<Button
15
android:id="@+id/button1"
16 android:layout_width="match_parent"
17 android:layout_height="wrap_content"
android:layout_margin="20dp"
18
android:gravity="center"
19
android:text="Change font size"
20
android:textSize="25sp" />
21 <Button
android:id="@+id/button2"
22
android:layout_width="match_parent"
23
android:layout_height="wrap_content"
24
android:layout_margin="20dp"
25
android:gravity="center"
26 android:text="Change color"
android:textSize="25sp" />
27
</LinearLayout>
28
29
30
31
32
33
▪ Now click on Design and your application will look as given below.
▪ So now the designing part is completed.
Java Coding for the Android Application:
▪ Click on app -> java -> com.example.exno1 -> MainActivity.
▪ Then delete the code which is there and type the code as given below.
Code for MainActivity.java:
?
package com.example.exno1;
1
2
import android.graphics.Color;
3
import android.support.v7.app.AppCompatActivity;
4
import android.os.Bundle;
import android.view.View;
5
import android.widget.Button;
6
import android.widget.TextView;
8
public class MainActivity extends AppCompatActivity
9
{
10 int ch=1;
float font=30;
11
@Override
12
protected void onCreate(Bundle savedInstanceState)
13
{
14
super.onCreate(savedInstanceState);
15 setContentView(R.layout.activity_main);
18
@Override
19
public void onClick(View v) {
20 t.setTextSize(font);
font = font + 5;
21
if (font == 50)
22
font = 30;
23
}
24
});
@Override
27
public void onClick(View v) {
28
switch (ch) {
29
case 1:
30 t.setTextColor(Color.RED);
break;
31
case 2:
32
t.setTextColor(Color.GREEN);
33
break;
34
case 3:
35 t.setTextColor(Color.BLUE);
break;
36
case 4:
37
t.setTextColor(Color.CYAN);
38
break;
39
case 5:
40 t.setTextColor(Color.YELLOW);
break;
41
case 6:
42
t.setTextColor(Color.MAGENTA);
43
break;
44
}
45 ch++;
if (ch == 7)
46
ch = 1;
47
}
48
});
49
}
50
}
51
52
53
54
55
56
57
58
59
60
Result:
Thus a Simple Android Application that uses GUI components, Font and Colors
is developed and executed successfully.