Does anyone have an idea how to implement an Brightness Screen Filter like the one here:
http://www.appbrain.com/app/screen-filter/com.haxor
I need a starting point and I can't figure out how to do it.
Just make a transparent full screen activity that lets touches pass through. To make touches pass through use the following Window flags before setting the contentView:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Window window = getWindow();
// Let touches go through to apps/activities underneath.
window.addFlags(FLAG_NOT_TOUCHABLE);
// Now set up content view
setContentView(R.layout.main);
}
For your main.xml layout file just use a full screen LinearLayout with a transparent background:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#33000000">
</LinearLayout>
Then to adjust the "brightness" just change the value of the background colour from your code somewhere:
findViewById(R.id.background).setBackgroundColor(0x66000000);
Get an instance of WindowManager.
WindowManager windowManager = (WindowManager) Class.forName("android.view.WindowManagerImpl").getMethod("getDefault", new Class[0]).invoke(null, new Object[0]);
Create a full screen layout xml(layout parameters set to fill_parent)
Set your view as not clickable, not focusable, not long clickable, etc so that touch is passed through to your app and the app can detect it.
view.setFocusable(false);
view.setClickable(false);
view.setKeepScreenOn(false);
view.setLongClickable(false);
view.setFocusableInTouchMode(false);
Create a layout parameter of type android.view.WindowManager.LayoutParams.
LayoutParams layoutParams = new LayoutParams();
Set layout parameter like height, width etc
layoutParams.height = LayoutParams.FILL_PARENT;
layoutParams.width = LayoutParams.FILL_PARENT;
layoutParams.flags = 280; // You can try LayoutParams.FLAG_FULLSCREEN too
layoutParams.format = PixelFormat.TRANSLUCENT; // You can try different formats
layoutParams.windowAnimations = android.R.style.Animation_Toast; // You can use only animations that the system to can access
layoutParams.type = LayoutParams.TYPE_SYSTEM_OVERLAY;
layoutParams.gravity = Gravity.BOTTOM;
layoutParams.x = 0;
layoutParams.y = 0;
layoutParams.verticalWeight = 1.0F;
layoutParams.horizontalWeight = 1.0F;
layoutParams.verticalMargin = 0.0F;
layoutParams.horizontalMargin = 0.0F;
Key step: You can set what percentage of brightness you need.
layoutParams.setBackgroundDrawable(getBackgroundDrawable(i));
private Drawable getBackgroundDrawable(int i) {
int j = 255 - (int) Math.round(255D * Math.exp(4D * ((double) i / 100D) - 4D));
return new ColorDrawable(Color.argb(j, 0, 0, 0));}
Finally add view to windowManager that you created earlier.
windowManager.addView(view, layoutParams);
Note: You need SYSTEM_ALERT_WINDOW permission to lay an overlay on the screen.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Have tested this and it works. Let me know if you get stuck.
Of course you can't use this is production code, but if you are playing around .. try this Undocumented hack
It uses :
private void setBrightness(int brightness) {
try {
IHardwareService hardware = IHardwareService.Stub.asInterface(
ServiceManager.getService("hardware"));
if (hardware != null) {
hardware.setScreenBacklight(brightness);
}
} catch (RemoteException doe) {
}
}
Remember that it uses this permission :
<uses-permission android:name="android.permission.HARDWARE_TEST"/>
You ca try this also:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.max_bright);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 100.0f;
getWindow().setAttributes(lp);
}
Related
I have tried to create a dialog that occupies the full screen width from the old AlertDialog builder to the new DialogFragment approach in the onCreateView() and onViewCreated() to get the displayed dialog to occupy the full width of the screen. I can certainly get the width and height values of the screen but regardless of how I try to force the dialog to use these values, they are ignored. The displayed dialog is always the same width regardless of orientation.
In my latest attempt I have an xml layout that I inflate. I need to use a custom view so I cannot define that view in xml. So I add it.
Here is the most current attempt I have in my DialogFragment code. Of course this is just one of many attempts I have made trying to follow hints from posts and Slidenerd videos.
public class PopupDialog extends DialogFragment implements View.OnClickListener
{
private static final String TAG = PopupDialog.class.getName();
Button cancel = null;
Button focus = null;
View viewInput = null;
int width;
int height;
int id;
public PopupDialog()
{
}
public PopupDialog(View v, int id, int width, int height)
{
viewInput = v;
this.id = id;
this.width = width;
this.height = height;
}
#Override
public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstance)
{
Log.d(TAG, "onCreateView of DialogFragment called.");
View viewDialog = inflator.inflate(R.layout.popup_dialog, null);
// RelativeLayout relativeLayout = (RelativeLayout)viewDialog;
// LayoutParams params = new LayoutParams(width, height);
// relativeLayout.setLayoutParams(params);
// Point point = new Point();
// Activity activity = getActivity();
// activity.getWindowManager().getDefaultDisplay().getSize(point);
// if(point.x > point.y)
if(width > height)
{
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else
{
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
ViewParent parent = viewInput.getParent();
if(parent != null)
{
Log.d(TAG, "View already present. Removing.");
((ViewGroup)parent).removeView(viewInput);
}
LayoutParams params = new LayoutParams(width, height);
viewInput.setLayoutParams(params);
((ViewGroup)viewDialog).addView(viewInput, 0);
cancel = (Button)viewDialog.findViewById(R.id.btn_cancel);
focus = (Button)viewDialog.findViewById(R.id.btn_focus);
cancel.setOnClickListener(this);
focus.setOnClickListener(this);
setCancelable(false);
return viewDialog;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "onViewCreated of DialogFragment called.");
//getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, height);
getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = 10; //x position
wmlp.y = 450 * (id) + 10;
// wmlp.width = width;
// wmlp.height = height;
}
I am plotting a sine wave. The view has the correct size as the sine wave has a range of 0 to 12 but in the landscape orientation the displayed dialog box only gets a little more than half way, so 0 to 6 + is seen and then one has to wait for the wave to recycle as it plots from 6 to 12 before it becomes visible again when it goes back to 0. I AM able to place the dialog box upper left hand corner.
Does anyone know how to solve this problem? I went to the fragment because I was led to believe that the canned AlertDialog approach was fixed in width and there was nothing one could do. I am facing the same limitation with the DialogFragment.
try adding this code in on create() method after setContentView
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
I gave up and created my graph in a ListView in a ViewFlipper. Not want I wanted but I got more real estate for the graph.
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 know how it's possible to add View on top of Keyboard like WhatsApp and Hangout. In chat screen, they insert emoticons view on top of the opened soft keyboard.
Does anyone know how to achieve this behavior?
Well, I have created a sample keyboard for chatting here...
Here, I use popup window for showing popup window and height of popup is calculated dynamically by height of keyboard
// Initially defining default height of keyboard which is equal to 230 dip
final float popUpheight = getResources().getDimension(
R.dimen.keyboard_height);
changeKeyboardHeight((int) popUpheight);
// Creating a pop window for emoticons keyboard
popupWindow = new PopupWindow(popUpView, LayoutParams.MATCH_PARENT,
(int) keyboardHeight, false);
and height is calculated using this function :
/**
* Checking keyboard height and keyboard visibility
*/
int previousHeightDiffrence = 0;
private void checkKeyboardHeight(final View parentLayout) {
parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
parentLayout.getWindowVisibleDisplayFrame(r);
int screenHeight = parentLayout.getRootView()
.getHeight();
int heightDifference = screenHeight - (r.bottom);
if (previousHeightDiffrence - heightDifference > 50) {
popupWindow.dismiss();
}
previousHeightDiffrence = heightDifference;
if (heightDifference > 100) {
isKeyBoardVisible = true;
changeKeyboardHeight(heightDifference);
} else {
isKeyBoardVisible = false;
}
}
});
}
Using all these stuff i am able to make a perfect overlapping keyboard....
then i inflate popup window with viewpager and gridview for emoticons.
Also, i use spannable string for showing these emoticons in listview and chat window
After a heavy time of research and try-and-error, I've found another solution similar to the one of Chirag Jain above, but using a custom Dialog.
mDialogKeyboard = new Dialog(this,android.R.style.Theme_NoTitleBar);
mDialogKeyboard.setContentView(R.layout.your_custom_layout);
mDialogKeyboard.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
mDialogKeyboard.getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
mDialogKeyboard.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
mDialogKeyboard.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
WindowManager.LayoutParams lp=mDialogKeyboard.getWindow().getAttributes();
lp.width=WindowManager.LayoutParams.MATCH_PARENT;
lp.height=mSoftKeyboardHeight;
lp.gravity=Gravity.BOTTOM | Gravity.LEFT;
lp.dimAmount=0;
Despite the fact that Chirag Jain answer seems to be more clean, I'll post this here for have an alternative method.
As far as I know you can draw on other applications, yes. I myself have designed such an app. As for drawing on an application such as the keyboard or any other application in specific, I guess, you'll have to define a layout with a height that's exactly that of the keyboard. So, that would vary from device to device. So, this isn't possible.
I still stick to my notion that WhatsApp merely dismisses the soft keyboard on pressing the smiley button and calls it's own fragment.
If you would still like to pursue this, here's how you draw a "window" over other applications. These should be it's layout parameters.
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
Albeit, your width will change to an absolute pixel value since you'd like the activity to be over the keyboard only.
If I've misunderstood the question, please correct me.
what my thinking is they have created their own keypad for smiles, and on click of smile icon or keypad icon they are hiding smile keypad and showing the normal keypad. there are two scenarios in whats app case 1) if you don't focus 1st time of editext then you can not see the show keypad button,and the height of smile keypad is exactly same as normal keypad,we will get the keypad height only after our view layout is changed, means only after the keypad is shown, that means they are creating their own keypad.. 2) if you focus the edittext and click of smile button then it will show the option of show keypad button Please correct me if i am not right on this
I recently had to implement a view which would be above a soft keyboard. #Chirag Jain's solution is almost right, but it does not count with the system buttons in the bottom of the screen! This will make the keyboard height incorrect on some devices like NEXUS 6. This solution should work across all devices:
1) create layout which contains your view
<RelativeLayout
android:id="#+id/keyboard_info_container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:background="#color/C12"
android:padding="10dp"
android:visibility="invisible">
....
</RelativeLayout>
2) Bind view
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.notifications_email_settings_fragment, container, false);
ButterKnife.bind(this, rootview);
checkKeyboardHeight(rootview);
3) keyboard check and view margin settings
private void checkKeyboardHeight(final View parentLayout) {
parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
int previousHeightDiffrence = 0;
int systemBarHigh = 999999;
#Override
public void onGlobalLayout() {
Rect r = new Rect();
parentLayout.getWindowVisibleDisplayFrame(r);
int screenHeight = parentLayout.getRootView()
.getHeight();
int keyboardHeight = screenHeight - (r.bottom);
if(systemBarHigh > keyboardHeight) {
systemBarHigh = keyboardHeight;
}
if (keyboardHeight > 250) {
int keyboardHightWithoutSystemBar = keyboardHeight - systemBarHigh;
// no need to update when the keyboard goes down
if (previousHeightDiffrence != keyboardHightWithoutSystemBar) { // if (Math.abs(previousHeightDiffrence - keyboardHeight) > 10) {
adjustKeyboard(keyboardHightWithoutSystemBar);
}
keyboardInfoContainer.setVisibility(View.VISIBLE);
isKeyBoardVisible = true;
previousHeightDiffrence = keyboardHightWithoutSystemBar;
} else {
isKeyBoardVisible = false;
if (keyboardInfoContainer != null) {
keyboardInfoContainer.setVisibility(View.INVISIBLE);
}
}
}
});
}
private void adjustKeyboard(int keyboardHeight) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) keyboardInfoContainer.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lp.bottomMargin = keyboardHeight;
keyboardInfoContainer.requestLayout();
}
#jirkarrr, Why don't you add the keyboardInfoContainer like this:
WindowManager wm = getWindowManager();
WindowManager.LayoutParams lps = new WindowManager.LayoutParams();
lps.x = 0; lps.y = keyboardHeight;
wm.addView(keyboardInfoContainer, lps);
I do as your code, but it cannot show out the keyboardInfoContainer.
I use a popup to put view over the keyboard:
public void showPopUpKeyboard() {
mIsPopupVisible = true;
// Initialize a new instance of LayoutInflater service
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
// Inflate the custom layout/view
View customView = inflater.inflate(R.layout.popup_in_keyboard, null);
mScrollView = (ScrollView) customView.findViewById(R.id.keyboard_layout_view);
// Initialize a new instance of popup window
mPopupWindow = new PopupWindow(
customView,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
setSizeForSoftKeyboard();
// Get a reference for the custom view close button
Button closeButton = (Button) customView.findViewById(R.id.ib_close);
// Set a click listener for the popup window close button
closeButton.setOnClickListener((View view) -> {
// Dismiss the popup window
mIsPopupVisible = false;
mPopupWindow.dismiss();
});
mPopupWindow.showAtLocation(mParentLayout, Gravity.CENTER, 0, 0);
}
Then I try to know keyboard's height:
mParentLayout.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
Rect r = new Rect();
mParentLayout.getWindowVisibleDisplayFrame(r);
int heightDiff = mParentLayout.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) {
//enter your code here
if (mIsPopupVisible) {
keepKeyboard();
mIsPopupVisible = false;
mPopupWindow.dismiss();
}
} else {
//enter code for hid
}
});
You can check this tutorial and this example in GitHub
I have an activity with ListView that has:
android:theme="#android:style/Theme.Dialog"
in Manifest.
When I open it and when it has only one line in ListView, the window that opens is very small.
How do I make the window take the whole screen?
Use this in your onCreate method of the Activity to make it full screen.
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.myxml);
LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.MATCH_PARENT;
params.width = LayoutParams.MATCH_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
I have found that setting the window size does work, but you have to do it a bit later. In this example the window width is set to 90% of the display width, and it is done in onStart() rather than onCreate():
#Override
protected void onStart() {
super.onStart();
// In order to not be too narrow, set the window size based on the screen resolution:
final int screen_width = getResources().getDisplayMetrics().widthPixels;
final int new_window_width = screen_width * 90 / 100;
LayoutParams layout = getWindow().getAttributes();
layout.width = Math.max(layout.width, new_window_width);
getWindow().setAttributes(layout);
}
Similar to the answer from PravinCG but it can be done with one line in onCreate()...
getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
Use the suggested code before setcontentview() call. It will work.
Just a small update. Used MATCH_PARENT instead of the deprecated FILL_PARENT. PravinCG's answer worked great for me.
Yeezz ! I figured it out ! The problem is that the margin sizes are not calculated in the window widht. So If you set the layout margin to 0 and move that part to the padding of the layout the problem will be solved.
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);
}
}