Mr_redpants
Mr_redpants

Reputation: 277

didnt work DialogFragment with 1 button.

Why doesn't my code work?

I need dialogFragment with 1 button.


I have 2 classes:

   public class MyAlertDialogFragment extends DialogFragment {

 static DialogFragment newInstance(int num) {
        MyAlertDialogFragment f = new MyAlertDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }


 public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
     View v = inflater.inflate(R.layout.alertdialog, container, false);
     Button button = (Button)v.findViewById(R.id.button1);

     return v;
 }

}

and Activity:

   public class DialogFragmentActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
showDialog();
}

void showDialog() {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    DialogFragment newFragment = MyAlertDialogFragment.newInstance(1);
    newFragment.show(ft, "dialog");
}

    }   

How do you complete this fragment?

Upvotes: 0

Views: 1193

Answers (1)

Shinyosan
Shinyosan

Reputation: 357

Don't override onCreateView, override onCreateDialog:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity())
            .setPositiveButton(android.R.string.ok, null);
    return alert.create();
}

Upvotes: 1

Related Questions