Layout params getWidth() returns zero - android

i have a RelativeLayout here's the code :
<RelativeLayout
android:id="#+id/camerapreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="#000">
</RelativeLayout>
its a camera preview for the camera application, i set the width to match_parent.
and here is my java code :
RelativeLayout preview = (RelativeLayout) findViewById(R.id.camerapreview);
int w = preview.getWidth();
LayoutParams params = preview.getLayoutParams();
params.width = w;
params.height = w;
preview.addView(mPreview);
basically i decalaired the width of the RelativeLayout to match_parent to be dynamic to the width of any device. But i want the view to be square so i used the java code abave, but every time i ran the program this line int w = preview.getWidth(); returns zero. I must have missed something, help please :D thank you.

The size of view cannot be calculated until its parent is calculated you can force that with following code:
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int widht = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
there is three way to do this, see this link
EDIT
you can use this too.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.camerapreview);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
hs.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();
}
});

this may help you...
RelativeLayout preview = (RelativeLayout) findViewById(R.id.camerapreview);
int width = getResources().getDisplayMetrics().widthPixels;
ViewGroup.LayoutParams params = preview.getLayoutParams();
params.width = width;
params.height = width;
preview.setLayoutParams(params);
here preview will occupy entire screen width... and it will be square shaped...

Layout has width 'match parent' . So if it hasn't views (is empty) width = 0.
Solution:
Add views to layout
Or set width to 'fill parent'

Related

How to get the Height and Width of the View?

I need to get the height and width of the View before adding into layout or its is display. I have tried with using MeasuredHeight and MeasuredWidth of the view but its retursn as 0. Could you please help me out to resolve this?
Below code snippet I have tried out:
int height = Column.MeasuredHeight;
int width = Column.MeasuredWidth;
You need to use a Layout change event to not receive 0 as the height.
If you ask it too soon, the layout isn't drawn yet. Use the ViewTreeObserver of the layout that you want to measure.
LinearLayout linearLayout = FindViewById<LinearLayout>(Resource.Id.linearLayout);
ViewTreeObserver vto = linearLayout.ViewTreeObserver;
vto.GlobalLayout += (sender, args) => {
System.Console.WriteLine (linearLayout.Height);
};
Try view.getWidth() and view.getHeight()
View view= LayoutInflater.from(this).inflate(R.layout.activity_main, null, false);
setContentView(view);
int viewWidth = view.getWidth();
int viewHeight = view.getHeight();

How to change the imageview size dynamically in android for a particular image?

Actually I am using Relative layout for image view. This image view is for receiving images from the internet to the application. The thing I need is can I change the image size dynamically for a particular images (say jpeg) and another size for attachments(docx,pdf), etc.
I used if condition for images and another if for attachments. Now how to set image size for receiving images and another image size for receiving attachments. I need to do dynamically. Note I am using only one image view for both images and attachments.
<RelativeLayout>
<ImageView>
id=iv
width=wrap content
height=wrap content
</ImageView>
</RelativeLayout>
for class file
if("image/png"){
iv.setImageResource(R.drawable.abc);
or
code to get images from web
}
else if(mimetype("application/pdf")
{
iv.setImageResource(R.drawable.abc)
}
this is the model
Help me out!
/*
requestLayout()
Call this when something has changed which has
invalidated the layout of this view.
*/
imageView.requestLayout();
imageView.getLayoutParams().height = height;
imageView.getLayoutParams().width = width;
reference : https://android--code.blogspot.com/2015/08/android-imageview-set-width-and-height.html
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)imageView.getLayoutParams;
lp.width = x;
lp.height = y;
imageView.setLayoutParams(lp)
try this,
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(width,height);
ImageView ivImage=(ImageView)findViewById(R.id.image);
ivImage.setLayoutParams(params);
Try this code to change image aspect ratio dynamically,
private void setPicDimensions() {
Point screenDimensions = Utils.getScreenDimensions();
width = screenDimensions.x;
height = Utils.getHeightFromAspectRatio(width, 16, 9);// dynamically set aspect ratio value here its 16:9
}
// in your image view set layout params dynamically whereever you need to change
//Declare height and width as int and iv_pic as imageview
LayoutParams layoutParams = iv_offer_pic.getLayoutParams();
layoutParams.height = height;
layoutParams.width = width;
iv_pic.setLayoutParams(layoutParams);
most important is:
<imageview android:scaleType="fitXY" />
<RelativeLayout>
<ImageView>
id=iv
android:scaleType="fitXY"
width=wrap content
height=wrap content
</ImageView>
</RelativeLayout>
for class file
if("image/png"){
iv.setImageResource(R.drawable.abc);
or
code to get images from web
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)imageView.getLayoutParams;
lp.width = 100;
lp.height = 100;
imageView.setLayoutParams(lp)
}
else if(mimetype("application/pdf")
{RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)imageView.getLayoutParams;
lp.width = 50;
lp.height = 50;
imageView.setLayoutParams(lp)
iv.setImageResource(R.drawable.abc)
}
this is the model
Help me out!

