How to pass values from DIalog class to it's host - java

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

Related

Clickable text link in Alert Dialog to open Activity

I want one word of the text in my Alert Dialog to be clickable and open an activity on click.
I tried to use a ClickableSpan in a SpannableString, but the click is not recognized at all...
public class TestDialog extends AppCompatDialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String text = "This is a test message.";
SpannableString ss = new SpannableString(text);
ClickableSpan cs = new ClickableSpan() {
#Override
public void onClick(#NonNull View view) {
System.out.println("Test");
}
};
ss.setSpan(cs, 10, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(ss)
.setTitle("Test Title")
.setPositiveButton("Ok!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// do something
}
})
.setNegativeButton("No!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// do something
}
});
return builder.create();
}
}
Thanks for your help!
Ok, I did it with the help of several other posts here and youtube videos.
I use a Custom Layout showing text combined with clickable spans which I then show in the Alert Dialog:
public class TestDialog extends AppCompatDialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String text = "This is a test message.";
SpannableString ss = new SpannableString(text);
ClickableSpan cs = new ClickableSpan() {
#Override
public void onClick(#NonNull View view) {
Intent testIntent = new Intent(getContext(), Test.class);
getContext().startActivity(testIntent);
}
};
ss.setSpan(cs, 10, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.test_view, null);
TextView message = (TextView) view.findViewById(R.id.textView);
message.setMovementMethod(LinkMovementMethod.getInstance());
message.setText(ss);
builder.setView(view)
.setTitle("Test Title")
.setPositiveButton("Ok!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// do something
}
})
.setNegativeButton("No!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// do something
}
});
return builder.create();
}
}
I hope I could also help you.
First, Make your text HTML links clickable by using Html.fromHtml:
.setMessage(Html.fromHtml(text))
and, Add this line to your Activity in Manifest:
<data android:host="my.app" android:scheme="http"></data>
Then, http://my.app should launch your activity.

Unable to create RecyclerView inside AlertDialog, content inside Alert Dialog is blank

I am trying to display two RecyclerViews inside alert dialog contents. Please Help what am I doing wrong?
The onCreateViewHolder function is not being called
Please note that the RecyclerViews are inside the ConstraintLayout in the xml. If that is causing them not to display what should be the correct procedure?
public class MainActivity extends AppCompatActivity {
private View inflatedView;
private RecyclerView fontList,sizeList;
private String[] fonts = {"normal","sans","serif","monospace"};
private String[] sizes = {"14","16","18","20","22","24"};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
inflatedView = getLayoutInflater().inflate(R.layout.design_style,null);
builder.setView(inflatedView).setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"OK Pressed",Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"Cancel",Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
fontList = inflatedView.findViewById(R.id.fontlist);
sizeList = inflatedView.findViewById(R.id.sizelist);
fontList.setLayoutManager(new LinearLayoutManager(this));
fontList.setAdapter(new MyAdapter(Arrays.asList(fonts)));
sizeList.setLayoutManager(new LinearLayoutManager(this));
sizeList.setAdapter(new MyAdapter(Arrays.asList(sizes)));
}
private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
private List<String> itemList;
public MyAdapter(List<String> itemList) {
this.itemList = itemList;
Log.d("lol","MyAdapter");
}
#NonNull
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.design_fontcell,viewGroup);
Log.d("lol","onCreateViewHolder");
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.mtextView.setText(itemList.get(i));
Log.d("lol",itemList.get(i));
}
#Override
public int getItemCount() {
Log.d("lol","getItemCount: "+itemList.size());
return itemList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView mtextView;
MyViewHolder(View cellView){
super(cellView);
mtextView = cellView.findViewById(R.id.textView_font);
}
}
}
}
Change your code like this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
inflatedView = getLayoutInflater().inflate(R.layout.design_style,null);
//No need of setView() for buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"OK Pressed",Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"Cancel",Toast.LENGTH_SHORT).show();
}
});
fontList = inflatedView.findViewById(R.id.fontlist);
sizeList = inflatedView.findViewById(R.id.sizelist);
fontList.setLayoutManager(new LinearLayoutManager(this));
fontList.setAdapter(new MyAdapter(Arrays.asList(fonts)));
sizeList.setLayoutManager(new LinearLayoutManager(this));
sizeList.setAdapter(new MyAdapter(Arrays.asList(sizes)));
builder.setView(inflatedView); //Set the view at the end
AlertDialog dialog = builder.create();
dialog.show();

How to use input dialog box

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

What's wrong in my alert dialog builder class?

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

Android - retrieving text input from an alertbuilder dialog

I've got a View defined in an xml file. It contains two Edittext fields (amongt other things like text)
I use an AlertBuilder to trigger a dialog where a user enters text(such as username and pass) into both edittext fields. When I try to retrieve the strings and send them to Login(), both strings are just null. What is going on?
It seems like somehow the string data isn't saved?
Here's when I show the Dialog in my app:
SignInDialog.show(ScreenMain.this,
"Login",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
LayoutInflater inflater = (LayoutInflater) ScreenMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_dialog_login, null);
LogIn(((EditText) layout.findViewById(R.id.screen_dialog_login_username_edit)).getText().toString(),
((EditText) layout.findViewById(R.id.screen_dialog_login_password_edit)).getText().toString());
}
},
"Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
Here's a class I use to instantiate a Dialog:
/* login dialog*/
static class SignInDialog {
public static void show(Context context, String positiveText, DialogInterface.OnClickListener positive, String negativeText, DialogInterface.OnClickListener negative){
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_dialog_login, null);
builder = new AlertDialog.Builder(context);
builder.setView(layout);
if(positive != null && positiveText != null){
builder.setPositiveButton(positiveText, positive);
}
if(negative != null && negativeText != null){
builder.setNegativeButton(negativeText, negative);
}
builder.create().show();
}
}
To inflate a layout is to create a new instance of it. (You're not receiving a reference to an existing instance.) So, in your onClick you are creating a new copy of the layout and your fields don't contain any text because they are not the same ones your user just entered text in.
Why not just completely subclass AlertDialog.Builder and add a method to retrieve the EditText values?
Do something like:
View layout = inflater.inflate(R.layout.screen_dialog_login, null);
layout.findViewById(R.id.*yourwidget*);
i tried it and it helped
Here is the method I am using:
private void showPopUp3() {
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(AlarmReceiverActivity.this);
helpBuilder.setTitle("hi");
// helpBuilder.setMessage("This is a Simple Pop Up");
final EditText input = new EditText(this);
input.setHeight(20);
input.setText("");
LayoutInflater inflater = getLayoutInflater();
final View checkboxLayout = inflater.inflate(R.layout.alarm, null);
checkboxLayout.findViewById(R.id.Yes).setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// setTitle("button2");
checkboxLayout.findViewById(R.id.note).setVisibility(View.VISIBLE);
}
});
checkboxLayout.findViewById(R.id.No).setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// setTitle("button2");
checkboxLayout.findViewById(R.id.note).setVisibility(View.INVISIBLE);
}
});
helpBuilder.setView(checkboxLayout);
helpBuilder.setPositiveButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
mMediaPlayer.stop();
finish();
}
});
helpBuilder.setNegativeButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
mMediaPlayer.stop();
//showSimplePopUp();
}
});
// Remember, create doesn't show the dialog
AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
}

Categories