public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bTambah, bLihat;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bTambah = (Button) findViewById(R.id.button_tambah);
bTambah.setOnClickListener(this);
bLihat = (Button) findViewById(R.id.button_view);
bLihat.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_tambah:
Intent i = new Intent(this, TambahDataActivity.class);
startActivity(i);
break;
case R.id.button_view:
Intent i2 = new Intent(this, TampilDataActivity.class);
startActivity(i2);
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/nama_app"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="CRUD"
android:textSize="20sp" />
<Button
android:layout_marginTop="20dp"
android:id="@+id/button_tambah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nama_app"
android:layout_centerHorizontal="true"
android:text="Tambah Data" />
<Button
android:id="@+id/button_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button_tambah"
android:layout_centerHorizontal="true"
android:text="Lihat Data"
/>
</LinearLayout>