Skip to content Skip to sidebar Skip to footer

Android, Seekbar In Dialog

I would like to use a dialog with a seekbar in my application. But I don't really know how to do it because I lack experience with android. So, when you press a button: a dialog sh

Solution 1:

Maybe you could think of create a custom dialog; it requires more work but it usually works for me ;) Create a new layout file for your dialog (say, your_dialog.xml):

<RelativeLayoutandroid:id="@+id/your_dialog_root_element"android:layout_width="wrap_content"android:layout_height="wrap_content"
><SeekBarandroid:id="@+id/your_dialog_seekbar"android:layout_width="fill_parent"android:layout_height="wrap_content"
    ></SeekBar><Buttonandroid:id="@+id/your_dialog_button"android:layout_width="wrap_content"android:layout_height="wrap_content"
    ></Button>

Then, in your activity:

DialogyourDialog=newDialog(this);
LayoutInflaterinflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
Viewlayout= inflater.inflate(R.layout.your_dialog, (ViewGroup)findViewById(R.id.your_dialog_root_element));
yourDialog.setContentView(layout);

Thus, you can operate on your element as follows:

ButtonyourDialogButton= (Button)layout.findViewById(R.id.your_dialog_button);
SeekBaryourDialogSeekBar= (SeekBar)layout.findViewById(R.id.your_dialog_seekbar);
// ...

and so on, to set listeners for button and seekbar.

EDIT: The seekbar onchangelistener should be as follows:

OnSeekBarChangeListener yourSeekBarListener = newOnSeekBarChangeListener() {
    @OverridepublicvoidonStopTrackingTouch(SeekBar seekBar) {
            //add code here
    }

    @OverridepublicvoidonStartTrackingTouch(SeekBar seekBar) {
            //add code here
    }

    @OverridepublicvoidonProgressChanged(SeekBar seekBark, int progress, boolean fromUser) {
            //add code here
    }
 };
 yourDialogSeekBar.setOnSeekBarChangeListener(yourSeekBarListener);

Solution 2:

I hope this will help you. Try this Code...

final AlertDialog.Builderalert=newAlertDialog.Builder(this); 

    alert.setTitle("Alert Box"); 
    alert.setMessage("Edit Text"); 

    LinearLayout linear=newLinearLayout(this); 

    linear.setOrientation(1); 
    TextView text=newTextView(this); 
    text.setText("Hello Android"); 
    text.setPadding(10, 10, 10, 10); 

    SeekBar seek=newSeekBar(this); 

    linear.addView(seek); 
    linear.addView(text); 

    alert.setView(linear); 



    alert.setPositiveButton("Ok",newDialogInterface.OnClickListener() 
    { 
        publicvoidonClick(DialogInterface dialog,int id)  
        { 
            Toast.makeText(getApplicationContext(), "OK Pressed",Toast.LENGTH_LONG).show(); 
            finish(); 
        } 
    }); 

    alert.setNegativeButton("Cancel",newDialogInterface.OnClickListener()  
    { 
        publicvoidonClick(DialogInterface dialog,int id)  
        { 
            Toast.makeText(getApplicationContext(), "Cancel Pressed",Toast.LENGTH_LONG).show(); 
            finish(); 
        } 
    }); 

    alert.show(); 

Solution 3:

This is the code on how to put seekbar in alertdialog: check this link.

publicvoidShowDialog(){
 final AlertDialog.BuilderpopDialog=newAlertDialog.Builder(this);
 finalSeekBarseek=newSeekBar(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(newSeekBar.OnSeekBarChangeListener() {



 publicvoidonProgressChanged(SeekBar seekBar, int progress, boolean fromUser){


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

HAPPY CODING!

Solution 4:

Create a view having edittext. use setView() method of AlertBuilder to set the view.

mytest.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:textColor="#000000"
        android:hint="8+ characters"
        android:maxLines="1"
        android:maxLength="18"
        android:imeOptions="actionSend|flagNoEnterAction" />

</RelativeLayout>



    AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(entryView);
    builder.setMessage("Are you sure you want to exit?")
            .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               publicvoidonClick(DialogInterface dialog, int id) {
                    MyActivity.this.finish();
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           publicvoidonClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });
    AlertDialog alert = builder.create();

Post a Comment for "Android, Seekbar In Dialog"