user2097111
user2097111

Reputation: 49

Call dialog from another Class

I got the following :

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

Answers (4)

Monty
Monty

Reputation: 3215

Create static method and put your code inside .

and call directly by class name i.e Dcon.myAlertMessage()

Upvotes: -1

Marko Niciforovic
Marko Niciforovic

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

trojantale
trojantale

Reputation: 306

Follow the android document here : http://developer.android.com/reference/android/app/DialogFragment.html

Upvotes: -1

Chris.Jenkins
Chris.Jenkins

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

Related Questions