How To Add Edittext In Snackbar In Android Programmatically?
I have Snackbar in my application. I want to add Edittext in Snackbar to accept some input. How can I add an Edittext in Snackbar?
Solution 1:
//Custom layouts are discouraged due to the intended use of Snackbars,but this will do your task!LinearLayoutlinearLayout= (LinearLayout)findViewById(R.id.linear_layout_root);
finalSnackbarsnackbar= Snackbar.make(linearLayout, "Hey Whats Up", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayoutlayout= (Snackbar.SnackbarLayout) snackbar.getView();
// Inflate your custom view with an Edit TextLayoutInflaterobjLayoutInflater= (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewsnackView= objLayoutInflater.inflate(R.layout.custom_snac_layout, null); // custom_snac_layout is your custom xml
layout.addView(snackView, 0);
snackbar.show();
Solution 2:
You need to inflate new view and add it to snackBar's layout.
// Make snackbarSnackbarsnackbar= Snackbar.make(rootView, "", Snackbar.Snackbar.LENGTH_INDEFINITE);
// Fetch the layout
Snackbar.SnackbarLayoutsLayout= (Snackbar.SnackbarLayout) snackbar.getView();
// remove the default textViewTextViewtextView= (TextView) sLayout.findViewById(android.support.design.R.id.snackbar_text);
textView.setVisibility(View.GONE);
// Inflate custom view (have an edittext in this)ViewnewView= mInflater.inflate(R.layout.edittext_layout, null);
sLayout.setView(newView,0);
Post a Comment for "How To Add Edittext In Snackbar In Android Programmatically?"