9

I'm trying to insert a SeekBar in a Dialog to change the brightness. To do this, I wrote this code.

public class Lum extends DialogFragment{


        private SeekBar brightbar;
        private int brightness;
        private ContentResolver cResolver;
        private Window window;
        TextView txtPerc;
        boolean stato;
        Context context;

        public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the Builder class for convenient dialog construction

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            LayoutInflater inflater = getActivity().getLayoutInflater();
            View view = inflater.inflate(R.layout.dialog_brightness, null);
            builder.setView(view);

        brightbar = (SeekBar) view.findViewById(R.id.brightbar);
        txtPerc = (TextView) view.findViewById(R.id.txtPercentage);
        //cResolver = getContentResolver();
        //window = getWindow();
        brightbar.setMax(255);
        brightbar.setKeyProgressIncrement(1);

        brightbar = (SeekBar) view.findViewById(R.id.brightbar);
        txtPerc = (TextView) view.findViewById(R.id.txtPercentage);
        cResolver = getContentResolver();
        window = getWindow();
        brightbar.setMax(255);
        brightbar.setKeyProgressIncrement(1);

        try {
            brightness = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        } catch (SettingNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        float perc = (brightness /(float)255)*100;
        txtPerc.setText((int)perc +" %");

        brightbar.setProgress(brightness);
        brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
        {
            public void onStopTrackingTouch(SeekBar seekBar)
            {
                Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
                LayoutParams layoutpars = window.getAttributes();
                layoutpars.screenBrightness = brightness / (float)255;
                window.setAttributes(layoutpars);
            }

            public void onStartTrackingTouch(SeekBar seekBar)
            {

            }

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


                    brightness = progress;

                    float perc = (brightness /(float)255)*100;
                    txtPerc.setText((int)perc +" %");
            }
        });     

            builder.setTitle("Brightness")
                   .setPositiveButton("Close", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           dismiss();
    }
                   });

            // Create the AlertDialog object and return it
            return builder.create();
        }
    }

The problem is that my IDE Eclipse marks errors in getContentResolver and getWindow because I'm working on a DialogFragment and not an Activity. How can I do to use this instruction in this class? Or, if you can not do that, there is an alternative? Thanks in advance

Mario G.
  • 393
  • 1
  • 3
  • 16

1 Answers1

24

http://developer.android.com/reference/android/content/Context.html#getContentResolver()

getContentResolver() needs a context.

You have context.getContentResolver(). Use getActivity().getContentResolver().

http://developer.android.com/reference/android/app/Activity.html#getWindow()

Similarly use getActivity().getWindow()

getActivity()

Return the Activity this fragment is currently associated with.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @user3027663 you are welcome. you can check the docs for more info – Raghunandan Nov 24 '13 at 16:43
  • Cool!! Thank you very much. Do you think you could help me with this question : http://stackoverflow.com/questions/25549679/how-can-i-split-a-long-single-sqliteopenhelper-into-serveral-classes-one-for-e – eddy Aug 28 '14 at 14:03