0

I have a customised view CustomView which I place inside the layout of my activity. Here is my layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_alignParentTop="true"/>

    <View
        android:id="@+id/view2"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <com.musicmuni.riyaz.views.CustomView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/view2"
        android:layout_toEndOf="@id/view2"
        android:layout_below="@id/view1"/>
</RelativeLayout>

Here, you see that my CustomView is placed towards right of a View with id view1 and below a View with id view2. What I do is set the width of CustomView based on my need by overriding onMeasure, so that I can scroll over my CustomView. I still need the window size inside which my CustomView has been placed. So lets say the screen dimension is 1200px X 800px and the height of view1 is 150px and the width of view2 is 60px, then the window size of CustomView should be 1050px X 740px. How can I get this number? If I will use CustomView.getWidth(), it will give me the width of the view that I had set inside onMeasure. What is the way to get the correct window size?

Edit 1:

Here is a drawing of what I want to achieve. Hope this makes it more clear: enter image description here

Swapnil
  • 1,870
  • 2
  • 23
  • 48
  • You could set your `CustomView` to have both `layout_height`and `layout_width` to **0dp** and have `layout_weight` equals to **1**. It will take the rest of the space. More info: http://stackoverflow.com/questions/3995825/what-does-androidlayout-weight-mean – klauskpm Oct 03 '16 at 14:35
  • I think you did not understand the question correctly. I have no problem in having my view occupying the entire space. I am not sure how to find out the actual width and height occupied by my view on the screen. – Swapnil Oct 03 '16 at 14:40
  • I understand now. But do you want to just get the size of your custom view? If so, `getWidth` and `getHeight` should do the work. Maybe you are calling it before the size is defined? Try to call it on `onResume` inside your activity. – klauskpm Oct 03 '16 at 15:00
  • No, I want to get the dimension of the area over the physical screen inside which my view has been put. That can be different from the actual dimensions of the view. For example, the case of a view which is scrollable, the actual width got by `getWidth()` is different from the dimension of the physical space inside which I see my view. – Swapnil Oct 03 '16 at 15:09
  • Well... You would need to list manually your views, get the screen/display size and do the math. I don't recommend you to get all views programmatically, because a view can be inside a group view, so the math wouldn't match. How to get screen dimensions: http://stackoverflow.com/questions/1016896/get-screen-dimensions-in-pixels – klauskpm Oct 03 '16 at 15:21
  • I have added a drawing in the question to be more clear on what I am saying. Hope this helps. – Swapnil Oct 03 '16 at 16:01

0 Answers0