Android MenuDrawer Change Size - java

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

Related

Android getDisplayMetrics() not returning accurate display size

I'm trying to get the pixel width/height of the device for placing elements and mapping touch events on the screen but not getting the desired effect (notice lower right square should be completely flush with the bottom right):
// screen size
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float ScrDensity = metrics.density;
int metricHeight = metrics.heightPixels;//
int metricWidth = metrics.widthPixels;//
Log.i("AllMetrics", metrics.toString());
// temp
int rightButton_diameter = 100;
int rightButton_xpos = metricWidth - rightButton_diameter;
int rightButton_ypos = metricHeight - rightButton_diameter;
Where AllMetrics returns:
DisplayMetrics{density=2.0, width=720, height=1184, scaledDensity=2.0, xdpi=320.0, ydpi=320.0}
I notice that the returned height is also different from the 720x1280 listed in the Virtual Device Manager (API 19, Android 4.4) and the x/y positions of the touch events seems accurate to the drawn elements (ie the clickable area of the button is the same as where it is being drawn)
Is this a configuration error or am I using the incorrect method of fetching screen position?
Check this code it works for me
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
Outside of activity you can try this
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm)
To access the DisplayMetrics members, initialize an object like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

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

Creating a View programmatically according to screen resolution

I created a BaseAdapter that implements ListAdapter. This BaseAdapter reads a JSON file and populate my ListView inflating a custom layout.
In this JSON file I have a boolean value (active = true or active = false). If active is true, I inflate the custom layout and everything is fine, but if active is false, I need to create a new View in this custom layout. Here's my code:
if (!active) {
JSONObject notifications = jsonData.getJSONObject("notifications");
String message = notifications.getString("message");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.height = 70;
TextView warning = new TextView(Controller.getContext());
warning.setText(message);
warning.setTextColor(Color.WHITE);
warning.setBackgroundColor(Color.RED);
warning.setGravity(Gravity.CENTER);
warning.setPadding(10, 0, 10, 0);
warning.setLayoutParams(params);
// contentFrame is a RelativeLayout where the view is created;
viewHolder.contentFrame.getLayoutParams().height = 320;
viewHolder.contentFrame.addView(warning);
}
My problem here is this:
params.height = 70;
viewHolder.contentFrame.getLayoutParams().height = 320;
I'm saying that my TextView is 70 pixels higher and my RelativeLayout is now 320 pixeis high.
It looks nice on my phone, but if I test on a phone with other resolution it will not look the same right?
Is there any way to create a View and define is Height or Width according to the screen resolution? Maybe create the View 10% the size of the screen resolution? Is that a good pratice? What's the best aproach for something like this?
Instead of using hardcoded pixel values, use DPs. DPs account for the device density so views look pretty much the same regardless of which device the app runs on.
See this for additional info.
So, define this in your dimens.xml:
<resources>
<dimen name="my_width">320dp</dimen>
</resources>
Then read it like this:
int width = getResources().getDimensionPixelSize(R.dimen.my_width);
You have to scale your size values according to the screen density:
float density = getResources().getDisplayMetrics().density;
That means you have to multiply all size values with density:
params.height = (int)(70 * density);
viewHolder.contentFrame.getLayoutParams().height = (int)(320 * density);
and the same with padding values:
warning.setPadding((int)(10 * density), 0, (int)(10 * density), 0);
You can define dimensions in XML like any other resource, and have those resources automatically selected by Android based on the current device configuration.
So you have two definitions, one for "normal" circumstances, and another for "bigger screen":
src/main/res/values/dimens.xml
src/main/res/values-sw600dp/dimens.xml
Each resource file can have the same dimension defined in dp. Android will pick the appropriate one. If you need to read the values programmatically as raw pixel sizes, you can do that with Resources.getDimensionPixelSize().
I would recommend against trying to define list items in terms of percentage of the size of the screen.

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.

Categories