How can I set an GestureOverlayView's width and height programmatically?
I tried the below code. But I am getting null pointer exception in setting the width and height.
GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
View inflate = getLayoutInflater().inflate(R.layout.main, null);
ImageView imageView = (ImageView) inflate.findViewById(R.id.imageView);
gestureOverlayView.addView(inflate);
gestureOverlayView.getLayoutParams().height = 50;
gestureOverlayView.getLayoutParams().width = 50;
setContentView(gestureOverlayView);
You need to add the view first before you can read out the height and width. Also then you meight get some trouble only after the second or third call of onMeasure(...) the dimensions are final.
If you want to set the width and height by code use this:
ViewGroup.LayoutParams lp = gestureOverlayView.getLayoutParams();
if(lp == null) {
// not yet added to parent
lp = new ViewGroup.LayoutParams(desiredWidth, desiredHeight);
} else {
lp.width = desiredWidth;
lp.height = desiredHeight;
}
gestureOverlayView.setLayoutParams(lp);
yourparent.addView(gestureOverlayView, lp);
Please note that you need to choose also the right LayoutParams implementation. Here I use that one of ViewGroup.
Related
I want to set the child view's height/width of PercentFrameLayout programmatically. How to set the percentage height/width of child views of PercentFrameLayout at runtime..
I assume that you have View with Id view_child inside of your PercentLayout.
so use this code snippet
View view = findViewById(R.id.view_child);
PercentRelativeLayout.LayoutParams params =(PercentRelativeLayout.LayoutParams) view.getLayoutParams();
// This will currently return null, if view not exist in your xml.
PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
info.heightPercent = 0.60;
view.requestLayout();
hope this help..
ImageView child= new ImageView(getActivity());
percentRelativeLayout.addView(child);
PercentRelativeLayout.LayoutParams params = (PercentRelativeLayout.LayoutParams) child.getLayoutParams();
PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
info.heightPercent = 0.05f;
info.widthPercent = 0.05f;
info.leftMarginPercent = 0.5f;
child.setLayoutParams(params);
Is the source of the slider and the lesson link:
http://www.oodlestechnologies.com/blogs/Facebook-Style-Slide-Menu-In-Android
In this case, the width of the file "left_menu.xml" determined TextView (android:layout_width="260dp")
How do I set the width to "LinearLayout" file "left_menu.xml" depending on the device screen? For example, I want the width of the "LinearLayout" was always 1/3 of the screen device? Or any way to set the width of the TextView 1/3 of the width of the device screen.
To set the LinearLayout or TextView width to 1/3 of the device screen:
first of all get the device screen width:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
try {
display.getRealSize(size);
} catch (NoSuchMethodError err) {
display.getSize(size);
}
int width = size.x;
int height = size.y;
now just create a LayoutParams and set it to the Text:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)(width/3),
LinearLayout.LayoutParams.WRAP_CONTENT); // or set height to any fixed value you want
your_layout.setLayoutParams(lp);
// OR
your_textView.setLayoutParams(lp);
LinearLayout linear = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linear.setLayoutParams(params);
As you can see, you can set Integer values for the LinearLayout.LayoutParams() constructor, like this:
LinearLayout.LayoutParams cellParams = new LinearLayout.LayoutParams(0, 100);
The costructor wants pixels and not dp(Density Pixels), here's a formula to convert PXs from DPs:
(int) (<numberOfDPs> * getContext().getResources().getDisplayMetrics().density + 0.5f)
LinearLayout layout = (LinearLayout)findViewById(R.id.ll);
LayoutParams params = (LayoutParams) layout.getLayoutParams();
params.height = 100;
params.width = 100;
The above answers are correct. I'll just suggest future readers to take a look at their xml layout file and be sure that the LinearLayout element is not using weight attribute. If so, consider updating weight in your LayoutParams object.
Another good practice is to retrieve the layout params object from the view you're trying to adjust, instead of creating one from scratch and having to set all the values.
LinearLayout YOUR_LinearLayout =(LinearLayout)findViewById(R.id.YOUR_LinearLayout)
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ 100,
/*weight*/ 1.0f
);
YOUR_LinearLayout.setLayoutParams(param);
The easy way to do that, set value of layoutParams, and please notice this size is not in DP.
view.layoutParams.width = 400;
view.layoutParams.height = 150;
view.requestLayout();
LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView
mainimage = new ImageView(HLActivity.this);
mainimage.setLayoutParams(params);
loader.DisplayImage(_pageslist.get(j).getUrl(), mainimage);
I already tried mainimage.getwidth() and mainimage.getHeight();
Now i want to findout the orignalwidth and height of the ImageView after
place the image in the imageview. can you please help me.
getWidth() and getHeight() are the indeed ones you are looking for, but you are trying to ask for the width and the height when the view is not measured yet. To fix this, we post a message to the ImageView, which will get executed after the measurement is done.
LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// mark it as final, so it can be accessed in the runnable.
final imageView mainimage = new ImageView(HLActivity.this);
mainimage.setLayoutParams(params);
mainimage.post(new Runnable() {
#Override
public void run() {
// get the width and the height
int width = mainimage.getWidth();
int height = mainimage.getHeight();
// do what you want to do with the sizes.
loader.DisplayImage(_pageslist.get(j).getUrl(), mainimage);
}
});
I have ImageView. I change margins and size of ImageVew on set new bitmap. For example, user click button, I set bitmap to ImageView and change params (this is code from my own view):
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
p.leftMargin = (int) ((pw - lw) / 2f);
_topMargin = (int) ((ph - lh) / 2f);
lp.topMargin = _topMargin;
lp.width = lw;
lp.height = lh;
requestLayout();
All work. But also I want change params, if change size parent view of my ImageView. If I try use code on change params from this event - nothing todo (ImageView don't change params).
How I can fix this issue?
I see now one way: on change parent size, I don't change ImageView params, only set some internal boolean flag. After finish parent changing, I change my ImageView params. But I don't know, what parent event I need watch, after which I can change my ImageView params.
My solution.
I use this method.
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
lp.leftMargin = (int) ((pw - lw) / 2f);
_topMargin = (int) ((ph - lh) / 2f);
lp.topMargin = _topMargin;
lp.width = lw;
lp.height = lh;
setLayoutParams(lp);//<---set layout params
requestLayout();
After changing layout params you need to set it back.
Hi I wanted to change height and width property of image view inside my activity
I tried it in following way but its not working for me ...
View card_view = getLayoutInflater().inflate(R.layout.card_details1,null);
coupon_img = (ImageView) card_view.findViewById(R.id.coupon_image);
// I tried this ////////
coupon_img.getLayoutParams().height = 20;
coupon_img.getLayoutParams().width = 20;
// I also tried this ////
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
coupon_img.setLayoutParams(layoutParams);
// also this one ////
coupon_img.setMaxHeight(10);
But I am not able to change height and width of imageview src. Is there any mistake I am doing?
How to do this?
Need help...
Thank you...
In this piece of code, I am creating a new instance of an ImageView at runtime and setting dimensions to it:
// SET THE IMAGEVIEW DIMENSIONS
int dimens = 120;
float density = activity.getResources().getDisplayMetrics().density;
int finalDimens = (int)(dimens * density);
LinearLayout.LayoutParams imgvwDimens =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgAlbumPhoto.setLayoutParams(imgvwDimens);
// SET SCALETYPE
imgAlbumPhoto.setScaleType(ScaleType.CENTER_CROP);
// SET THE MARGIN
int dimensMargin = 5;
float densityMargin = activity.getResources().getDisplayMetrics().density;
int finalDimensMargin = (int)(dimensMargin * densityMargin);
LinearLayout.LayoutParams imgvwMargin =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgvwMargin.setMargins
(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDimensMargin);
This will set the dimensions of the ImageView. They will, however, be in px. Use the code from here if you need dp values: https://stackoverflow.com/a/9563438/450534
UPDATED:
To change the dimensions of an existing ImageView that has already been defined in the XML, use this:
coupon_img.setLayoutParams(new LayoutParams(100, 100));
try some thing like this...
LayoutParams params = new LayoutParams(100, 100);
parantlayout.addView(coupon_img, params);
I think this will help you.
I think you are not adding the changed image to the layout..
LinearLayout ll = (LinearLayout)findViewById(R.layout.yourlinearlayout);
image.setLayoutParams(
new LinearLayout.LayoutParams(
bmp.getWidth(),
bmp.getHeight()));
ll.addView(image);// Then add the image to linear layout