Simple Android Form Example

This is a simple form application. App contains an editText, a TextView and a button. When you click button, TextView print what you write on editText. There is two part in this example. First is xml ( user interface) and the second is java class part. The project structure is below.





A. main_activity.xml
This is a user interface file. You defined what user see and interact.
This file's contain the following components.

      *main_activity.xml
               *LinearLayout
                      -EditText
                      -TextView
                      -Button


The file (main_activity.xml) content is below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/messageEditText" />

    <TextView
        android:layout_width="212dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="This shows message you entered"
        android:id="@+id/messageTextView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click"
        android:id="@+id/clickButton" />
</LinearLayout> 
 
B.MainActivity.java
 
This class contains three field of ui components. These are editText, textView 
and button. You realize that id of EditText "messageEditText" is get in java file
with findViewById(R.id.messageEditText). You define a ClickListener field that 
instance and set this clickListener to button.



import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText messageText;
    TextView messageView;
    Button clickButton;
    private View.OnClickListener clickButtonListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String message = messageText.getText().toString();
            messageView.setText(message);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        messageText = (EditText) findViewById(R.id.messageEditText);
        messageView = (TextView) findViewById(R.id.messageTextView);
        clickButton = (Button) findViewById(R.id.clickButton);


        clickButton.setOnClickListener(clickButtonListener);
    }
}


C. Run Application

Run the application, when emulator start, you will see this. Write sth the edit text, then click (Click) button. the message label show what you write.




After you write sth and click "CLICK" button. you see below.