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.
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
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;
}
}
|
Subscribe to:
Post Comments (Atom)
You may be interested in:
- Registration Form in Android with Validation
- Login with Validation (Android Example)
- Implementing Tab Example in android using TabActivity
- Splash screen in Android using Thread
- Showing Map
- button in android example
- Spinner Android Example
- Change the Profile Mode Programmatically (Android Example)
- Login Example (Android)
- Generating Google APIs
13 Responses to “button in android example”
23 October 2019 at 06:07
it has been of great help.thanks
can you help me get well on Listeners
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
28 September 2023 at 00:08
https://bayanlarsitesi.com/
Manisa
Denizli
Malatya
Çankırı
GG2OS
29 September 2023 at 05:41
Ankara
Van
Hakkari
Edirne
Yozgat
0T5
9 October 2023 at 07:41
görüntülü.show
whatsapp ücretli show
68Q6
21 October 2023 at 09:15
Kastamonu Lojistik
Yozgat Lojistik
Çorlu Lojistik
Kırşehir Lojistik
Sinop Lojistik
Q2XDXB
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
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
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
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
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
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
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.