Translate To Your Own Langauge

button in android example

As we know that in any technology, if you want to apply some functionality to the UI on the user interaction then best way is to use Button. In android, we can use two type of buttons. i.e
  • Button
  • ImageButton.
In this tutorial, i am going to tell you about how to implement the button in android. I will use an example so that you could easily understand the concept. In my example, I have used two buttons so that you can compare their functionality. These are the steps:

Step1: 

First of all you have to make the UI for your project. I will not tell you to make new prject and all that because it is already given in the first tutorial. If you don't know how to make a new project then visit
Getting started with android .So lets make the UI. You can use Drag and drop method or just copy and paste the following code into your layout file which is in layout/activity_main.xml

activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        android:text="Button 1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn1"
        android:layout_below="@+id/btn1"
        android:layout_marginTop="86dp"
        android:text="Button 2" />

</RelativeLayout>


Step 2:

Now you UI is ready. Now add the functionality to the UI. Follow these steps in mainActivity.java file
  • Make objects of Button.
Button btn1,btn2;
  • Initialize the objects of button in Oncreate method
                //Initilize the objects and add reference.
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
  • set onclickListener on both buttons
Your final code in mainActivity.java file will be as following:

mainActivity.java
package com.AT4U.button_example;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
       //creating object for two buttons
       Button btn1,btn2;
      

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              //Initilize the objects and add reference.
              btn1=(Button)findViewById(R.id.btn1);
              btn2=(Button)findViewById(R.id.btn2);
              //btn1 functionaliy
              btn1.setOnClickListener(new View.OnClickListener() {
                    
                     @Override
                     public void onClick(View arg0) {
                           // TODO Auto-generated method stub
                     Toast.makeText(getApplicationContext(), "btn1 is clicked", Toast.LENGTH_SHORT).show();
                     }
              });
             
              //btn2 functionaliy
                           btn2.setOnClickListener(new View.OnClickListener() {
                                 
                                  @Override
                                  public void onClick(View arg0) {
                                         // TODO Auto-generated method stub
                                  Toast.makeText(getApplicationContext(), "btn2 is clicked", Toast.LENGTH_SHORT).show();
                                  }
                           });
             
       }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

}

13 Responses to “button in android example”

Unknown said...
23 October 2019 at 06:07

it has been of great help.thanks

can you help me get well on Listeners


Utku said...
7 August 2023 at 18:46

yurtdışı kargo
resimli magnet
instagram takipçi satın al
yurtdışı kargo
sms onay
dijital kartvizit
dijital kartvizit
https://nobetci-eczane.org/
U1J


Doğu665 said...
28 September 2023 at 00:08

https://bayanlarsitesi.com/
Manisa
Denizli
Malatya
Çankırı

GG2OS


ThunderNova134 said...
29 September 2023 at 05:41

Ankara
Van
Hakkari
Edirne
Yozgat
0T5


TimePhantom314 said...
9 October 2023 at 07:41

görüntülü.show
whatsapp ücretli show
68Q6


ElectricPhantomI56 said...
21 October 2023 at 09:15

Kastamonu Lojistik
Yozgat Lojistik
Çorlu Lojistik
Kırşehir Lojistik
Sinop Lojistik
Q2XDXB


CBC01JeromeE4D7D said...
6 November 2023 at 00:10

AC410
Eskişehir Evden Eve Nakliyat
Edirne Lojistik
Tekirdağ Parça Eşya Taşıma
Trabzon Parça Eşya Taşıma
Düzce Lojistik


DAA32Caitlin30D57 said...
11 November 2023 at 07:39

A4F48
Eskişehir Lojistik
Ağrı Şehirler Arası Nakliyat
Niğde Parça Eşya Taşıma
Çorum Lojistik
Bilecik Parça Eşya Taşıma
Etimesgut Fayans Ustası
Gölbaşı Parke Ustası
Çorum Evden Eve Nakliyat
Ünye Oto Boya


791FCJorge4F0EC said...
12 November 2023 at 08:52

B3D28
Bitmex Güvenilir mi
Yobit Güvenilir mi
Antep Parça Eşya Taşıma
Erzincan Parça Eşya Taşıma
Eryaman Fayans Ustası
Karaman Parça Eşya Taşıma
Silivri Evden Eve Nakliyat
Urfa Şehir İçi Nakliyat
Konya Şehir İçi Nakliyat


86651RogerD5FE0 said...
16 November 2023 at 12:52

955B2
Denizli Evden Eve Nakliyat
Tekirdağ Evden Eve Nakliyat
Antalya Evden Eve Nakliyat
order testosterone propionat
Ardahan Evden Eve Nakliyat
buy sarms
testosterone enanthate for sale
Eryaman Alkollü Mekanlar
order steroids


A3D0BAlayna474A5 said...
27 December 2023 at 13:36

EFAD4
Mardin Yabancı Sohbet
Kayseri Kızlarla Canlı Sohbet
antep telefonda kızlarla sohbet
bitlis ücretsiz sohbet uygulamaları
Kırıkkale Random Görüntülü Sohbet
ısparta Canlı Sohbet Bedava
yozgat rastgele sohbet siteleri
Denizli Görüntülü Sohbet Siteleri Ücretsiz
denizli chat sohbet


4F2E4JulieDB8A1 said...
5 January 2024 at 08:08

416D4
chat sohbet
kırıkkale bedava görüntülü sohbet
Amasya Sesli Sohbet Odası
bedava görüntülü sohbet sitesi
görüntülü sohbet kadınlarla
şırnak canlı sohbet et
adıyaman görüntülü sohbet uygulama
muş sesli sohbet odası
bartın rastgele sohbet


A0701Faith1DD4F said...
20 January 2024 at 19:34

BEA78
Binance Referans Kodu
Threads İzlenme Hilesi
Loop Network Coin Hangi Borsada
Nonolive Takipçi Satın Al
Threads İzlenme Satın Al
Soundcloud Beğeni Satın Al
Youtube İzlenme Hilesi
Bitcoin Kazanma
Threads Yeniden Paylaş Satın Al


Post a Comment

Comment your feedback for the post. You can also ask your queries regarding android.

Blogger Widgets
All Rights Reserved Android Tutorials | Designed by: Deepak Anand