change the width & height of ImageButton at runtime in android

I have used this code to make a ImageButton & after that i want to change the size of it at run time. Both the codes the written below,
<ImageButton
android:id="#+id/btn_new"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/untitled3"
/>
public void scaler1() {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
btn_new.setMinimumWidth(width/4);
btn_new.setMinimumHeight(height/6);
}
But it is not working please give any solution of it.
This is the actual code i'm using
public void scaler1() {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
edit_screen.setWidth(width);
edit_screen.setHeight(height / 6);
lpsame = (LayoutParams) btn_0.getLayoutParams();
lpsame.width=width/4;
lpsame.height=height/6;
btn_0.setLayoutParams(lpsame);
LayoutParams lpedit = (LayoutParams) edit_screen.getLayoutParams();
lpedit.width=width;
lpedit.height=height/6;
LayoutParams lpequal = (LayoutParams) btn_0.getLayoutParams();
lpequal.width=width/2;
lpequal.height=height/6;
edit_screen.setLayoutParams(lpedit);
btn_2.setLayoutParams(lpsame);
btn_3.setLayoutParams(lpsame);
btn_4.setLayoutParams(lpsame);
btn_5.setLayoutParams(lpsame);
btn_6.setLayoutParams(lpsame);
btn_7.setLayoutParams(lpsame);
btn_8.setLayoutParams(lpsame);
btn_9.setLayoutParams(lpsame);
btn_add.setLayoutParams(lpsame);
btn_subtrac.setLayoutParams(lpsame);
btn_multiply.setLayoutParams(lpsame);
btn_divide.setLayoutParams(lpsame);
btn_dot.setLayoutParams(lpsame);
btn_percentage.setLayoutParams(lpsame);
btn_CA.setLayoutParams(lpsame);
btn_back.setLayoutParams(lpsame);
btn_equal.setLayoutParams(lpequal);
editParams = new RelativeLayout.LayoutParams(width, height/6);
sameparam= new RelativeLayout.LayoutParams(width/4,height/6);
equalparam= new RelativeLayout.LayoutParams(width/2,height/6);
edit_screen.setLayoutParams(editParams);
}
You can change properties of a View by using LayoutParams class
LayoutParams lp = (LayoutParams) btn_new.getLayoutParams();
lp.width = yourWidth;
lp.height = yourHeight;
btn_new.setLayoutParams(lp);
Update
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(yourWidth, yourHeight);
yourButtons.setLayoutParams(layoutParams);
You have to get an instance of your view, before changing its properties
ImageButton btn_new = (ImageButton) findViewById(R.id.btn_new);
try this:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
LayoutParams layoutParams = image_new.getLayoutParams();
layoutParams.width=width/4;
layoutParams.height=height/6;
image_new.setLayoutParams(layoutParams);

android RelativeLayout change height programmatically

