Ex. No.
1 Android Application that uses GUI components,
Font and Colors
Aim:
To develop a Simple Android Application that uses GUI
components, Font and Colors.
Algorithm:
Creating a New project:
Open Android Studio and then click on File -> New -> New
project.
Then type the Application name as [Link].1 and click Next.
Then select the Minimum SDK as shown below and
click Next.
Then select the Empty Activity and click Next.
Finally click Finish.
Designing layout for the Android Application
Click on app -> res -> layout -> activity_main.xml.
Place the TextView for displaying "Hello World".
Place two buttons, "CHANGE FONT SIZE" "CHANGE COLOR" for
changing the font size and color.
Sample Source Code:
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center"
android:text="Hello World!"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change font size"
android:textSize="25sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change color"
android:textSize="25sp" />
</LinearLayout>
[Link]:
package [Link].exno1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity
{
int ch=1;
float font=30;
@Override
protected void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
final TextView t= (TextView) findViewById([Link]);
Button b1= (Button) findViewById([Link].button1);
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link](font);
font = font + 5;
if (font == 50)
font = 30;
}
});
Button b2= (Button) findViewById([Link].button2);
[Link](new [Link]() {
@Override
public void onClick(View v) {
switch (ch) {
case 1:
[Link]([Link]);
break;
case 2:
[Link]([Link]);
break;
case 3:
[Link]([Link]);
break;
case 4:
[Link]([Link]);
break;
case 5:
[Link]([Link]);
break;
case 6:
[Link]([Link]);
break;
}
ch++;
if (ch == 7)
ch = 1;
}
});
}
}
Output:
Result:
Thus a Simple Android Application that uses GUI components, Font and Colors is
developed and executed successfully.