setMinimumScale on WebView - java

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.

Related

Android: getHolder().setFixedSize(1920,1080) causes Issue with screen Resolution

Im developing a Game. I want my screen to always be 1920x1080.
getHolder().setFixedSize(1920,1080);
setFixedSize on my SurfaceView achieves this. It also creates a problem, however: the screen is still in WQHD res instead of also being FHD. This leads to mouseclickposition being bigger than the canvas is visually, and thus clicking on buttons no longer works unless I press where they actually would be in the smaller resolution.
Is there a way to set Android's resolution to 1920x1080? Maybe setting the Layout to that? I also tried making my canvas equal to a bitmap thats the right dimensions but that doesnt seem to change Anything at all.
Bitmap bitmap = Bitmap.createBitmap(10,100,null);
Canvas c = new Canvas(bitmap);
An alternative way to solve this might be to calculate the corresponding mouseClick Position from the bigger screen to the smaller, I suppose, but that seems like the suboptimal solution.
Here is what solved it
public static float normalize(float value, float min, float max) {
return Math.abs((value - min) / (max - min));
}
public boolean onTouchEvent(MotionEvent event) {
int differenceInWidth = actualScreenwidth - screenwidth; //2560-1920
float xPercent = normalize(event.getX(),0,actualScreenwidth);
Log.d("mouse_P_xPercent", Float.toString(xPercent));
float yPercent = normalize(event.getY(),0,actualScreenheight);
Log.d("mouse_P_yPercent", Float.toString(yPercent));
mouseCurrentPositionX = (int) (screenwidth*xPercent);
mouseCurrentPositionY = (int) (screenheight*xPercent);

Android how to resize (scale) an xml vector icon programmatically

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);

Android Dialog Won't Resize (PhoneGap)

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?

android get available height and width for portrait and landscape

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.

WebView Content size

I have some problems with webview content size. I need to convert full web page to a single image and tried this code
Bitmap screenshot;
screenshot = Bitmap.createBitmap(view.getWidth(), view.getContentHeight(), Bitmap.Config.ARGB_8888);
final Canvas c =new Canvas(screenshot);
view.draw(c);
where "view" is a WebView object
But the result is an image that have 1024x769 pixels. My web page is quite bigger (height is about 2000px). I've tried different ways to solve this problem, but still with zero rezult.
I find solution.
If you have the same problem, you can add onPicture Listener to your webview, like this
web.setPictureListener(new WebView.PictureListener() {
public void onNewPicture(WebView view, Picture picture) {
float temp = (float) view.getHeight();
height = view.getContentHeight() * a;
}
});
and get all your need height and width
For taking the picture of webview you have to enabled cache and from cache you can get it as a bitmap ,below i am mentioning the code hope will useful for you-
webview.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(webview.getDrawingCache());
webview.setDrawingCacheEnabled(false);
it is working for me just check your side,will wait for your acceptance of answer.thanks

Categories