2

I have to build at run-time a custom dialog. Depending on the situation, the dialog should show from one to four seek bars. I would like to obtain something like:

  • title1 seekbar1
  • title2 seekbar2 ...

I was experimenting with the FireMissilesDialogFragment example code taken from the Android developer guide webpage. I have modified the onCreateDialog() method adding the following code:

    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    Context     context = builder.getContext();//getActivity().getBaseContext(); 
    LinearLayout    layout = new LinearLayout( context );

    layout.setLayoutParams( new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT) );


    SeekBar         seek_bar = new SeekBar( context );

    TextView        text = new TextView( context );
    text.setClickable(false);
    text.setText( "slide me" );

    layout.addView( text, new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT) );

    layout.addView( seek_bar, new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT) );


    builder.setView( layout );

But instead of displaying in two different rows the text 'slide me' and the seek bar, it displays only the text. How can i create such a custom dialog?

Marco Masci
  • 818
  • 10
  • 22

2 Answers2

0

Android Dialogs are somewhat limited in what they can contain. It's easier to create an activity, and tell android to show it as a dialog:

  1. Create a new activity and add a layout file with these sliders

  2. In the manifest, use this line

<activity android:theme="@android:style/Theme.Dialog">

To have the activity appear as a dialog. Can't go wrong with this approach.
Also, this way if you need to get a result from the dialog, you can use startActivityForResult

Community
  • 1
  • 1
Jameo
  • 4,507
  • 8
  • 40
  • 66
0

You need to make a layout first with seek bar and a text view. The you can use layout inflater to add it to you diloge box. Try this....

 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.MyLayout));
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setView(layout);
AlertDialog alertDialog = builder.create();
alertDialog.show();
SeekBar sb = (SeekBar)layout.findViewById(R.id.yourSeekBar);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
        //Do something here with new value
    }
});