MainActivity.java
public class MainActivity extends Activity { 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  /** Call customButton Method */
  customButton();  
 }
 private void customButton() {
  // TODO Auto-generated method stub
   Button myButton = new Button(this);
   myButton.setText("Click Me");
   LinearLayout ll = (LinearLayout)findViewById(R.id.btn_layout);
   LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
   ll.addView(myButton, lp);
   myButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this, "Hello Friends", Toast.LENGTH_LONG).show();      
    }
   });  
 }
}
activity_main.xml
<LinearLayout 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:background="@drawable/dynamic"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/btn_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>


 
 
 
 
