1

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.

jofftiquez
  • 7,548
  • 10
  • 67
  • 121

3 Answers3

3

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

    } 
});
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • RelativeLayout preview = (RelativeLayout) findViewById(R.id.camerapreview); preview.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); int width = preview.getMeasuredWidth(); LayoutParams params = preview.getLayoutParams(); params.width = width; params.height = width; preview.addView(mPreview); i did this and its still zero, did I do it right? – jofftiquez Jan 04 '14 at 07:13
  • see my Edit @GreenFox, did you see the link? – Shayan Pourvatan Jan 04 '14 at 07:20
2

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...

Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • this is probably right, but the camera preview is stretched horizontally, maybe because the width's value is pixels, how can i convert that to DP? – jofftiquez Jan 04 '14 at 07:35
  • 1
    @GreenFox see here http://stackoverflow.com/questions/4605527/converting-pixels-to-dp and http://stackoverflow.com/questions/8309354/formula-px-to-dp-dp-to-px-android. there are many outcomes when you type **convert pixels to dp in android** in Google search... – Gopal Gopi Jan 04 '14 at 07:41
1

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'