Reputation: 277
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
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