custom AlertDialog with layout - android

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.

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();
}
});

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

Setting EditText in an AlertDialog

I have created a Custom AlertDialog and I'm trying to set text for EditTexts. I have tried following
public class CheckListDialog extends DialogFragment {
private View view;
public CheckListDialog(View view) {
this.view = view;
}
#Override
public AlertDialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_checklist, null);
EditText etCost = (EditText) dialogLayout.findViewById(R.id.etCost);
EditText etOdoReading = (EditText) dialogLayout.findViewById(R.id.etOdometer);
etOdoReading.setText("bla");
etCost.setText("tada");
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(Reminders.this, android.R.color.transparent));
builder.setView(inflater.inflate(R.layout.dialog_checklist, null))
.setTitle("jaja")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
}
The EditTexts are empty and does not contain the values. How can i fix this?
Replace
builder.setView(inflater.inflate(R.layout.dialog_checklist, null))
with
builder.setView(dialogLayout)
You're modifying the edittexts in one layout and then inflating a new layout for the dialog.
For such operations you have to use a custom layout to inflate into the CheckListDialog class of yours. DialogFragment will provide you complete freedom to customize your dialog as per your requirement.
You can refer to this tutorial

Why setText() of EditText is not working?

I want to put a string in a EditText in a AlertDialog,which is in a onItemClick Listener with a ListVIew.Here is the part of code
dialogbuilder= new AlertDialog.Builder(this)
.setTitle(R.string.title_connect_dialog)
.setView(getLayoutInflater().inflate(R.layout.dialog_connect,null))
.setPositiveButton("Connect", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.dialog_connect, null);
edit1 = (EditText)view.findViewById(R.id.devicename);
edit2 = (EditText)view.findViewById(R.id.deviceadd);
private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View v, int arg2, long arg3) {
String info = ((TextView) v).getText().toString();
name = info.substring(0,info.length() - 18);
address = info.substring(info.length() - 17);
dialogbuilder.show();
edit1.setText(name);
edit2.setText(address);
}
};
When I run the project,there is nothing in the EditText!It does not work!But there is no exception or error.I tried only set text in the XML file can work.I replaced EditText with TextView ,but is not work too.
Are there something wrong in logical?Thank you for reading.
Use as view instance for accessing Views form Dialog layout which you are passing in setView method of AlertDialog.Builder change:
.setView(getLayoutInflater().inflate(R.layout.dialog_connect,null))
to
.setView(view)
EDIT:
or instead of inflate layout again to access views you should use dialogbuilder instance to initialize EditText as:
edit1 = (EditText)dialogbuilder.findViewById(R.id.devicename);
edit2 = (EditText)dialogbuilder.findViewById(R.id.deviceadd);
You are showing the Dialog before setting the text
dialogbuilder.show();
edit1.setText(name);
edit2.setText(address);
Try changing that around
edit1.setText(name);
edit2.setText(address);
dialogbuilder.show();

Not able to get input from the custom fragment dialog

i am displaying a custom dialog for user input on top of my list view activity layout. But when i try to refer the edit text in onCreateDialog() method, its always coming as blank. Below is my code, appreciate if someone can point out where i am doing wrong -
public class QuestionDialog extends DialogFragment
{
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
final View v =inflater.inflate(R.layout.ques_dialog,null);
builder.setView(inflater.inflate(R.layout.ques_dialog, null))
.setTitle("Title Message!")
.setCancelable(true)
.setPositiveButton("GO", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
EditText et1 = (EditText)v.findViewById(R.id.et1);
// here not able to get the data from edit text
String s1 = et1.getText().toString().trim();
Log.d("debug","data -"+s1+" and length"+s1.length());
.... rest code goes here
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
..rest code goes here
}
});
return builder.create();
}
}
That is happening because of this lines:
final View v =inflater.inflate(R.layout.ques_dialog,null);
builder.setView(inflater.inflate(R.layout.ques_dialog, null))
You first inflate the v View and then set as the content of the dialog a newly inflated view(so in the end you end up looking for a view which isn't the actual content of the dialog). Use this:
final View v =inflater.inflate(R.layout.ques_dialog,null);
builder.setView(v);

Categories

Resources