How do you reference EditText in alertdialog? [duplicate] - android

This question already has answers here:
findviewbyid returns null in a dialog
(7 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
Here is my code:
public void openDialog(){
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
AlertDialog alert = alertDialogBuilder.create();
final EditText a = (EditText) alert.findViewById(R.id.kekekeke);
LayoutInflater inflater = getLayoutInflater();
alertDialogBuilder.setView(inflater.inflate(R.layout.list_example, null));
alertDialogBuilder
.setMessage("Enter a New Name")
.setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());
}
});
}
My problem is that I got below error :
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
I'm confused because I thought that I already accessed it through the edittext.

You can reference it like below
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.list_example, null);
EditText a = (EditText) v.findViewById(R.id.kekekeke);
alertDialogBuilder.setView(v);

Try this,
public void openDialog(){
final AlertDialog.Builder alertDialogBuilder =
new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.list_example, null);
alertDialogBuilder.setView(view);
final EditText a = (EditText) view.findViewById(R.id.kekekeke);
alertDialogBuilder .setMessage("Enter a New Name")
.setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());
}
})
AlertDialog alert = alertDialogBuilder.create();
alert.show();
You should inflate the view first

Problem is your alert dialog has not set the view when you get the reference from it, so it does not have anything until you call setview, so you are calling it before and it is providing you nothing so you get the null pointer
public void openDialog(){
final AlertDialog.Builder alertDialogBuilder =
new AlertDialog.Builder(this);
AlertDialog alert = alertDialogBuilder.create();
LayoutInflater inflater = getLayoutInflater();
alertDialogBuilder.setView(inflater.inflate(R.layout.list_example, null));
final EditText a = (EditText) alert.findViewById(R.id.kekekeke);
alertDialogBuilder .setMessage("Enter a New Name")
.setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());
}
})

Problem is your alert dialog has not to the view when you try to get reference from view, so it does not have anything until you set your view to AlertDialog, So it returns NullPointerException,
Just replace this code with your code
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
AlertDialog alert = alertDialogBuilder.create();
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.list_example, null);
final EditText a = (EditText) view.findViewById(R.id.kekekeke);
alertDialogBuilder.setView(view);
alertDialogBuilder
.setMessage("Enter a New Name")
.setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());
}
});

Related

How to access 'findViewById' method inside Dialog onClick event

I'm having a custom layout xml file named login_form with a LinearLayout root named login_form_root. I'm trying to display this layout using dialog but can't get values from EditText as I get cannot resolve method findViewById inside onCreateDialog.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.login_form, null));
builder.setPositiveButton(okText, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
View myView = inflater.inflate(R.layout.login_form, (ViewGroup) findViewById(R.id.login_form_root));
EditText userEmail = (EditText) myView.findViewById(R.id.email_address);
userEmailValue = userEmail.getText().toString();
Toast.makeText(getActivity(), userEmailValue, Toast.LENGTH_SHORT).show();
}
Error is here:-
"(ViewGroup) findViewById(R.id.login_form_root)"
so how can I inflate the custom layout to get EditText values?
You should save the View when inflate it, then you can use it later
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
final View view = layoutInflater.inflate(R.layout.login_form, null);
builder.setView(view);
AlertDialog dialog = builder.create();
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
EditText userEmail = (EditText) view.findViewById(R.id.email_address);
String userEmailValue = userEmail.getText().toString();
}
});

Android- getChildAt(poisition) produces null

Guys i have a Custom alert dialogue and inside that i am inflating a listView
i want to access the child of that listview but it produces error can anyone help here is my relevant code snippet
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Context);
View alertView = AddExpenses.this.getLayoutInflater().inflate(R.layout.layout_alert_expenses, null);
int amount = Integer.parseInt(etExpensesAmount.getText().toString());
final ListView lvAlertDialogue = (ListView) alertView.findViewById(R.id.lv_alert_expenses);
final TextView tvTotal = (TextView) alertView.findViewById(R.id.tv_amount);
tvTotal.setText(etExpensesAmount.getText().toString());
CustomAdapterAlertList customAdapterAlertList = new CustomAdapterAlertList(AddExpenses.this, listSelectedContact, amount);
lvAlertDialogue.setAdapter(customAdapterAlertList);
alertDialogBuilder.setView(alertView);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
final EditText etAmount;
View singleView = lvAlertDialogue.getChildAt(listSelectedContact.size()-1); //singleView is null here
getChildAt() works for the visible item in list view otherwise it will return null.
Seeing your code I suppose you want the last item. You can try the following code.
View singleView = lvAlertDialog.getChildAt(lvAlertDialog.getLastVisiblePosition()):
In your code you are calling lvAlertDialog.getChildAt(lvAlertDialog.getLastVisiblePosition())
just after the alertDialog.show();
getChildAt is excuteing before showing alertDialog and
getChildAt will work if and only if the dialog is visible.
so it is null.
Try this code. Hope It will work.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Context);
View alertView = AddExpenses.this.getLayoutInflater().inflate(R.layout.layout_alert_expenses, null);
int amount = Integer.parseInt(etExpensesAmount.getText().toString());
final ListView lvAlertDialogue = (ListView) alertView.findViewById(R.id.lv_alert_expenses);
final TextView tvTotal = (TextView) alertView.findViewById(R.id.tv_amount);
tvTotal.setText(etExpensesAmount.getText().toString());
CustomAdapterAlertList customAdapterAlertList = new CustomAdapterAlertList(AddExpenses.this, listSelectedContact, amount);
lvAlertDialogue.setAdapter(customAdapterAlertList);
alertDialogBuilder.setView(alertView);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
View singleView = lvAlertDialogue.getChildAt(listSelectedContact.size() - 1); //singleView is null here
}
});
alertDialog.show();

