Monday 4 April 2011

How to Use Button Click

Button is the most commonly use function to fire up an event or action. We'll show an example to use button with android. How to get an action when you click on a button?


In your main.xml file, create the button like this:-
<Button android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="myClickHandler" 
    android:text="Click Me"/>


Then in your .java file, put this code:-

public class buttonclick extends Activity
{
    private Button btnclickme;    

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnclickme = (Button) findViewById(R.id.Button01);

        btnclickme.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)            
            {
                 //what happen when click            
                 //put your code here
             }
         });               
    }
}



How's that? Hope that's helpful.

No comments: