Reputation: 9171
This is my layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<net.simonvt.widget.NumberPicker
android:id="@+id/numberPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" >
</net.simonvt.widget.NumberPicker>
</RelativeLayout>
It shows a custom picker in my app. But I want to show this picker inside a dialog.
This is my custom dialog:
public class NumberPickerCustomDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Dialog")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).;
return builder.create();
}
How can I put my picker in this dialog?
Thank you!
Upvotes: 1
Views: 6515
Reputation: 11403
You need to provide a custom layout to the dialog, to do so grab the LayoutInflater service and use it to inflate your layout.
public class NumberPickerCustomDialog extends DialogFragment {
Context context;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// get context
context = getActivity().getApplicationContext();
// make dialog object
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// get the layout inflater
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate our custom layout for the dialog to a View
View view = li.inflate(R.layout.my_custom_view, null);
// inform the dialog it has a custom View
builder.setView(view);
// and if you need to call some method of the class
MyCustomView myView = (MyCustomView) view.findViewById(R.id.custom_id_in_my_custom_view);
myView.doSome("stuff");
// create the dialog from the builder then show
return builder.create();
}
}
Upvotes: 3