How to perform a method on button click in android?

How to perform a method when the user clicks button in android studio? I have no idea how to do it so please help. I want to take value from textfield perform sign of it and display it on a textview when the user presses a button. please help.

Replay

Very simple:

Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
                ***Do what you want with the click here***
            }
        });

You should try to search it online before you ask a question here :)

Article: Android: how to handle button click

MainActivity.java

public class MainActivity extends AppCompatActivity {

private EditText etText;
private Button btnSubmit;
private TextView tvText;

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

    etText = (EditText)findViewById( R.id.et_text );
    btnSubmit = (Button)findViewById( R.id.btn_submit );
    tvText = (TextView)findViewById( R.id.tv_text );

    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            yourMethod();
        }
    });
}

private void yourMethod() {

    tvText.setText(etText.getText());
}

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.admin.myapplication.MainActivity">

<EditText
    android:id="@+id/et_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter the text to be viewed in text view" />

<Button
    android:id="@+id/btn_submit"
    android:text="Submit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:text="Your response will be shown here after clicking submit"
    android:id="@+id/tv_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Category: java Time: 2016-07-31 Views: 5

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.255 (s). 12 q(s)