How to use button in android studio?

Learn how to use button in Android studio


In this tutorial we will learn how we can use the button in the android studio. We will understand it by making an android app. This app will be very simple. Let us understand what will our do? This app will have two widgets. One is TextView and other is button. The text will be invisible at starting of our app. But when we press the button , it will become visible. So firstly create your project in the android studio. If you don’t know how to create a project in android studio click here to read how to create project in android studio

After creating project open MainActivity.xml , drag and drop button, textview from widget area. Our MainActivity.xml will look like the following code.

?xml version="1.0" encoding="utf-8"?
android.support.constraint.ConstraintLayout 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:layout_height="match_parent" tools:context="com.exampel.gaurav.myapplication.MainActivity"

Button android:id="@+id/button"

 android:layout_width="wrap_content" 

android:layout_height="wrap_content" 

android:text="Button" 

android:onClick="click" 

app:layout_constraintTop_toTopOf="parent" 

app:layout_constraintBottom_toBottomOf="parent" 

android:layout_marginBottom="8dp" 

app:layout_constraintVertical_bias="0.501" 

android:layout_marginLeft="8dp" 

app:layout_constraintLeft_toLeftOf="parent"

 android:layout_marginRight="8dp" app:layout_constraintRight_toRightOf="parent" 

TextView android:id="@+id/textView" 

android:layout_width="wrap_content"

 android:layout_height="wrap_content" 

android:text="Good Job" app:layout_constraintTop_toTopOf="parent" 

android:layout_marginTop="8dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toTopOf="@+id/button" 

app:layout_constraintVertical_bias="0.623" 

android:layout_marginRight="8dp" 

app:layout_constraintRight_toRightOf="parent" 

android:layout_marginLeft="8dp"

 app:layout_constraintLeft_toLeftOf="parent" 
android.support.constraint.ConstraintLayout

 

Remember in this case onClick method for button is “click”. We will add this method in MainActivity.java. This method will invoke on button click. So now add following code in MainActivity.java

package com.exampel.gaurav.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {

Button button;
TextView textview;

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

button = (Button)findViewById(R.id.button);
textview = (TextView)findViewById(R.id.textView);

}

public void click(View view)
{

if(textview.getVisibility() == View.GONE){
textview.setVisibility(view.VISIBLE);
}else{
textview.setVisibility(view.GONE);
}

}
}

First of all, we made objects of button and textvew. Now to find bind their ids with their objects we use findViewById and pass ids of widgets.Our click method is checking if textview is visible or not. If it is visible then make it invisible otherwise make it visible.
see the result in the video here.

Leave a Reply

Your email address will not be published. Required fields are marked *

five × 3 =