Thursday, 12 July 2012

Sample Button App In Android

This post consist of a "Sample Button usage "

create one sample application name as  MyFirstApp 
-- Create a activity ,It looks like..

 

-- In res you will find the xml file with main.xml as name
-- add Button to that xml file It looks Like 

main.xml
 public class MyFirstAppActivity extends Activity {
   /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

-- Now go to Activity and write a code for Button it as simple as follow 

public class MyFirstAppActivity extends Activity {
    /** Called when the activity is first created. */
    private Button bt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        bt = (Button) findViewById(R.id.button1);
        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(MyFirstAppActivity.this,
                        "Hey You Clicked Button", Toast.LENGTH_LONG).show();
            }
        });

    }
}




No comments:

Post a Comment