In: Computer Science
This is an Android Question. I am trying to make a FAB(Floating Action Button) show me a different page when I press it.
First, we add the floating action button to the layout in the xml file of the layout using the code below.
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="@drawable/ic_my_icon"
android:contentDescription="@string/submit"
android:layout_margin="16dp" />
Next, in the Activity file, we need to define what happens when the floating action button is pressed.
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
});
Here, we have added a listener to the floating action button set which would now start a new activity when the floating action button is pressed.