0

I have a gridlayout who's size I want to check before displaying anything in the activity, because I want to check if an image needs to be resized. Can't do it OnStart() because apparently it hasn't been loaded yet. Adding a listener doesn't work due to synchronization issues with the rest of the code. It would be nice if I could just manually generate the value, by using the dimensions of the device, but I for the life of me can't find the exact value of those stupid margins Android places automatically. Any suggestions? Essentially in the code below I need to have height/width of the GridLayout BEFORE SplitImage() is called.

XML

<GridLayout
    android:id="@+id/grid_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:useDefaultMargins="false" >

</GridLayout>

Relevant JAVA

public class GamePlay extends Activity {
int difficulty = 0;
int image = 0;
int widthView = 0;
int heightView = 0;
int boardW;
int boardH;
int nSqr;
Bitmap[][] gameBoard;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    Intent intent = getIntent();
    difficulty = intent.getIntExtra("DIFFICULTY_KEY_DMIROU246", 0);
    image = intent.getIntExtra("LEVEL_KEY_DMIROU246", 0);
    setContentView(R.layout.activity_game_play);
    Log.w("Derp", "1");

}

@Override
protected void onStart() {
    super.onStart();
    View view = (View) findViewById(R.id.grid_layout);
    Log.w("Derp", "2");
    heightView = view.getHeight();
    widthView = view.getWidth();
    Log.w("Derp", "3");
    gameBoard = SplitImage();
    Log.w("Derp", "9");
}
user2941307
  • 17
  • 2
  • 6
  • Please check next link to see how you can do it: **[Android get programmatically created view's width][1]** [1]: http://stackoverflow.com/questions/19633318/android-get-programmatically-created-views-width/19633471#19633471 – Diego Palomar Oct 31 '13 at 12:26

3 Answers3

3

You won't get the height or width of the view until it is drawn on the screen. For this purpose you'll have two ways to get the hight and width of a layout:-

1) ViewTreeObserver--Doc

    View view = (View) findViewById(R.id.grid_layout);
    ViewTreeObserver vto = view.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @Override 
        public void onGlobalLayout() { 
            this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
            int width  = view.getMeasuredWidth();
            int height = view.getMeasuredHeight(); 
            gameBoard = SplitImage();
            Log.w("Derp", "9");
        } 
    });

2) Handler.postDelayed--Doc

    View view = (View) findViewById(R.id.grid_layout);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                int width  = view.getMeasuredWidth();
                int height = view.getMeasuredHeight(); 
                gameBoard = SplitImage();
                Log.w("Derp", "9");
            }
    }, 500);
bakriOnFire
  • 2,685
  • 1
  • 15
  • 27
0

The easiest solution is to do this asynchronously, use view.getViewTreeObserver then add a OnGlobalLayoutListener that will get the size of your view and call splitImage().

Take care that your listener is not called to often, the best is to remove it after it has been triggered once.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
0

Try This One

Problem:I have some data show as list (Expandable view).

Main list view Show i am Using List View and show Child view Display another inner listview used.so child view can not calculate cell heigt.

1) i have both UI TABLAT and PHONE

2) if any user any problem implement time this code so contact me i will help you."rp1741995@gmail.com"

    public static int getListViewHeightBasedOnChildren(ListView listView,Context context) {
         ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter != null)
        {
          int totalHeight = 0;
            int size = listAdapter.getCount();
            Log.e(TAG, "getListViewHeightBasedOnChildren: >>"+size);
            for (int i = 0; i < size; i++)
            {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            totalHeight = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount()-1));
            if(listAdapter.getCount()!=0) {
                if (MyApplication.isTablet(context)) {
                    Float height = convertDpToPixel(10);
                    totalHeight = (totalHeight / listAdapter.getCount()) + Math.round(height);
                }else {
                    Float height = convertDpToPixel(80);
                    totalHeight = (totalHeight / listAdapter.getCount()) + Math.round(height);
                }

            }
 ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight;
            //params.height;
             Log.e(TAG, "getListViewSize: "+String.valueOf(totalHeight));

             listView.setLayoutParams(params);
            return totalHeight;
        }
     return 0;
}
Rahul Patil
  • 129
  • 1
  • 5