user902080
user902080

Reputation: 163

How to get a spinner into an alertbox/dialog?

I am trying to put a spinner into an alert box and would much appreciate it, if someone would point me in the direction of a tutorial or show some code on how this can be done.

Upvotes: 0

Views: 3844

Answers (3)

Alê Oliveira
Alê Oliveira

Reputation: 6439

You can do it like this:

// ProgressBar properties
RelativeLayout.LayoutParams progressParams = new RelativeLayout.LayoutParams(Patterns.PROGRESS_BAR_WIDTH, Patterns.PROGRESS_BAR_WIDTH);
progressParams.addRule(RelativeLayout.CENTER_VERTICAL);
progressParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

mProgress = new ProgressBar(context);
mProgress.setIndeterminate(true); 
rootLayout.addView(mProgress,progressParams);
mProgress.setVisibility(View.VISIBLE);

Where rootLayout is your Activity's layout where you want to put the spinning "box". The LayoutParams that I used is just to place the box in the layout's center. When your box is no longer necessary, you can dismiss it like this:

mProgress.setVisibility(View.GONE);
layoutBg.removeView(mProgress);

Upvotes: 1

NSjonas
NSjonas

Reputation: 12032

  1. Create an xml layout with a spinner
  2. in your code:

    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.layoutname);
    
  3. you can access the spinner like this

    Spinner spin = (Spinner)dialog.findViewById(R.id.spinnerid);
    

Upvotes: 2

S9Designs
S9Designs

Reputation: 271

If you're using an alert dialog, you can add a custom layout containing your Spinner to your existing dialog.

To see an example of this, look for the "DIALOG_TEXT_ENTRY" case in this example: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlertDialogSamples.html

Upvotes: 1

Related Questions