buttonに対して、プログラムを介して「Hello Wordl!」ボタンを生成し、そのボタンをクリックするとHello World!のトーストが表示されるサンプルです。

サンプル

activity_main.xml

					
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
				

MainActivity

					
Button bt = this.findViewById(R.id.button);
bt.setText("Hello World!");
/*
 * Hello World!ボタンクリック
 */
bt.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(View v) {
		Toast.makeText(getApplicationContext(), "Hello World! Click", Toast.LENGTH_SHORT).show();
	}
});