In Android, Button is a user interface element where user click to perform some action, A button can be text or icon when user touches it it will perform some task. There are different types of button are available in android studio, like RadioButton, TonggelButton,CompondButton.
Here we discuss about onClick method, when ever onClick method some task will be performed.Create a android project, here in Activity.xml take root layout as LinearLayout , orientation as vertical.When ever user click on Button we will display a Toast message, for that take editText Below that a button, the code will lock like as shown in below.
Activity.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:orientation="vertical"
- android:layout_height="match_parent"
- tools:context="com.example.user.application1.MainActivity">
- <EditText
- android:hint="enter message"
- android:textSize="30dp"
- android:id="@+id/edit"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- <Button
- android:textSize="20dp"
- android:textColor="#F00"
- android:onClick="click"
- android:id="@+id/button"
- android:layout_marginTop="50dp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Button" />
- </LinearLayout>
MainActivity.java
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- EditText edittext;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- edittext = (EditText) findViewById(R.id.edit);
- }
- public void click(View view) {
- Toast.makeText(this, "message "+edittext.getText().toString(), Toast.LENGTH_SHORT).show();
- }
- }
Hope this post is helpful,for more useful post like and share with your friends, we also have Youtube channel for more details check out the video below, do like share and subscribe to our channel it will boost our energy and help to improve our work, Have Nice Day.
Button handling in Android Studio.
Reviewed by gcrt
on
March 17, 2018
Rating:

No comments: