Hiding the navigation bar in a background service- android - android-studio

I have an app that introduces a translucent view over all other running apps (basically a filter running as a background service). The problem that i am facing is that the app does not apply the filter over the navigation bar (bottom bar with back, home and recent tabs buttons).
The flags that i have used to create a view are shown in the code below:
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
0 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);
Is there any special flag or anything else that i need to cover the nav bar ?
This is the screenshot of the filter running with everything else covered except the nav bar

Add a Y offset to your layoutparams for your overlay to go over your navigation bar.
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,0, navbarHeight(),
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
0 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);
private int navbarHeight() {
...code to calculate height of navbar
}

Related

Android: Custom bottom sheet dialog

How can I implement following design functionality with android standard component bottom sheet:
Image when Bottom sheet dialog fragment will appear:
Image when user scrolled to up to view bottom of content:
I will use ViewPager to scrolling header images and RecyclerView to showing descriptions and other informations. And parallax effect to ImageView(which are placed in ViewPager) when scrolling content vertically. Have a minimum height of the ImageView(and ViewPager), user can't collapse fully it (Look to second screenshot, which is user scrolled until the end of content).
I want stop scrolling ImageView when it will reach to minimum height(look to second one Screenshot), but the content of below ImageView should be continue scrolling
This can be done with an if statement in an on scroll view such as shown below:
ScrollView scrollView = findViewById(R.id.scrollView); //Adjust for your code
ImageView imageView = findViewById(R.id.imageView); //Adjust for your code
boolean imageIsHidden = false;
int threshold = 250;
scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int scrollY = rootScrollView.getScrollY();
if(scrollY >= threshold){
imageIsHidden = true;
//Move image outside of scroll view so it doesn't scroll
}
else if(scrollY < threshold && imageIsHidden){
imageIsHidden = false;
//Move image inside of scroll view so it does scroll
}
}
});
What this does is has a boolean called imageIsHidden and an integer called threshold. Threshold is where you want it to make it disappear. You will need to play around with this value to find a sweet spot.
You will also need to implement moving the image inside and outside of the scroll view as well in the if and if else statement.

JavaFX Menu Flow

I have an application with two menu bars (application menus and administration menus) on the top row and a search box in between them. The first menu bar is left justified with the search box immediately following while the second menu is right justified. This leaves room for additional application menus without moving the administration menus.
I tried an HBox, but can't get the second menu right justfied.
I tried using an AnchorPane and anchoring the application menu to the left and the admin menu to the right. This works fine until you resize it. When the display gets too small to show both menus it starts truncating letters. I want it to wrap the second menu to the next line.
I tried using a FlowPane which works great for getting one to flow under the other when resized, but I can't get the second menu reliable right justified. I tried a trick of putting a listener on the parent width and calculating the hgap to use, but the first time this gets called, the menu bars have a size of 0 and so the hgap is too big for the actual menus. After I resize it once, that trick works beautifully.
Even better would be a menuBar that could flow automatically so that I didn't have to break it up to allow it to wrap around. But if that capability exists, I've been unable to find it.
AFAIK MenuBar does not support wrapping its Menus.
There can be different approaches to achieve the layout you want. One of them at the below.
To align the second admin flowPane to the right, use HBox.setHgrow for flowPane. To align menu bars in flowPane, use flow.setAlignment(Pos.TOP_RIGHT):
#Override
public void start(Stage primaryStage) {
final Menu menu01 = new Menu("App Menu 1");
final Menu menu02 = new Menu("App Menu 2");
final Menu menu1 = new Menu("Admin Menu 1");
final Menu menu2 = new Menu("Admin Menu 2");
final Menu menu3 = new Menu("Admin Menu 3");
MenuBar menuBar0 = new MenuBar();
menuBar0.getMenus().addAll(menu01, menu02);
menuBar0.setMinWidth(220); // do not shrink
MenuBar menuBar1 = new MenuBar();
menuBar1.getMenus().addAll(menu1);
MenuBar menuBar2 = new MenuBar();
menuBar2.getMenus().addAll(menu2);
MenuBar menuBar3 = new MenuBar();
menuBar3.getMenus().addAll(menu3);
FlowPane flow = new FlowPane(Orientation.HORIZONTAL);
// flow.setStyle("-fx-background-color: gray; -fx-border-color: red"); // visual debug
flow.setAlignment(Pos.TOP_RIGHT);
flow.setHgap(0);
flow.getChildren().addAll(menuBar1, menuBar2, menuBar3);
TextField searchField = new TextField();
searchField.setPromptText("Search here..");
// make it unresizable
searchField.setMinWidth(200);
searchField.setMaxWidth(200);
HBox mainBox = new HBox(5);
mainBox.setAlignment(Pos.CENTER_LEFT);
HBox.setHgrow(flow, Priority.ALWAYS);
mainBox.getChildren().addAll(menuBar0, searchField, flow);
mainBox.setStyle("-fx-background-color: lightgray;");
VBox vBox = new VBox(0);
vBox.getChildren().addAll(mainBox, new Button("Demo"));
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.show();
}