How can I add both image and text into alertbox in android

I coded like below for creating a alertdialog . alert box works properly but how can I add image into alertbox?? while the title and messages ared parsed strings from my database.I need to add image from database dynamically.
private AlertDialog.Builder builder;
public void onClick(View v) {
builder = new AlertDialog.Builder(ProfileViewActivity.this).setTitle(ser_name)
.setMessage(desc);
builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
Inflate layout to you alertbox
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);
ImageView imgview = (ImageView) dialogView.findViewById(R.id.img);
imgview.setResource(R.drawable.img);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
make you own layout

custom AlertDialog with layout

Im trying to get the content of the editText, but idont know how to do it. Please help me in this.(Im using the method showDialog)
protected Dialog onCreateDialog(int id, Bundle args)
{
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
builder2.setView(inflater.inflate(R.layout.dialog_search_teacher,null));
final EditText price,city;
price=(EditText)findViewById(R.id.price_search);
city=(EditText)findViewById(R.id.city_search);
builder2.setPositiveButton("Serach", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
serach(city.getText().toString(),price.getText().toString());
}
});
builder2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert2 = builder2.create();
return(alert2);}
If you need to get the text from the EditText you have to do this:
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_search_teacher, null);
city=(EditText)view.findViewById(R.id.city_search);
and then, on the onclick() method you have to retrieve the text:
city.getText().toString();
but if you want to pass the string to the parent class you have to do an interface like this:
http://developer.android.com/training/basics/fragments/communicating.html
You have to find editText id like this
price=(EditText)builder2.findViewById(R.id.price_search);
You should declaration your editText under the dialog method like,
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_search_teacher, null);
city=(EditText)view.findViewById(R.id.city_search);
After that, while doing onClick listener for postive button, you can get the content of editText using
city.getText().toString();
Hope this will help you.

Get subview from AlertDialog

I have the following AlertDialog and its onClick method:
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setView(View.inflate(this, R.layout.barcode_alert, null));
alertDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() {
#Override
public void onClick(#NotNull DialogInterface dialog, int which) {
// how to get editTextField.getText().toString(); ??
dialog.dismiss();
}
});
The XML I inflate in the dialog (barcode_alert.xml) contains, among other things, an EditText field, and I need to get its string value after the user taps the Search button.
My question is, how do I get a reference to that field so I can get its text string?
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
final View v = View.inflate(this, R.layout.barcode_alert, null); //here
alertDialog.setView(v);
alertDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() {
#Override
public void onClick(#NotNull DialogInterface dialog, int which) {
((EditText)v.findViewById(R.id.edit_text)).getText().toString(); //and here
dialog.dismiss();
}
});
Try this,
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
final View v = getLayoutInflater().inflate(this, R.layout.barcode_alert, null);
final EditText editTextField = (EditText) v.findViewById(R.id.edit_text);
editTextField.setOnClickListener(MyActivity.this);
alertDialog.setView(v);
alertDialog.setPositiveButton("Search", new DialogInterface.OnClickListener() {
#Override
public void onClick(#NotNull DialogInterface dialog, int which) {
String enteredValue = editTextField.getText().toString(); // get the text from edit text
dialog.dismiss();
}
});
You can inflate your view and get a reference to a variable:
View v = View.inflate(this, R.layout.barcode_alert, null);
you can use findViewById to get a reference to your view. Make sure that your variable is final or a member variable. Otherwise you cannot access it in the onClickListener
final EditText editText = (EditText)v.findViewById(R.id.your_edit_text

Categories

Resources