Reputation: 49
I got the following :
A class called DConce
which contains the code of one dialog I'm gonna use:
public class DConce extends DialogFragment{
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder dshow = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
dshow.setView(inflater.inflate(R.layout.dialogconc, null))
.setPositiveButton("Send", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//TODO
}
})
.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
DConce.this.getDialog().cancel();
}
});
return dshow.create();
}
and another class into a Fragment called public final class Sincr extends Fragment
where I need to call the dialog on a :
case R.id.btnConce:
//here
break;
How would I do that?
thanks in advance.
Upvotes: 2
Views: 2455
Reputation: 3215
Create static method and put your code inside .
and call directly by class name i.e Dcon.myAlertMessage()
Upvotes: -1
Reputation: 3591
What you need to add to your code is newInstance like:
public static DConce newInstance(){
DConce arg = new DConce();
return arg;
}
And then in activity:
FragmentManager manager = getSupportFragmentManager(); // or getFragmentManager, depends on which api lvl you are working on but supportFragmentManager will make you dialog work also on devices lower than api lvl 11(3.0 - > Honeycomb)
DialogFragment Dialog = DConce.newInstance();
Dialog.show(manager, "tag");
Upvotes: 2
Reputation: 306
Follow the android document here : http://developer.android.com/reference/android/app/DialogFragment.html
Upvotes: -1
Reputation: 13129
Modify your DialogFragment:
public class DConce extends DialogFragment{
public static DConce newInstance(){
DConce f = new DConce();
return f;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder dshow = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
dshow.setView(inflater.inflate(R.layout.dialogconc, null))
.setPositiveButton("Send", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//TODO
}
})
.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
DConce.this.getDialog().cancel();
}
});
return dshow.create();
}
}
Then to show it;
case R.id.btnConce:
DConce.newInstance().show(getChildFragmentManager(), null);
break;
You can of course optionally pass in a String
for the tag
parameter, to uniquely identify the fragment.
Upvotes: 2