I am trying to implement a button into a fragment in order to use the Float Action Button (FAB) in order to view the snackbar. How do I link the fab1 to a button in a fragment? this is the .java
file:
public class PrimaryFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.primary_layout,null);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab1);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Hai, Hallo", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
findViewById is defined in Activity not Fragment .
Try this:
getActivity().findViewById(R.id.fab1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.