How to make modification Android Snackbar containing with LayoutInflater button and editText

﹥>﹥吖頭↗ 提交于 2019-12-02 09:40:13
Ankush Bist

here is the source required to implement button listener to your snackbar custom layout.

@Override
     public void onClick(View v) {

        CoordinatorLayout linearLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);

        final Snackbar snackbar = Snackbar.make(linearLayout, "", Snackbar.LENGTH_INDEFINITE);
        Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();

        // Inflate your custom view with an Edit Text
        LayoutInflater objLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View snackView = objLayoutInflater.inflate(R.layout.custom_snac_layout, null);
        // custom_snac_layout is your custom xml

        // button id for snackbar
        Button button_mapel = (Button) snackView.findViewById(R.id.btn_mapel);

        // perform button click listener
        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // here perform your button click task
           }
        });

        layout.addView(snackView, 0);
        snackbar.show();

    }

Follow this documentation

https://developer.android.com/training/snackbar/action.html

public class MyUndoListener implements View.OnClickListener{

    &Override
    public void onClick(View v) {

        // Code to undo the user's last action
    }
}

Now for setAction()

Snackbar mySnackbar = Snackbar.make(findViewById(R.id.myCoordinatorLayout),
                                R.string.email_archived, Snackbar.LENGTH_SHORT);
mySnackbar.setAction(R.string.undo_string, new MyUndoListener());
mySnackbar.show();

Try This,

Button btn_feed_back = (Button)snackView.findViewById(R.id.feedback);
    btn_feed_back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

You have to get id of your Button. And have to set clickListener.

You have to just replace your code with this.

I have put listener on btn_mapel, you can put any other button according to your requirement.

Try this.

     @Override
     public void onClick(View v) {

        CoordinatorLayout linearLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);

        final Snackbar snackbar = Snackbar.make(linearLayout, "", Snackbar.LENGTH_INDEFINITE);
        Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();

        // Inflate your custom view with an Edit Text
        LayoutInflater objLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View snackView = objLayoutInflater.inflate(R.layout.custom_snac_layout, null);
        // custom_snac_layout is your custom xml

        Button mapelBt = (Button)snackView.findViewById(R.id.btn_mapel);

        mapelBt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

             // Get your button click callback here
        }
    });


        layout.addView(snackView, 0);
        snackbar.show();

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