jueves, 18 de julio de 2013

How to get view dimensions at onCreate

Hi, Trying to get the dimensions of my RelativeLayout at the onCreate of my Activity without no success, at the begining teh code seemed very easy :
RelativeLayout dashboard = (RelativeLayout)findViewById(R.id.dashboard);
int width = dashboard.getWidth()
no luck!, in this case getWidth() is always 0. Then I tryed to figure out why this happens and I realize that dashboard is not rendered yet, so, I have to find a callback after rendering to get the dashboard width... no luck! onResume didn't work neither, so I was researching through internet and I've found this blog where my problem is perfect solved. I've just going to put the code but you can read the entire article at http://code2thought.blogspot.com.es/2011/01/getting-layout-dimensions-in-android.html
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dashboard);
  
  RelativeLayout dashboard = (GridLayout)findViewById(R.id.dashboard);
  
  
  dashboard.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
   
   @Override
   public void onGlobalLayout() {
    
    int width = dashboard.getWidth()
    
   }
  });
  
 }
happy coding!

martes, 16 de julio de 2013

GridLayout views out of the screen

Sometimes your views inside a gridlayout are outside the screen, to fix that you can give to your views android:width="0", that way it fits inside the Gridlayout.
<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2">

    <Button
        android:layout_column="0"
        android:layout_columnSpan="1"
        android:layout_gravity="start|end"
        android:layout_row="0"
        android:text="ASDFASDF"
         />

    <Button
        android:layout_column="1"
        android:layout_gravity="start|end"
        android:layout_row="0"
        android:text="SDAVDFBDFB" />

    <Button
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="fill|center"
        android:layout_row="1"
        android:text="ASDVADFBFDAFEW" />

    <Button
        android:layout_column="0"
        android:layout_columnSpan="1"
        android:layout_gravity="fill|center"
        android:layout_row="2"
        android:text="FWEA AWFWEA" />

    <Button
        android:layout_column="1"
        android:layout_columnSpan="1"
        android:layout_gravity="fill"
        android:layout_row="2"
        android:text="BERWEfasf" />

    <Button
        android:layout_width="94dp"
        android:layout_column="0"
        android:layout_columnSpan="1"
        android:layout_gravity="fill|center"
        android:layout_row="3"
        android:text="SDFVBFAEVSAD" />

    <Button
        android:layout_column="0"
        android:layout_columnSpan="1"
        android:layout_gravity="fill|center"
        android:layout_row="4"
        android:layout_rowSpan="2"
        android:text="GVBAERWEFSD" />

    <Button
        android:width="0dp"
        android:layout_column="1"
        android:layout_columnSpan="1"
        android:layout_gravity="fill|center"
        android:layout_row="3"
        android:layout_rowSpan="2"
        android:text="VSDFAVE SDFASDWA SDFASD" />

    <Button
        android:width="0dp"
        android:layout_column="1"
        android:layout_columnSpan="1"
        android:layout_gravity="fill|center"
        android:layout_row="5"
        android:text="FWEWEGAWEFWAE"/>

</GridLayout>