Ilias Tsagklis

About Ilias Tsagklis

Ilias Tsagklis is a senior software engineer working in the telecom domain. He is an applications developer in a wide variety of applications/services. Ilias is co-founder and Executive Editor at Java Code Geeks.

Main Activity UI example

One of the most basic steps you have to take when developing an Android Application is the design and the specifications of the main Activity (and any activity) of your App. Android API gives all the basic tools to make that action fairly easy. The basic layout of an Activity is described in a Layout XML file. In this XML file we can specify and customize the basic UI components like Buttons, EditText components, Icons etc… While programming your Android App you will notice that you keep using the same Strings or the same values for certain parameters throughout the Application.

In order to better organize your resources that you are going to use, you can create an Resource XML file, like res/values/strings.xml to create string resources for instance, that you can use in many places an in many ways in your Application. Then, you have to tell your Activity to use the layout XML file you’ve created as its Layout description. Finally you can further customize your UI by creating several Event Listeners to capture an handle several UI Events.

In short, to create a basic UI for your applications you have to:

  • Create an XML layout file. Most of the times the creation of a new Activity is followed by the automatic creation of a new layout file
  • Specify the resources you want. In out case we create several String resources in res/values/strings.xml file
  • Use setContentView to tell your Activity to use your Layout XML file as its Layout description
  • Create event Listeners in your Activity to handle UI Events

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MovieSearchAppActivity extends Activity {

    private static final String EMPTY_STRING = "";

    private EditText searchEditText;
    private RadioButton moviesSearchRadioButton;
    private RadioButton peopleSearchRadioButton;
    private RadioGroup searchRadioGroup;
    private TextView searchTypeTextView;
    private Button searchButton;

    public void onCreate(Bundle savedInstanceState) {






  searchButton.setOnClickListener(new OnClickListener() {


public void onClick(View v) {

    String query = searchEditText.getText().toString();

    if (moviesSearchRadioButton.isChecked()) {

  longToast(moviesSearchRadioButton.getText() + " " + query);


    else if (peopleSearchRadioButton.isChecked()) {

  longToast(peopleSearchRadioButton.getText() + " " + query);




  searchEditText.setOnFocusChangeListener(new DftTextOnFocusListener(getString(;

  int id = searchRadioGroup.getCheckedRadioButtonId();

  RadioButton radioButton = (RadioButton) findViewById(id);



    private void findAllViewsById() {

  searchEditText = (EditText) findViewById(;

  moviesSearchRadioButton = (RadioButton) findViewById(;

  peopleSearchRadioButton = (RadioButton) findViewById(;

  searchRadioGroup = (RadioGroup) findViewById(;

  searchTypeTextView = (TextView) findViewById(;

  searchButton = (Button) findViewById(;

    public void longToast(CharSequence message) {

  Toast.makeText(this, message, Toast.LENGTH_LONG).show();

    private OnClickListener radioButtonListener = new OnClickListener() {

  public void onClick(View v) {

RadioButton radioButton = (RadioButton) v;



    private class DftTextOnFocusListener implements OnFocusChangeListener {

  private String defaultText;

  public DftTextOnFocusListener(String defaultText) {

this.defaultText = defaultText;


  public void onFocusChange(View v, boolean hasFocus) {

if (v instanceof EditText) {

    EditText focusedEditText = (EditText) v;

    // handle obtaining focus

    if (hasFocus) {

  if (focusedEditText.getText().toString().equals(defaultText)) {




    // handle losing focus

    else {

  if (focusedEditText.getText().toString().equals(EMPTY_STRING)) {







<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""

    <EditText android:id="@+id/search_edit_text" 














android:text="@string/movies" />






android:text="@string/people" />











<?xml version="1.0" encoding="utf-8"?>


    <string name="hello">Hello World, MovieSearchAppActivity!</string>
    <string name="app_name">MovieSearchApp</string>

    <string name="search">Search</string>
    <string name="movies">Movies</string>
    <string name="people">People</string>


This was an Example on how to Create a basic User Interface for your Android Activities.

Related Article:

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2015, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: