Button Android

Google Android button example source code for developers.

// get handle
Button myButton;
myButton = (Button)findViewById(R.id.idMyButton);
//set focus
myButton.requestFocus();
// set background image
myButton.setBackgroundResource(R.drawable.backImage);
// or
myButton.setBackgroundDrawable(getResources().getDrawable( R.drawable.someImage));
 
// set visibility
myButton.setVisibility(View.INVISIBLE); // VISIBLE
 
///////// SET LISTENER
       Button myButton =(Button)findViewById(R.id.button1);
        myButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "AHOJ",
                    Toast.LENGTH_LONG).show();
            }
        });	
 
// or set onClickListener
 myButton.setOnClickListener(myListener);
//end onCreate .....
   private OnClickListener myListener = new OnClickListener() {
        public void onClick(View v) {
 
        }
   }
This entry was posted in Android Examples Code and tagged , , . Bookmark the permalink.


Comments are closed.



3,013 views