1

I have a gridview and make a custom adapter for fill it, user can set by spinner rows and columns of my grid. In each cell of grid I set a videoview.

So I need to set dinamically the size for each videoview in my custom Adapter in order to fill the remaining part of the screen. Following this I can do the task, I take display size and set layout for my view dividing by the number of rows and columns.

The problem is that principal layout has action-bar and a textview . So, windows size is not correct. I need to subtract action-bar and textview size. I find a solution for know action-bar size, but when get height of my textview it is always 0.

As suggest here I should take textview size after rendering, but for render my gridview I need to know this size !!

There are other ways to do it ? It's necessary to manually calculate view size ??

this is my layout

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="it.nexera.visiamobile.ViewLiveMultiActivity"
     >

     <TextView
            android:id="@+id/txt_grid"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="24sp"
           android:textColor="#454545"
           android:gravity="left"
           android:text="@string/sel_grid" />

        <Spinner
            android:id="@+id/grid_spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/gridview"
            android:layout_toRightOf="@+id/txt_grid" />

    <GridView  
        android:id="@+id/gridview"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:stretchMode="columnWidth"
        android:layout_below="@+id/txt_grid"
        android:gravity="center"
    />

</RelativeLayout>

and this is my getView method for custom adapter :

// create a new VideoView for each item referenced by the Adapter
    @SuppressLint("NewApi")
    public View getView(int position, View convertView, ViewGroup parent) {
        VideoView videoView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            videoView = new VideoView(mContext);
                WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            int width=0;
            int height=0;

            if (android.os.Build.VERSION.SDK_INT >= 13){
                Point size = new Point();
                display.getSize(size);
                width = size.x;
                height = size.y;
            }
            else{
                width = display.getWidth();
                height = display.getHeight();

            }
            // Calculate ActionBar height
            TypedValue tv = new TypedValue();
            int actionBarHeight=0;
            if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
            {
                actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,mContext.getResources().getDisplayMetrics());
            }


            TextView textview = (TextView) ((Activity) mContext).findViewById (it.nexera.visiamobile.R.id.txt_grid); 

            int textview_height=textview.getHeight();

            height=height-actionBarHeight-textview_height;
            AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                    (width/COL_NUMBER),
                    (height/ROW_NUMBER));
            videoView.setLayoutParams(param);



           Uri video = Uri.parse(mvideoSrc[position]);
           videoView.setVideoURI(video);
           videoView.start();

        } else {
            videoView = (VideoView) convertView;
        }

        return videoView;
    }
Community
  • 1
  • 1
tulkas85
  • 1,103
  • 1
  • 17
  • 43

2 Answers2

1

Your problem is that you retrieve at the beginning the size of the whole screen of your device. So ActionBar must be substracted, but any other views if your layout doesn't take all the remaining space. So your method contradicts android modularity, for example, if your view is used in different ways depending on the size of the device.

I think what you need instead is using ViewTreeObserver, like this :

final View myView = ... // The RelativeLayout that's declared as root of your layout file
myView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        // You can here use View.getMeasuredWidth() or View.getMeasuredHeight() which correspond to the available space for the view containing your GridView and your TextView and then set your TextView's size
    }
}

Edit : To make the gridview fill all remaining space you can instead use a LinearLayout, using its weight attribute :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1"
    tools:context="it.nexera.visiamobile.ViewLiveMultiActivity"
     >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt_grid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:textColor="#454545"
            android:gravity="left"
            android:text="@string/sel_grid" />

        <Spinner
            android:id="@+id/grid_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <GridView  
        android:id="@+id/gridview"
        android:layout_width="0dp" 
        android:layout_height="0dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:layout_weight="1"
    />    
</LinearLayout>
Antoine Marques
  • 1,379
  • 1
  • 8
  • 16
  • ok, but I need to resize videoview, not textview... how can I do this in innested ongloballayoutlistener ? – tulkas85 Jan 23 '14 at 17:10
  • Some nice property of the gridview is that it resized automatically its children, so, by setting the size of the gridview, the video views contained inside shall be resized accordingly. If this does not make what you want, could you precise what's wrong then ? – Antoine Marques Jan 24 '14 at 08:04
  • Yes, I want the gridview fills the entire screen remaining. But Gridview itself not do this thing, I must to set manually layout for childs, in this case videoviews. Becouse if I set videoView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); in getView method, video not shown. Video shows only if I set fixed widht and height for layout params. If you could post an example for automatically gridview layout I would appreciate it – tulkas85 Jan 24 '14 at 09:16
  • Ok. As i understood, your TextView is somehow over the gridview and you want the gridview to take all remaining space. I post another answer adressing this concern – Antoine Marques Jan 24 '14 at 10:49
  • with this layout the video does not appear. And layout_above and toRightOf not work with LinearLayout (this doesn't matter..) – tulkas85 Jan 24 '14 at 11:48
  • your layout not work with my adapter, I can't see video using it. – tulkas85 Jan 27 '14 at 11:06
0

Have you tried by adding a RelativeLayout under the Grid, and adding two hidden views on it, one in the topleft corner, and another one in the bottoright corner (1dip height/width, for example). Then, from code you can findViewById them, and call getLocationOnScreen. It's just the first dirty idea that came to my mind. That way you can get the exact size in pixels (or dpi) of the Grid.

thelawnmowerman
  • 11,956
  • 2
  • 23
  • 36