Live update Screen brightness with SeekBar

So, I have a code that adjusts the brightness of the system, and I implemented the search bar correctly, but when I adjust the search bar, the brightness of the screen does not change. I looked through a few other posts, and it looks like the solution is to create a Dummy class that quickly opens and closes. If I do this, then I will feel that there will be a ton of delay, because the screen will be constantly updated as the arrow moves.

@Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { ContentResolver cr = getContentResolver(); try{ int brightness = Settings.System.getInt(cr,Settings.System.SCREEN_BRIGHTNESS); Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS, brightness); WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = brightness / 255.0f; getWindow().setAttributes(lp); } catch (Settings.SettingNotFoundException a) { } } @Override public void onStartTrackingTouch(SeekBar seekBar) { //Toast.makeText(MainActivity.this, "Started Tracking Seekbar", Toast.LENGTH_SHORT).show(); } @Override public void onStopTrackingTouch(SeekBar seekBar) { } 

Another problem with the update is that I run this brightness control on the screen, where there is a preview of the camera and killing the camera every time it takes a while to load the dummy screen and further increase the delay

I saw this post, but I could not figure out how to implement it. Since I do not use settings

I have a pointer in my preference for updating screen brightness. Is there a way to update the user interface to see the changes in real time?

+4
source share
2 answers

I solved the problem with the following code, which now works as intended!

 @Override public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { // TODO Auto-generated method stub BackLightValue = (float)arg1/100; WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.screenBrightness = BackLightValue; getWindow().setAttributes(layoutParams); } @Override public void onStartTrackingTouch(SeekBar arg0) { } @Override public void onStopTrackingTouch(SeekBar arg0) { int SysBackLightValue = (int)(BackLightValue * 255); android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, SysBackLightValue); } 
+3
source

Use my following code, which works fine:

in any XML file:

  <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:background="?android:attr/selectableItemBackground" android:max="10" android:progressDrawable="@drawable/progress" android:scaleType="fitCenter" android:thumb="@drawable/thumb" /> 

note: accept attributes as much as you need in the search bar.

in any of your actions use the following method and call this:

  private void setSeekbar() { seekBar = (SeekBar) findViewById(R.id.seekBar1); seekBar.setMax(255); float curBrightnessValue = 0; try { curBrightnessValue = android.provider.Settings.System.getInt( getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS); } catch (SettingNotFoundException e) { e.printStackTrace(); } int screen_brightness = (int) curBrightnessValue; seekBar.setProgress(screen_brightness); seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { int progress = 0; @Override public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) { progress = progresValue; } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, progress); } }); } 
+3
source

Source: https://habr.com/ru/post/1485509/


All Articles