I want a dialog that adjusts brightness of the screen using SeekBar.
How to display custom dialog with SeekBar that adjusts brightness of screen?
You can use this API. This will change screen brightness of your window and not of the whole system.
// Make the screen full bright for this activity.
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 1.0f;
getWindow().setAttributes(lp);
This code snippet should help you
pdlg = ProgressDialog.show(getParent(), "Loading...", "");
WindowManager.LayoutParams lp = pdlg.getWindow().getAttributes();
p.dimAmount=0.0f; //Dim Amount
pdlg.getWindow().setAttributes(lp); //Applying properties to progress dialog
pdlg.dismiss(); //Dismiss the dialog
Hey you can set the system brightness like this:
//Set the system brightness using the brightness variable value
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
//Get the current window attributes
LayoutParams layoutpars = window.getAttributes();
//Set the brightness of this window
layoutpars.screenBrightness = brightness / (float)255;
//Apply attribute changes to this window
window.setAttributes(layoutpars);
Or you may refer on this complete tutorial
Related
I'm trying to set the screen brightness from my app, but as soon the screen rotates (Auto-Rotate) my brightness is reset to the systems default brightness.
The code I'm using is following:
final WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
lp.screenBrightness = 0.5f;
((Activity) context).getWindow().setAttributes(lp);
((Activity) context).startActivity(new Intent(context, DummyActivity.class));
This is happening because your activity is restarting.
You can try adding your window settings code in onCreate of your activity.
Make sure that this code is added before setting the view of the activity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 0.5f;
getWindow().setAttributes(lp);
setContentView(R.layout.your_layout_id);
}
I want to achieve the following design: A dialog with custom layout, created programatically. It will contain an EditText and a Button. I want the soft keyboard to pop up when the dialog appears, and I want the dialog to fill the screen horizontally and to be placed right above the keyboard.
Here is what I've done right now:
final AlertDialog obsDialog = new AlertDialog.Builder(ProdutoDetalheActivity.this).create();
final View obsLayout = View.inflate(getApplicationContext(), R.layout.observation_layout, null);
Button obsButton = (Button) obsLayout.findViewById(R.id.observation_button);
obsEdit = (EditText) obsLayout.findViewById(R.id.observation_edit);
obsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText(String.valueOf(obsEdit.getText()));
obsDialog.dismiss();
}
});
obsDialog.setView(obsLayout);
obsDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
obsDialog.show();
obsEdit.requestFocus();
But this doesn't makes the dialog full width, nor calls the soft keyboard. And I still wonder how can I align the dialog with the keyboard.
I've tried these answers with no success.
Thanks in advance for any help!
[EDIT] I've brought the keyboard up by using the following code:
obsDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
obsDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
[EDIT] Below is a printscreen of the design I want to achieve:
As far as I know, you have to adjust the dialog size/position in the code if you do not want the default size/position.
In you case specifically, you can fist declare adjustResize for the activity's windowSoftInputMode in the AndroidManifest.xml.
After that, you can set your dialog gravity at bottom and set the width in the onStart of the dialog fragment:
...
getDialog().getWindow().setLayout(mWidth, mHeight);
WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
lp.gravity = Gravity.BOTTOM;
lp.x = 0; lp.y = 0;
getDialog().getWindow().setAttributes(lp);
...
the mWidth and mHeight are initialized in onCreate, but I think you can do it in the onStart as well. To get the width, I used below code:
Point size = new Point();
WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
display.getSize(size);
mWidth = size.x;
Hope this can meet your requirement. I used this to create a similar dialog, but I do not have a edit in the dialog. And I need it to be at the bottom of the screen.
I am creating an Android application. In that application, I am using
a Dialog window to display HTML contents which come from the sever
side. I created the Dialog window using below method.
private void displayDialogWindow(String html) {
// here I have created new dialog window
dialog = new Dialog(cordova.getActivity(),
android.R.style.Theme_NoTitleBar);
dialog.getWindow().getAttributes().windowAnimations =
android.R.style.Animation_Dialog;
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
// creates a new webview to display HTML and attached to
dialog to display
webview = new WebView(cordova.getContext());
webview.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
webview.setWebChromeClient(new WebChromeClient());
webview.requestFocusFromTouch();
final String mimeType2 = "text/html";
final String encoding2 = "UTF-8";
webview.loadDataWithBaseURL("", html, mimeType2, encoding2, "");
dialog.setContentView(webview);
// get the screen size of the device
WindowManager wm = (WindowManager) ctx
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
// set appropriate sizes to the dialog window whether there is
side menu or not
lp.copyFrom(dialog2.getWindow().getAttributes());
lp.width = width ;
lp.height = height - 100;
dialog.getWindow().setAttributes(lp2);
dialog.show();
}
Using above method I could successfully create a Dialog window.
And I am creating the Dialog window using a Thread,
Runnable runnable = new Runnable() {
public void run() {
displayDialogWindow(html);
}
};
But after display the Dialog window once, I want to change the width
of the Dialog window dynamically. (eg. when I click on another button
of my app, the width of the window should be decreased and come to the
original width when another button is pressed).
Can any one help me to change the width of the Dialog window dynamically?
I think that you have to "recreat" your dialog everytime you want to change it.
After yourDialog.show() method, you can insert this line:
yourDialog.getWindow().setLayout((6 * width)/7, LayoutParams.WRAP_CONTENT);
This allow you to resize your Dialog window and it's content.
Of course width and height are the size of your screen.
I have an activity in which I'm using ProgressDialog.
After performing dialog.dismiss(), the screen remains grey until the user press the screen and then it gets light.
How can I make the screen light after the dialog.dismiss() command ?
int brightnessMode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = 0.5F; // set 50% brightness
getWindow().setAttributes(layoutParams);
I'm using the following to set the system auto brightness mode and level:
android.provider.Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE, 0);
android.provider.Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, y.brightness1);
I can change auto-brighess on and off, and set different levels. The settings seem to be applied properly -- I can go to into Settings --> Display --> Brightness, and whanever setting I set is actually shown correctly. However, the actual screen isn't changing its brightness. If i just tap on the slider in Display Settings, then everything gets applied.
I shoudl mention that I'm running an app withat a main activity, and these settings are getting applied in the BroadcastReceiver. I did try to create a dummy activity and tested the stuff there, but got the same results.
OK, found the answer here:
Refreshing the display from a widget?
Basically, have to make a transparent activity that processes the brightness change. What's not mentioned in the post is that you have to do:
Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE, 0);
Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, brightnessLevel);
then do
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness;
getWindow().setAttributes(lp);
And if you call finish() right after applying the changes, brightness will never actually change because the layout has to be created before the brightness settings is applied. So I ended up creating a thread that had a 300ms delay, then called finish().
I'm doing something similar with screen brightness in one of my apps, and I'm doing it through the WindowManager and it works. I'm using the following code to get the current screen brightness (and save it for later) and set it to full:
WindowManager.LayoutParams lp = getWindow().getAttributes();
previousScreenBrightness = lp.screenBrightness;
float brightness = 1;
lp.screenBrightness = brightness;
getWindow().setAttributes(lp);
Use the answer given by "user496854" above
If you are taking max screenBrightness =255 then while doing
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness; getWindow().setAttributes(lp);
divide screenBrightness by 255 like
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness/(float)255;
getWindow().setAttributes(lp);
I created a static method in my Application class which I invoke from all my Activity.onResume() methods.
MyApplication extends Application {
...
public static void setBrightness(final Activity context) {
// get the content resolver
final ContentResolver cResolver = context.getContentResolver();
// get the current window
final Window window = context.getWindow();
try {
// get the current system brightness
int brightnessLevel = System.getInt(cResolver,System.SCREEN_BRIGHTNESS);
// get the current window attributes
LayoutParams layoutpars = window.getAttributes();
// set the brightness of this window
layoutpars.screenBrightness = brightnessLevel / (float) 255;
// apply attribute changes to this window
window.setAttributes(layoutpars);
} catch (SettingNotFoundException e) {
// throw an error cuz System.SCREEN_BRIGHTNESS couldn't be retrieved
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}
}
}
MyActivity extends Activity {
...
public void onResume() {
super.onResume();
Log.d(TAG, "onResume()");
MyApplication.setBrightness(this);
}
}