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();
Related
I have a listview in which when you select a row, it's checkbox becomes checked / unchecked. However, I have a onItemLongClick that displays a dialog.
Problem is when I long-click a row in the listview, it becomes checked and I don't want that to happen, I just need it to display a dialog. This is confusing me because the onItemClick is also called when I use onItemLongClick.
Here's the code for onItemClick:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkmark);
TextView tv3 = (TextView)view.findViewById(R.id.tx_amount);
String shitts = listView.getItemAtPosition(position).toString();
HashMap<String, String> data = new HashMap<>();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
try {
checkBox.setChecked(!checkBox.isChecked());
String[] a = shitts.split(", ");
String[] sep = a[0].split("=");
String betamount = sep[1];
String[] sepx = a[2].split("=");
String betnumber = sepx[1];
String showbetnumber = betnumber.replaceAll("[;/:*?\"<>|&{}']","");
if(checkBox.isChecked()){
hash.put(showbetnumber,tv3.getText().toString());
}else {
tv3.setText(betamount);
checked.removeAll(Collections.singletonList(position));
hash.remove(showbetnumber,tv3.getText().toString());
}
}catch (Exception e){
}
}
});
and here's the code for onItemLongClick
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
TextView txAmt = view.findViewById(R.id.tx_amount);
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle("Enter Amount:");
final EditText input = new EditText(MainActivity.this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(Configuration.KEYBOARD_12KEY);
alert.setView(input);
alert.setPositiveButton("enter", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String x = input.getText().toString();
txAmt.setText(x);
}
});
alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Put actions for CANCEL button here, or leave in blank
}
});
alert.show();
return false;
}
});
Any help is appreciated!
Create a boolean isLongClick and set it to false.
onItemLongClick(), set isLongClick to true.
On your dialog, set isLongClick to false again when any button is clicked or if the dialog was dismissed.
Finally, wrap all your code inside onItemClick() in:
if (!isLongClick) {
// onItemClick() code
}
The source of this solution
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.
i was searching and trying alot today but it all does not help.
I want to use a Dialog with my own layout. In this layout there is a spinner with some subitems. My Problem is i can't get those selecteditems.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.decision, null));
LayoutInflater inflater2= getLayoutInflater();
View viewInflater=inflater2.inflate(R.layout.decision, null);
Spinner spinner=(Spinner) viewInflater.findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.v("click", "returnValue");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.v("click", "nothing");
}
});
builder.setPositiveButton("Start", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.v("click", "positive");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.v("click", "negative");
}
});
builder.show();
}
I think that the problem can be that you inflate a layout, which returns a new view. This new view has different reference than this original and when you set the listener, it affects only at this new, not this you want. Maybe you should set this new view to newly created Alert Dialog?
Have you tried something like this (instead of builder.show())?
AlertDialog alertDialog = builder.create();
alertDialog.setView(viewInflater);
alertDialog.show();
It helped me so much when I wanted do set my custom view to 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
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