Findviewbyid Returns Null in a Dialog

view.findViewById(....) always return null in dialog

Try this..

Change View name

final View main_view = inflater.inflate(R.layout.record_fragment, null);
builder.setView(main_view).setPositiveButton("Save experiment", null);

and use it

EditText x = (EditText) main_view.findViewById(;

because in your public void onClick(View view) { onclick has view that edit text need to refer inflated view. Not onclick view.


As like @Raghunandan said change onclick view name as public void onClick(View v) {

How to resolve findViewByID() returning Null after setContentView()

Please use

addSiteButton =(Button) addSiteSheet.findViewById(;

findViewById() returns null for Views in a Dialog

Calling findViewById() will search for views within your Activity's layout and not your dialog's view. You need to call findViewById() on the specific View that you set as your dialog's layout.

Try this

private void initSearch() {
AlertDialog.Builder searchDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
searchDialog.setTitle("Search Photos");
searchDialog.setMessage("Specify tag and value...");
// R.layout.search_dialog is my custom layour, it displays fine, it works.
View dialogView = inflater.inflate(R.layout.search_dialog, null);
EditText tagText = (EdiText) dialogView.findViewById(;
searchDialog.setPositiveButton( ... ) ...
AlertDialog myAlert = searchDialog.create(); //returns an AlertDialog from a Builder.;

Notice how I'm inflating the view and storing it in a View named dialogView. Then, to find your EditText named tagField, I'm using dialogView.findViewById(;

findViewById throws null exception when it is used on Alert Dialog widgets

Replace this:

tv7 = findViewById(
bt4 = findViewById(

With this:

tv7 = messageBoxView.findViewById(
bt4 = messageBoxView.findViewById(

You are inflating a view (the dialog) into your activity with:

val messageBoxView = LayoutInflater.from(this).inflate(R.layout.layout_dialog, null)

You then need to define tv7 and bt4 views, but they are in the view that you inflated previously. So you need to define them based on the inflated view.

findViewById Return null inside BottomSheetDialogFragment

Since that button is inflated during onCreateView, you can set the listener during onCreateView():

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

// get the views and attach the listener

TextView closeBtn = (TextView) view.findViewById(;
closeBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

return view;

Note that I'm doing view.findViewById() and not getActivity().findViewById()

Why findViewById() is returning null

you need to bind views from dialog, so manage it like this;

val dialogView = layoutInflater.inflate(R.layout.brush_size_dialog, null)
tvBrushSize = dialogView.findViewById(

Related Topics

Leave a reply