Cam Connor
Cam Connor

Reputation: 1231

how to create custom alert diaogue

This is the code google says to use to create a custom alert dialogue (it says to create your own layout and then use that layout for setContentView)

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
       .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int id) {
               // sign in the user ...
           }
       })
       .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               LoginDialogFragment.this.getDialog().cancel();
           }
       });      
return builder.create();
}

google then says to make an instance of this class in another class then show it with the show() method but the show method needs a fragment manager and the fragment manager gets an error when you try to create it.

here are the instructions http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout

And I need to add a spinner to the alert dialogue that starts a new activity

If someone could figure out how to get this working (make the dialogue pop up) and post it that would be great

Upvotes: 0

Views: 95

Answers (1)

Ronak Mehta
Ronak Mehta

Reputation: 5979

You can create Alert Dialog with spinner this way

Updated

 public class WvActivity extends Activity {

 TextView tx;
 String[] s = { "India ", "Arica", "India ", "Arica", "India ", "Arica",
    "India ", "Arica", "India ", "Arica" };
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 final ArrayAdapter<String> adp = new ArrayAdapter<String>(WvActivity.this,
        android.R.layout.simple_spinner_item, s);

 tx= (TextView)findViewById(R.id.txt1);
 final Spinner sp = new Spinner(WvActivity.this);
 sp.setLayoutParams(new     LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
 sp.setAdapter(adp);

 AlertDialog.Builder builder = new AlertDialog.Builder(WvActivity.this);
 builder.setView(sp);
 builder.create().show();
 }
}

Upvotes: 1

Related Questions