Android get rid of DialogFragment floating margin - java

I'm displaying a dialogfragment in my app and I notice that even when I set the fragments' y position to the bottom of the screen there's still a margin visible:
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
windowParams.y = size.y;
In the below screenshot you can see that the light blue (my dialogfragment) is still appearing some distance away from the bottom of the screen despite being set to the bottom of the screen. How do I remove this margin?

The solution is to add this line:
setStyle(DialogFragment.STYLE_NO_FRAME, 0);

To avoid extra margin in bottom, top , left and right use below lines of code.
//Dialog fragment will be shown at the bottom of screen
//if you want to show on entire screen just comment it
getDialog().getWindow().setGravity(Gravity.BOTTOM);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = getDialog().getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
//For full height set it to MATCH_PARENT else WRAP_CONTENT
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);

Related

How do I enable a button after the scrollbar goes to the bottom?

So, I used a JScrollPane and then I added a JTextArea. I used textArea.setCaretPosition(0) to reset the scroll and it went at the top. All good, until I wanted to set a disabled Button on enable when the scrollbar reaches at the bottom.
How can I do that?
You can listen for changes to the JScrollPane’s viewport, and compare the bottom of the viewport’s visible rectangle with the height of the viewport’s view (that is, the JTextArea):
JViewport viewport = scrollPane.getViewport();
viewport.addChangeListener(e -> {
Rectangle rect = viewport.getViewRect();
int bottom = rect.y + rect.height;
endButton.setEnabled(bottom >= viewport.getViewSize().height);
});

screen coordinates interfere with the bar at the bottom of the screen

I'm learning about android game development and have made a few small games. The problem I encounter is that when I draw an object at the bottom of my screen it is hidden behind the bar at the bottom.
For example I made a simple pong game, but my paddle is hidden behind the bar.
I retrieve the screen size as follows:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int x = size.x;
int y = size.y;
My paddle is a rectF object with bottom coordinate equal to y.
Other little games i made encouter the same problem. Every time i draw an object at the bottom of the screen with the canvas and paint classes it interferes with the bar.
How can i solve this? Is there a way to retrieve the coordinates of that bar?
According to specifications, DisplayMetrics is probably what you should use instead of Display:
public void getMetrics (DisplayMetrics outMetrics)
Gets display metrics that describe the size and density of this display. The size returned by this method does not necessarily represent the actual raw size (native resolution) of the display.
You could use this:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;

Adding an ImageView at a random position in a LinearLayout

I want to add image views dynamically to a full screen linear layout. I have written some code but some image views are either partially or totally going outside the screen. Some times image views are well within the phone screen.
How can I ensure that they remain totally inside the screen.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
final int widthArea = displayMetrics.widthPixels;
final int heightArea = displayMetrics.heightPixels;
final LinearLayout home = (LinearLayout) findViewById(R.id.llHome);
ImageView imgView = new ImageView(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200, 200);
imgView.setLayoutParams(lp);
imgView.setImageResource(R.drawable.img_1);
imgView.setX(r.nextInt(widthArea));
imgView.setY(r.nextInt(heightArea));
home.addView(imgView);
You should take into account the dimension of the image itself. Settings X and Y place its starting point within the screen but it might leak on a side.
First, load your image as a Drawable, then include drawable.getWidth() (and maybe even height, that depends on your layout) to your X position.
imgView.setX(r.nextInt(widthArea - drawable.getWidth()) + drawable.getWidth());

How to set margin for ImageView in pixels in android java?

I have a set of ImageViews whose BackgroundResource and LayoutParams will be defined in a separate xml file. The height, width, marginleft and margintop are given in pixels. The values are in respect to a 1024x600 pixel resolution screen. The code i'm using
ImageView ImgV= (ImageView)findViewById(R.id.ImgV_1);
RelativeLayout.LayoutParams the_dimens = new RelativeLayout.LayoutParams(50,50);
the_dimens.setMargins(800,50,0,0);
ImgV.setBackgroundResource(R.drawable.bulbon);
ImgV.setLayoutParams(the_dimens);
While the height and width seem to be rightly displayed in pixels, the margins are not. So how can I set the margin values to apply in pixels rather than whatever default units the below code takes.
the_dimens.setMargins(800,50,0,0);
The app is only for a 1024x600 device, so i'm not worried about resolution independent design.
Thanks.
check this
int dpValue = 5; // margin in dips
float d = context.getResources().getDisplayMetrics().density;
int margin = (int)(dpValue * d); // margin in pixels
You can do in this way:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);

Camera not stretching to width of android device

In my android camera app, Im trying to make it so that the camera layout view fills the width of the screen and then is proportional (to a square) for the height of the layout. But its cropping it too small for some reason:
example of what is happening..
The green arrow represents where it should go to.
Ive tried fill_parent, etc. So, how can I make it go to the end of the screen?
I believe this is the problem. I know I did this because I was not sure how to make it automatically stretch to the right proportion.
int width = 352;
int height = 288;
camPreview = new CameraSurfaceView(width, height);
myCamHolder.addCallback(camPreview);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = width*288/352;
Maybe you don't need 288/352, but rather preview size.height/preview size.width

Categories

Resources