I try change layout height programmatically. I have two buttons. In one button I change my layout position and second button I try to receive save position witch I was first time.
<RelativeLayout
android:id="#+id/popaplayout"
android:layout_width="fill_parent"
android:layout_height="200dp" >
This is a my layout
and in first button I wrote
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
parms.addRule(RelativeLayout.BELOW, R.id.movies_title_layout);
scrollpopap.setLayoutParams(parms);
scrollpopap.setScrollingEnabled(true);
and second button
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, 300);
rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
scrollpopap.setLayoutParams(rel_btn);
scrollpopap.setScrollingEnabled(false);
In xml file I wrote height 200dp and programmatically 300 and there are different height. How I can wrote save height in programmatically?
if anyone knows solution please help me
thanks
First convert 200 dp into pixels -
final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (200 * scale + 0.5f);
Then set programatically,
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, pixels);
rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
scrollpopap.setLayoutParams(rel_btn);
scrollpopap.setScrollingEnabled(false);
Convert DP to PX like this.
final float scale = getContext().getResources().getDisplayMetrics().density;
int px = (int) (100 * scale + 0.5f); // replace 100 with your dimensions
Set Some layout_gravity of relative layout. and Change height and width by java code.
RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
rl.getLayoutParams().height = px;
rl.getLayoutParams().width = px;
If you intending to change RelativeLayout height, then this help.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.getLayoutParams().height = 100;
relativeLayout.getLayoutParams().width = 100;
Use this
RelativeLayout layout = (RelativeLayout) findViewById(R.id.yourlayout_id);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;
For Kotlin Users who want to set Height & Width from Dimens file, use this:
relativeLayout.layoutParams.width = relativeLayout.context
.resources.getDimensionPixelSize(R.dimen.width)
relativeLayout.layoutParams.height = relativeLayout.context
.resources.getDimensionPixelSize(R.dimen.height)

Android image scale type to fit width and scaleY = scaleX

I have a layout with a background image. The image width should be the same as the screen width (match_parent). But it's ugly, the OS don't stretch the picture in height.
I heard about ImageView scaleType, so I have the background image in an ImageView instead of a layout background, but I can't config to be what I want.
How can I set to the layout or ImageView to scale the image (in width) to fit the width of the screen AND scale the height with the same scale?
I want the second screen in the picture:
UPDATE:
I'm trying from code, but I get 0 width + I can't believe this can't be done from xml with an ImageView.
Drawable imageDrawable = getResources().getDrawable(imageResource);
contentContainer.setBackgroundDrawable(imageDrawable);
Rect rect = imageDrawable.getBounds();
int originalWidth = rect.width();
int originalHeight = rect.height();
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = originalHeight * (width/originalWidth);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
contentContainer.setLayoutParams(layoutParams);
contentContainer.post(new Runnable() {
#Override
public void run() {
contentContainer.setScaleY(contentContainer.getScaleX());
Rect rect = contentContainer.getBackground().getBounds();
int originalWidth = rect.width();
int originalHeight = rect.height();
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = originalHeight * (width/originalWidth);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
contentContainer.setLayoutParams(layoutParams);
}
}
Use FrameLayout as parent and then add imageview as child.
Give the width match_parent and height fill_parent to the imageView and another ways is apply the android:layout_weight attribute try this may be it's work.
you can do this at runtime by measuring the width of the device like this:--
super.onCreate(savedInstanceState);
setContentView(<layout-resoure-id);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int imageSize = 0;
if (width > height)
imageSize = height;
else
imageSize = width;
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.getLayoutParams().width = imageSize;
imageView.getLayoutParams().height = imageSize;
imageView.setImageResource(R.drawable.pattu);
your xml:--
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"/>
</LinearLayout>
Enjoy...!
Have you tried playing with scaleType in you layout file?
<ImageView
...
android:scaleType="fitStart"
/>
This should scale your image to fit at the top of your layout, stretching or shrinking if necessary. Haven't had a chance to test (and I can't remember the height/width settings--sorry!), but this could be a solution.
ImageView imageView = (ImageView) findViewById(R.id.your_image_id);
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
int imageSize = height>width?width:height;
your configurations go here !
imageView.getLayoutParams().width = imageSize;
imageView.getLayoutParams().height = imageSize + (/* additional height logic*/);
you should put the imageView scaletype as fitXY and do this to stretch the image like the way you want!
<ImageView
...
android:scaleType="fitXY"
/>

Categories

Resources