Display Snackbar without CoordinatorLayout

坚强是说给别人听的谎言 提交于 2020-01-12 03:36:26

问题


I have Displayed Snackbar using CoordinatorLayout but in some layout i did't used CoordinatorLayout layout and i want to display snackbar but faced problem with it.

I have tried following way to achieve snackbar without CoordinatorLayout but it will display following result.

Snackbar.make(getActivity().getWindow().getDecorView().getRootView(), "Testing Snackbar", Snackbar.LENGTH_LONG).show();

Test it with android 8.0 Oreo

Is there any way to achieve Snackbar without CoordinatorLayout?

Thanks in advance.

code

public class FirstFragment extends Fragment {

RelativeLayout rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);

        Log.e("onCreateView", "onCreateView");

        rootView = (RelativeLayout)view.findViewById(R.id.rootView);
        setSnackBar(rootView, "Testing with answered");
        return view;
    }

public static void setSnackBar(View coordinatorLayout, String snackTitle) {
        Snackbar snackbar = Snackbar.make(coordinatorLayout, snackTitle, Snackbar.LENGTH_SHORT);
        snackbar.show();
        View view = snackbar.getView();
        TextView txtv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
        txtv.setGravity(Gravity.CENTER_HORIZONTAL);
    }
}

回答1:


public static void setSnackBar(View root, String snackTitle) {
  Snackbar snackbar = Snackbar.make(root, snackTitle, Snackbar.LENGTH_SHORT);
  snackbar.show();
  View view = snackbar.getView();
  TextView txtv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
  txtv.setGravity(Gravity.CENTER_HORIZONTAL);
}

Just call the above method and pass any parent layout such as LinearLayout, RelativeLayout or ScrollView, for example:

setSnackBar(layout,"This is your SnackBar");  //here "layout" is your parentView in a layout

Do not forget to find your view using findViewById().




回答2:


Try this easy piece of code.. a dummy view from the android framework itself is used

Snackbar.make(findViewById(android.R.id.content),"Your Text Here",Snackbar.LENGTH_SHORT).show();




回答3:


As others suggest best solution to do this is via method mentioned before. I would add and option to show SnackBar with button to dismiss SnackBar. This comes useful when you'd like to give user time to read your message and then acknowledge your application that your user read your message.

   public void setSnackBar(View root, String textToDisplay) {
    Snackbar snackbar = Snackbar.make(root, textToDisplay, Snackbar.LENGTH_INDEFINITE);
    snackbar.setAction("OK", new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            snackbar.dismiss();
            //your other action after user hit the "OK" button
        }
    });
    snackbar.show();


来源:https://stackoverflow.com/questions/46275038/display-snackbar-without-coordinatorlayout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!