Form Widget Palette: placing text fields and Button, Button onClick event

You place widgets like EditText (a text field) and Button in the layout XML with an id, then in Java grab them with findViewById and react to a tap by setting an onClick, reading what the user typed with editText.getText().toString().

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

From a label to a real form

So far FestConnect only shows text. A registration form must take it: a box to type a name, and a button that does something with it.

Those are two widgets, EditText (a text field) and Button, placed in the XML layout. But placing them is half the job. The other half is Java: grabbing each widget by its id and reacting when the button is tapped. That tap reaction is the onClick event, and it is where your app first listens to the user.

Theory

Design the form, then staff the desk

The XML is the printed form on the counter: a blank for the name, a 'Submit' button drawn on the page. But paper does nothing on its own. Java is the clerk at the desk who, when Submit is pressed, actually reads the blank and acts. You design the form once (XML) and station a clerk to respond (the onClick handler).

Theory

Place, find, react

1. Place in XML: an EditText and a Button, each with android:id="@+id/...".

2. Find in Java: EditText name = findViewById(R.id.name); links the XML widget to a Java object.

3. Read: name.getText().toString() gets what the user typed (as a String).

4. React to a tap, two ways:

  • XML android:onClick="register" calling a public void register(View v) method, or
  • Java button.setOnClickListener(v -> { ... });.

Practical

Register button reads the name

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_main);

        EditText name  = findViewById(R.id.name);
        Button register = findViewById(R.id.register);

        register.setOnClickListener(v -> {
            String typed = name.getText().toString();   // read the field
            // (next lesson: show 'typed' in a Toast)
        });
    }
}

// XML alternative for the button:
// android:onClick="register"  ->  public void register(View v) { ... }

Think first

Why getText().toString()?

To read the name field you write name.getText().toString(), not just name.getText(). Why the extra toString()? What type does getText() actually return?

Show the answer

getText() returns an Editable (a CharSequence-like object), not a plain String. Most code wants a String, so you call `.toString()` to convert it. If you skip it and try to use the Editable as a String (for comparison, storage, or a Toast that expects a String), it will not behave as you expect. The idiom getText().toString() is how you pull a usable String out of an EditText every time.

Quiz

If you use android:onClick="register" in the XML, what must the Java method look like?

  1. public void register(View v) { ... }
  2. private String register() { ... }
  3. public void register() { ... } (no parameters)
  4. void register(int id) { ... }
Show the answer

public void register(View v) { ... }

An android:onClick handler must be public, return void, and take a single View parameter (the clicked view): public void register(View v) (A). It cannot be private or return a value (B), it must accept the View parameter, so a no-arg version fails to bind (C), and the parameter is a View, not an int (D). Get the signature wrong and Android cannot find/call the method, so the button appears dead.

Watch out

Widget and onClick traps

1. Reading an EditText without toString(): getText() is an Editable, not a String. Use getText().toString().

2. Wrong onClick signature: the XML onClick method must be public void name(View v). A private one, or one without the View parameter, will not be called.

3. Forgetting findViewById (or a wrong id): returns null, and touching it crashes with a NullPointerException. Match the @+id in XML to the R.id in Java.

Theory

Next: tell the user it worked

Right now the tapped name goes nowhere visible. The next lesson adds a Toast, the small message that pops up briefly, so tapping Register flashes 'Welcome, Aditi'. It is the simplest way to give feedback, and it slots straight into the onClick you just wrote.

Summary

Key takeaways

  • Place widgets in XML: EditText (text field) and Button, each with android:id="@+id/...".
  • In Java, findViewById(R.id.xxx) links the XML widget to a Java object (match the ids).
  • Read an EditText with getText().toString() (getText() returns an Editable, not a String).
  • Handle a tap two ways: android:onClick="method" (public void method(View v)) or setOnClickListener in Java.
  • A wrong onClick signature or a null findViewById makes the button seem dead or crash.
  • Memory hook: XML designs the form, Java staffs the desk.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Android Widgets (UI)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Form Widget Palette: placing text fields and Button, Button onClick event · Mobile Application Development - 1 (option B) · Gri-Learn