Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I put a seek bar in an alert dialog?

I want an alert dialog box to pop up after I click a button to have a seek bar so that the person can change the value from 1-48. I've made a custom xml that has a seek bar and two text view and I would like the dialog box to have two buttons, one just cancels the dialog and the other does more work. Here is the code I have so far.

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <SeekBar android:max="48" android:layout_height="wrap_content" android:id="@+id/seekBar1" android:layout_width="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginTop="74dp"></SeekBar>
    <TextView android:text="Change Hour Range" android:layout_height="wrap_content" android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></TextView>
    <TextView android:text="Only most recent positions" android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="15dp"></TextView>

</RelativeLayout>

Here is the alert dialog that I have so far

new AlertDialog.Builder(ImTracking.this)
                    //is there something like setcontentview for alert dialogs?

                    .setPositiveButton("Cancel", null)
                    .setNegativeButton("OK",
                            new DialogInterface.OnClickListener() {
 // does some work here
like image 717
Sean Avatar asked Aug 25 '11 01:08

Sean


2 Answers

Yep builder.setView(View v); here is how you can use it.

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot));
    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
        }
    });

Edit: Added progressListener to sample code. Do note that you cannot get a reference to your SeekBar until after you call alertDialog.show(), if the SeekBar is not being shown findViewById() will return null. Also note that you must use layout.findViewById(); because the SeekBar is a child of the RelativeLayout that 'layout' is a reference to.

like image 68
FoamyGuy Avatar answered Sep 20 '22 00:09

FoamyGuy


    //This is the code you seek!

 public void ShowDialog(){
   final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
 final SeekBar seek = new SeekBar(this);
   seek.setMax(255);
  seek.setKeyProgressIncrement(1);

 popDialog.setIcon(android.R.drawable.btn_star_big_on);
popDialog.setTitle("Please Select Into Your Desired Brightness ");
popDialog.setView(seek);


 seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {



 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){


  txtView.setText("Value of : " + progress);
  }

Or you can look on this tutorial

like image 37
dondondon Avatar answered Sep 23 '22 00:09

dondondon