I have a Dialog with a custom view and it lets me set the x, y, width and height of the dialog, and that's all working.
I'm placing this Dialog over a WebView create by PhoneGap.
I sometimes need to reposition the dialog depending on what's going on in the WebView, so I created a function I could call from the JavaScript to resize it.
Here's how I am originally setting up the Dialog:
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.x = this.dpToPixels(dialogDimensions.optInt("x"));
lp.y = this.dpToPixels(dialogDimensions.optInt("y"));
lp.width = this.dpToPixels(dialogDimensions.optInt("width"));
lp.height = this.dpToPixels(dialogDimensions.optInt("height"));
lp.gravity = Gravity.TOP | Gravity.LEFT;
// make it possible to click outside dialog
lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
dialog.setContentView(main);
dialog.show();
dialog.getWindow().setAttributes(lp);
dialogDimensions is an JSONObject with the x, y, width, height parameters, and dpToPixels() converts the sizes to account for differences in pixel density (i.e. on the Nexus 10, 300px in the WebView is actually 600px on the tablet).
Here's my resize method:
public String resize(JSONObject dimensions) {
dialogDimensions = dimensions;
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.x = this.dpToPixels(dialogDimensions.optInt("x"));
lp.y = this.dpToPixels(dialogDimensions.optInt("y"));
lp.width = this.dpToPixels(dialogDimensions.optInt("width"));
lp.height = this.dpToPixels(dialogDimensions.optInt("height"));
Log.i(LOG_TAG, dialog.getWindow().getAttributes().debug("Before Resize: "));
dialog.getWindow().setAttributes(lp);
Log.i(LOG_TAG, dialog.getWindow().getAttributes().debug("After Resize: "));
return "";
}
The log messages are logging what I would expect, but the dialog doesn't do anything...
What I've Tried
I tried a dialog.show() then dialog.show() before, after and both before and after the setAttributes().
I also tried to invalidate it, but I'm not sure I did it right... it was something I copied from a StackOverflow. Here's what I tried:
dialog.getCurrentFocus().invalidate();
dialog.getWindow().getCurrentFocus().invalidate();
main.invalidate(); // view passed into dialog.setContentView
I also tried a couple different ways of settings/getting the LayoutParams including creating a new set, copying, etc. Doesn't seem to make a difference. I even tried setting it to fill its parent and stuff, but no effect.
Any ideas how to get it to resize? Maybe it's not even an issue with the redraw?
Related
I have a horizontal RecyclerView with child elements which I want to change the width of to be a bit less than the current match_parent set via the layout. I want to do this so that the next element in list is a bit visible on screen.
In the adapter onCreateViewHolder I am doing this:
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = MainActivity.deviceWidth - (int) MainActivity.dpToPx(40.0f);
view.setLayoutParams(params);
where MainActivity.deviceWidth is:
DisplayMetrics displaymetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
deviceWidth = displaymetrics.widthPixels;
screenDensity = Resources.getSystem().getDisplayMetrics().density;
and dpToPx is this function:
public static float dpToPx(float dp) {
return (dp * screenDensity + 0.5f);
}
This works. however, I noticed that on different devices, the "margin" I am setting is not always the same. I.e. the next card in the RecyclerView is not always showing the same amount.
Is the dpToPx conversion correct? I might be missing something else in my calculation but I am not sure what.
Update
Changing:
params.width = MainActivity.deviceWidth - (int) MainActivity.dpToPx(40.0f);
To:
params.width = (int) (MainActivity.deviceWidth *0.9);
works!. I am fine with this solution. I just want to know why the previous one does not work...
This is driving me crazy. I would like to be able to resize an xml vector drawable icon programmatically in order to use it in an ImageView.
This is what I've done so far which is not working
Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.ic_marker,null);
drawable.setBounds(0,0,512,512);
imageVenue.setImageDrawable(drawable);
The vector icon ic_marker is not resized. It just keeps the hardcoded width and height values every time.
Any ideas?
You can change the width and height of your imageview programmatically. Since vector drawables will preserve the original quality of the image, this will make the desired output happen.
ImageView iv = (ImageView) findViewById(R.id.imgview);
int width = 60;
int height = 60;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(params);
I'm currently facing the same problem.
I'm trying something like this, cause ViewParent has actually height set explicitly, so I use match_parent and set margins. It doesn't work all the time though, cause I simply use this view in a viewholder for RecyclerView... Also I've noticed that sometimes I see scaled up version with artifacts, sometimes full size, sometimes there are margins, and bigger margins... But it still might work for you, if you use it in a simpler scenario.
mImageViewFront.setImageDrawable(vectorDrawable);
final int paddingLR = mImageViewFront.getWidth() / 4;
final int paddingTB = mImageViewFront.getHeight() / 4;
LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.setMargins(paddingLR, paddingTB, paddingLR, paddingTB);
mImageViewFront.setLayoutParams(params);
I'm using the Android MenuDrawer from https://github.com/SimonVT/android-menudrawer in my Activity.
After Updating the Library, the Menu was a little bit smaller than before. How can I change the size (width) of the Menu?
MenuDrawer has a xml attribute for the size - mdMenuSize.
Alternatively you can set this programmatically too with the setMenuSize method of the menudrawer.
before on create method:
private MenuDrawer mDrawer;
public static final int TOUCH_MODE_NONE = 0;
After on Create method:
mDrawer.setMenuSize(600);
600 is in dp set as u like
following is piece of my code:
mDrawer = MenuDrawer.attach(this);
mDrawer.setTouchMode(TOUCH_MODE_NONE);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
if(width>=640)
mDrawer.setMenuSize(600);
else if(width<640)
mDrawer.setMenuSize(400);
mDrawer.setContentView(R.layout.drawermain);
mDrawer.setMenuView(R.layout.drawermenu);
I have a webView that I manipulate with this code: (this all works fine)
WebView myWebView = (WebView) findViewById(R.id.stats_webview);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.setPadding(0,0,0,0);
myWebView.setInitialScale(getScale());
Which gets the initial scale from this method
private int getScale(){
Display display = ((WindowManager)
getSystemService(StatsActivity.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
Double val = new Double(width)/new Double(720);
val = val * 100d;
return val.intValue();
}
That all works fine and loads the view slightly scaled down to fit the screen from my 720pixel web source. The problem is that once someone zooms in, they cannot zoom back out all the way to the initial scale. They can only zoom back out to a scale of 1, so they are unable to fit the entire page back on the screen. I've found references to a setMinimumScale method but it doesn't seem to be available any more. Am I just missing an include file or is there a new way to accomplish this?
I have tried:
myWebView.setMinimumScale(getScale());
and
myWebView.getSettings().setMinimumScale(getScale());
but neither works.
If editing the HTML file is an option, you can use the viewport meta tag.
check the documentation for more details.
so I have written the following method in my activity:
private void setDisplayMetrics(){
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int dh = metrics.heightPixels;
int dw = metrics.widthPixels;
if(dw < dh){
deviceWidth = dw;
deviceHeight = dh;
}else{
deviceWidth = dh;
deviceHeight = dw;
}
System.err.println("--------------> dh : "+deviceHeight+" | dw "+deviceWidth);
}
And it works great, in the sense that it gets me the total width and height of the screen with great accuracy and reliability (which is what I have asked it to do).
Here is the problem. On older android devices the screen dimensions are the same as the dimensions the application can take up, and the script above helps me to set the size of elements in the app. BUT with android ICS I have this graphic button bar on the bottom of the screen, and it messes up my whole strategy.
What I would really like is the ability to get the available app dimensions for the portrait view as well as the landscape view at the same time in one method. And have these dimensions be accurate regardless of the presence of the bar pictured above.
Does anyone know how to achieve this?
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop=
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;
Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight);
From what I've read so far you can't get this height directly. The approach via getWindowVisibleDisplayFrame() does not seem to work.
The only promising solution I have found so far is using a custom layout to measure the screen size as explained in
http://evgeni-shafran.blogspot.de/2011/01/android-screen-size-problem.html.
I am convinced that this will work but to me it seems to be more trouble than it is worth.