Find end page in javafx web view while scrolling

I have loaded local html file in JavaFx2. i am scrolling the web view page. I need to get alert message when i reach the end of the page.
How to do that?
Since you're in a webview, you can use javascript to do that, injecting a following code into the loaded page:
setInterval(function() {
var y = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
y -= 12;
var maxY = document.body.scrollHeight - window.innerHeight;
if (y == maxY)
{
alert('bottom');
}
}, 200);
This will check if you scrolled down to the bottom every 200 ms.
Note that the standard javascript alert() is not available in the current JavaFX 2.x (but is planned for version 3.0). You have to handle it yourself like that:
webview.getEngine().setOnAlert(new EventHandler<WebEvent<String>>() {
#Override
public void handle(WebEvent<String> e)
{
System.out.println("Alert: " + e.getData());
}
});
This should print Alert: bottom to standard output when scrolled to the bottom. Not tested. Tested and working.
Note the y -= 12: this is to account for if the horizontal scroll bar is visible. 12 is the standard height of the scroll bar (so you need to modify it if you're styling scroll bars with CSS). Also, if you are not expecting to see horizontal scroll bars, you need to remove that line altogether. For that reason I recommend manually disabling horizontal scroll bars using CSS.
Also, on a related note, for some reason if you're in fullscreen WebView the maxY is going to always be 0. Currently there doesn't seem to be a workaround

How can I create resizing spacers in JavaFX?

First of all, I'm a long time Java/Swing developer. I recently installed JavaFX 2.2 to play around with.
I'm creating a fairly simple app, whose main window has a toolbar on top and content in the rest of the window. The obvious way to accomplish this is to use a BorderPane, and stick a ToolBar into the top section. So far, so good. However, I would like some of the controls in the toolbar to be at the left edge of the window, and some at the right edge. I can find no way to do this. I can put an invisible spacer object into the toolbar, but I only know how to give it a fixed width; it doesn't resize when the window is resized.
So I thought that instead of using a ToolBar object, I'll just use an HBox; it should be equivalent to a horizontally-oriented Swing Box object, right? And the Swing Box class has a createHorizontalGlue() method that inserts an auto-sizing spacer. Well, I can't find an equivalent in the JavaFX HBox class. Is there no simple way to do this?
I figured out how to do it using an HBox instead of a ToolBar to hold the controls; the key is the HBox.setHgrow() method, which allows you to set a spacer object to grow to fill the available space. I still don't know if it's possible to do this with an actual ToolBar instance.
/**
* Creates and populates the Node that serves as the window toolbar.
*
* #return a newly constructed and populated toolbar component
*/
private Node makeToolbar() {
// Auto-sizing spacer
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
// Horizontal box containing toolbar controls
HBox box = new HBox();
box.setPadding(new Insets(8));
box.setAlignment(Pos.CENTER);
box.getChildren().addAll(openButton, spacer, resizeSlider);
// Colored background panel with drop shadow
Pane bgRect = new Pane();
bgRect.setStyle("-fx-background-color: #e0e0e0;");
bgRect.setEffect(DropShadowBuilder.create().width(1).build());
// StackPane to hold box and rectangle
StackPane stack = new StackPane();
stack.getChildren().addAll(bgRect, box);
return stack;
}
i do it this way:
private Node makeFooter(Node left, Node right) {
ToolBar footer = new ToolBar();
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
spacer.setMinWidth(Region.USE_PREF_SIZE);
footer.getItems().addAll(left, spacer, right);
return footer;
}
hope i could help someone

Eclipse rcp layout issue

I am facing an issue with editor and its contents layout. Its following: there is an editor with print preview functionality, main part of the editor is taken by paper clips PrintPreview component, on the left side there is a composite with some buttons (see screenshot 1).
As you can see PrintPreview is located inside ScrolledComposite.
The problem is: when I am resizing window or views under editor, the bottom scrollbar moves under views and thus disappears. There is no way to scroll horizontally afterwards (no editor scrollbar appears). see screenshot2 (bottom scrollbar is getting hidden).
It starting to get hidden, when editor height is less then height of the right composite ( there are more widgets below the last button, I've just erased them)
If I am shrinking the window, then right composite never loses its width and always visible (see screenshot 3 - It does not allow to resize more than that).
The composites are created in the following way:
#Override
public void createPartControl(final Composite parent) {
GridLayout gridLayout = new GridLayout(2, false);
parent.setLayout(gridLayout);
// Scroll area for the print area
scroll = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
final GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.widthHint = 1;
gridData.heightHint = 1;
scroll.setLayoutData(gridData);
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);
// Button area
final Composite buttonArea = new Composite(parent, SWT.FLAT);
final GridData layoutData = new GridData(SWT.FILL, SWT.TOP, false, false);
buttonArea.setLayoutData(layoutData);
gridLayout = new GridLayout(1, false);
buttonArea.setLayout(gridLayout);
The question is: how not to prevent disappearing of the bottom scrollbar? Can I have the same behavior as width the width of the right composite - It not possible to resize less then right composite height? Or how can I show scrollbar on the whole editor if some minimum height limit is exceeded?
I've tried different things on different composites, like setting minimumHeight and heightHint, putting buttonsArea into the scrolledcomposite (just for fun) etc. But nothing have helped me.
Does anybody knows fast solution? Thank you.
See this SO post: Prevent SWT scrolledComposite from eating part of it's children
You need to listen for width changes on the content composite
(mParent), compute the minimum height again given the new content
width, and call setMinHeight() on the scrolled composite with new
height.
If the above post is not as useful as it seems then share some minimal compiling and executable code to replicate the problem. Also, mention the eclipse/swt version, OS and JDK version, it helps in analyzing the problem.
Demo code for resize event and output !!
Code :
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class ScrolledCompositeTest
{
public static void main (String [] args)
{
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
GridLayout layout = new GridLayout();
layout.numColumns = 4;
// set the minimum width and height of the scrolled content - method 2
final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
sc2.setExpandHorizontal(true);
sc2.setExpandVertical(true);
final Composite c2 = new Composite(sc2, SWT.NONE);
sc2.setContent(c2);
layout = new GridLayout();
layout.numColumns = 7;
c2.setLayout(layout);
Button b2 = new Button (c2, SWT.PUSH);
b2.setText("first button");
sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Button add = new Button (shell, SWT.PUSH);
add.setText("add children");
final int[] index = new int[]{0};
add.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
index[0]++;
Button button = new Button(c2, SWT.PUSH);
button.setText("button "+index[0]);
sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
c2.layout();
}
});
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Output:
Actual Size
Vertical Resize
Horizontal Resize

Resources