I am new to working with a floating action button and trying to get some of the basic things working today. I am currently lingering to work with onClick functionality. I pulled most of the code from the basic Google googles FAB example, and there it has the onChecked method, which sends a line to the log to show that you clicked on it.
@Override public void onCheckedChanged(FloatingActionButton fabView, boolean isChecked) {
I thought I could replace the functionality there, but it didn’t affect. So I tried to create onClickListener , like with any other button, but it also did not affect. I am not sure how to proceed, since not one option worked. my goal is to simply create a dialog when I click the floating action button, but for now, I'm just trying to use the placeholder alert dialog.
This is the class FloatingActionButtonFragment:
public class FloatingActionButtonFragment extends Fragment implements FloatingActionButton.OnCheckedChangeListener { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fab_layout, container, false); // Make this {@link Fragment} listen for changes in both FABs. FloatingActionButton fab1 = (FloatingActionButton) rootView.findViewById(R.id.fab_1); fab1.setOnCheckedChangeListener(this); fab1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage("Are you sure?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } }); // Create the AlertDialog object and return it AlertDialog dialog = builder.create(); dialog.show(); } }); return rootView; } @Override public void onCheckedChanged(FloatingActionButton fabView, boolean isChecked) { // When a FAB is toggled, log the action. switch (fabView.getId()){ case R.id.fab_1: break; default: break; } } }
And here is the FloatingActionButton class:
public class FloatingActionButton extends FrameLayout implements Checkable { public static interface OnCheckedChangeListener { void onCheckedChanged(FloatingActionButton fabView, boolean isChecked); } private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked }; private static final String TAG = "FloatingActionButton";
At the moment there is not much for any class, they are mostly husk, but I just want to get this basic functionality before I continue, and being a noob, I, I don’t know why this wouldn’t work .