Thursday 15 October 2015

Android: handle button click event

     Buttons are the important elements in every application, And this article will guide you to make use of buttons in your android application. The java code has to be copied to your activity class and the xml code to your corresponding layout xml file.




To Trigger the event by connecting the button with its Resource ID
Java:
 Button clickButton = (Button) findViewById(R.id.clickButtonone);  
 clickButton.setOnClickListener( new OnClickListener() {  
 @Override public void onClick(View v) {  
 // TODO Auto-generated method stub ***Do what you want with the click here***   
 } });  

XML:
<Button  
     android:id="@+id/clickButtonone"   
     android:layout_width="wrap_content"  
     android:text="Click here"   
     android:layout_height="wrap_content" />  

To Trigger the event by onClick attribute of layout xml

Java:
 public void clicker(View v){  
 //your button event goes here  
 }  

XML:
<Button  
     android:id="@+id/clickButtonone"  
     android:layout_width="wrap_content"  
     android:text="Click here"  
      android:onClick="clicker"  
     android:layout_height="wrap_content" />   

No comments:

Post a Comment