I want to show a dialog inside another dialog box it's working fine but When I click ADD or EDIT button there is no response. I have posted the code below
private Dialog myTextDialog(final String title) {
final AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setView(getCurrentFocus());
builder.setPositiveButton("ADD", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myAddCategory(title);
}
});
builder.setNegativeButton("EDIT", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mAdapter = new MyExpandableListAdapter(MyGraphicalActivity.this, listOptionGroup, listOptionChild);
}
});
return builder.create();
}
private Dialog myAddCategory(final String title) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View viewMessEdit = inflater.inflate(R.layout.add_category,(ViewGroup) findViewById(R.id.alertlayout));
builder.setView(viewMessEdit);
//viewMessEdit.setBackgroundResource(R.color.grayy);
builder.setPositiveButton("ADD", new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
final EditText savedText = ((EditText) viewMessEdit.findViewById(R.id.inputbox));
newcategory=savedText.getText().toString();
newcategory = checkCategory(newcategory);
if(newcategory!="null" && newcategory.length()>0)
{
Pattern pattern;
pattern=Pattern.compile("[A-Z | a-z]*");
Matcher matcher=pattern.matcher(newcategory);
if(!matcher.matches())
{
myTextDialog(title).show();
Toast.makeText(getBaseContext(),"Invalid Category name!",Toast.LENGTH_LONG).show();
}
else
{
Intent addcategorylist_intent=new Intent(MyGraphicalActivity.this,addcategorylist.class);
startActivity(addcategorylist_intent);
Toast.makeText(getBaseContext(), "ADDED", Toast.LENGTH_SHORT).show();
}
}
else
{
myTextDialog(title).show();
Toast.makeText(getBaseContext(),"You Can not give Blank & already specified Category!",Toast.LENGTH_LONG).show();
}
}
});
builder.setNegativeButton("CANCEL",new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
Change the following code
builder.setPositiveButton("ADD", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myAddCategory(title);
}
});
to
builder.setPositiveButton("ADD", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myAddCategory(title).show();
}
});
Related
i'm trying to make an EditText field that onClick opens a numberPicker so the user can input a number.
this is my code:
tipET.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
final NumberPicker numberPicker = new NumberPicker(getContext());
numberPicker.setMaxValue(1000);
numberPicker.setMinValue(0);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setView(numberPicker);
builder.setTitle("tip");
builder.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log.d(TAG, "TIP NUMBER IS:" + numberPicker.getValue());
num = numberPicker.getValue();
}
});
builder.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
builder.create();
builder.show();
}
});
tipET.setText(String.valueOf(num));
for some reason tipET text stays the same after i input another number, why is that?
also for the first time i click on the EditText the keyboard pops up, and only on the second click my NumberPicker opens, how can i prevent that?
Add tipET.setText(String.valueOf(num)); to the onClick method for the positive button, right below num = numberPicker.getValue();, like this:
tipET.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
final NumberPicker numberPicker = new NumberPicker(getContext());
numberPicker.setMaxValue(1000);
numberPicker.setMinValue(0);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setView(numberPicker);
builder.setTitle("tip");
builder.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log.d(TAG, "TIP NUMBER IS:" + numberPicker.getValue());
num = numberPicker.getValue();
// update the text of the EditText here
tipET.setText(String.valueOf(num));
}
});
builder.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
builder.create();
builder.show();
}
});
So without having to create a custom dialog as I currently have a number of dialog layouts and don't want to have to do it for each one, is there a way to prevent this dialog from closing when the positive button is pressed and the EditText is empty?
Currently it closes the dialog every time I hit enter and there is nothing in the EditText field.
public AlertDialog webpageDialog() {
AlertDialog.Builder webpageDialogBuilder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
webpageDialogBuilder.setView(inflater.inflate(R.layout.dialog_webpage, null))
.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
final AlertDialog webpageDialog = webpageDialogBuilder.create();
webpageDialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getString(R.string.enter), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
EditText webpageInput = (EditText) webpageDialog.findViewById(R.id.dw_et_webpage_address);
Log.d(TAG, "Positive on click");
if(TextUtils.isEmpty(webpageInput.getText().toString())){
Log.d(TAG, "Edit text empty");
webpageInput.setError(context.getString(R.string.error_web_required));
} else {
Log.d(TAG, "Edit text not empty");
ms.setUriString("http://" + webpageInput.getText().toString());
ms.returnWithResult(1);
dialog.cancel();
}
Log.d(TAG, "Returning");
}
});
Log.d(TAG, "Returning dialog");
return webpageDialog;
}
this is how I'm doing it.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
EditText yourEditText = new EditText(this);
layout.addView(yourEditText);
builder.setView(layout);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
final AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ("".equals(yourEditText.getText().toString().trim())) {
//this will stop your dialog from closing
yourEditText.setError("This field is required!");
return;
}
//you logic here
dialog.dismiss();
}
});
I hope following line can help,
webpageDialogBuilder.setCancelable(false);
I've been struggling with this for a while now, looking for any tips how to proceed. I'm trying to create a fragment that has an add button, that will create a new textview and when clicking the textview , you would have the option to delete it.
Adding textviews works fine. The problem is in the foreach loop that sets up listeners for each textview and adds the option to delete them. It's forcing me to declare the current parameter final, or the deletion won't work. But if I do declare it final, then I can't add any new textviews to the list, so they wont have listeners.
So if anyone could give me some kind of a hint, that would be hugely appreciated.
Here's the list and the adding part.
//Variables
List<TextView> TextViews = new ArrayList<TextView>();
#Override
//Put button functionality here
public void onClick(View v) {
final LinearLayout myLayout = (LinearLayout) view.findViewById(R.id.linearlayout);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Title");
// Set up the input/
final EditText input = new EditText(getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
notesIndex = new Integer(notesIndex + 1);
noteText = input.getText().toString();
TextView a = new TextView(view.getContext());
a.setText(noteText);
a.setHeight(150);
a.setGravity(Gravity.CENTER);
myLayout.addView(a);
TextViews.add(a);
noteTexts.add(noteText);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
And here is the foreach loop that sets up listeners for each TextView. They are both called in OnCreateView.
for (final TextView current : TextViews) {
current.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Delete?");
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
myLayout.removeView(current);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
});
}
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
addViewToLayout();
}
});
void addViewToLayout(){
notesIndex = new Integer(notesIndex + 1);
noteText = input.getText().toString();
TextView a = new TextView(view.getContext());
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAlertToDelete(v)
}
});
a.setText(noteText);
a.setHeight(150);
a.setGravity(Gravity.CENTER);
myLayout.addView(a);
TextViews.add(a);
noteTexts.add(noteText);
}
void showAlertToDelete(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Delete?");
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
myLayout.removeView(v);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
I want to add dialog box in my app to let the user put his/her desire Ip address.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Please put Ip address")
------> here the user can type on the dialog in String
.setNeutralButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
printer = new PrinterObject("134.188.204.155");--->the result text from dialog
....
}
});
AlertDialog alert = builder.create();
alert.show();
Anybody knows how to add it?
Try this...
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("Please put Ip address");
builder.setMessage("");
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.alert, null);
final EditText ipfield = (EditText) view.findViewById(R.id.ipfield);
builder.setView(view);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
// do what you need
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
You can use this snippet:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Please put Ip address");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
So this is my Dialog class:
public class SecondActivity extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.second, null))
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismiss();
}
});
return builder.create();
}
}
In my dialog there are 2 edit texts which get 2 strings. I want to use those 2 strings in my MainActivity if the user presses on the save button. How do I do that?
The inflate method returns a view where you can execute a findViewById, like the following:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.second, null);
EditView editView = (EditView)v.findById(R.id.your_edit_view);
builder.setView(v)
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismiss();
}
});
return builder.create();
}