Alertdialog.Builder with Custom Layout and Edittext; Cannot Access View

AlertDialog.Builder with custom layout and EditText; cannot access view

editText is a part of alertDialog layout so Just access editText with reference of alertDialog

EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);

Update:

Because in code line dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));

inflater is Null.

update your code like below, and try to understand the each code line

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);

EditText editText = (EditText) dialogView.findViewById(R.id.label_field);
editText.setText("test label");
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();

Update 2:

As you are using View object created by Inflater to update UI components else you can directly use setView(int layourResId) method of AlertDialog.Builder class, which is available from API 21 and onwards.

AlertDialog with custom layout not showing up

The problem is this line:

builder.setView(findViewById(R.id.system_profile_dialog));

Change it to:

builder.setView(LayoutInflater.from(this).inflate(R.layout.layout_name, null, false));

You're supposed to pass an inflated view there, not an id of a view in current activity.

Adding custom layout in an AlertDialog

You can set custom layout to your dialog like below:

Create a custom layout file:

custom.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF" />

<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"/>

</RelativeLayout>

Then in your activity:

// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title");

// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text1);
text.setText("Text view 1");

TextView text = (TextView) dialog.findViewById(R.id.text2);
text.setText("Text view 2");
dialog.show();

How to implement a custom AlertDialog View

You are correct, it's because you didn't manually inflate it. It appears that you're trying to "extract" the "body" id from your Activity's layout, and that won't work.

You probably want something like this:

LayoutInflater inflater = getLayoutInflater();
FrameLayout f1 = (FrameLayout)alert.findViewById(android.R.id.body);
f1.addView(inflater.inflate(R.layout.dialog_view, f1, false));

Unable to access views inside an AlertDialog

Turns out, AlertDialog returns a view

AlertDialog dialog = builder.create();
dialog.show();
mPasswordField = (EditText) dialog.findViewById(R.id.password_field);

This did the trick for me.

Android: Retrieve EditText value from DialogBox with Custom View

Create one view in which inflate the xml file, and use that view before findViewById()

final View view = inflater.inflate(R.layout.schedule,null);
builder.setView(view);

final EditText edtSelectDate = (EditText) view.findViewById(R.id.edtSelectDate);

EditText in Alert Dialog Android

The reason you are not able to access editTextField is because of it is declared as local variable in alertDialog() method.

If in case you want to keep that variable as local ,you need to set the listener method directly in that method as i show below.

private EditText = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Message")
.setView(inputEditTextField)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String editTextInput = inputEditTextField.getText().toString();
Log.d("onclick","editext value is: "+ editTextInput);
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();


Else you can replace this line from alertDialog() method with this below line

editTextField = new EditText(this.getContext());



you need add this line above onCreate method

EditTextField editTextField;


Related Topics



Leave a reply



Submit