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

强颜欢笑 提交于 2019-11-30 14:01:02

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.

    //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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!