0

Basically I'm trying to make an application for tablets. There going to be a bunch of widgets added and removed by the user inside a table layout. It needs to be dynamic so that the widgets are sized differently based on the width and height of the table layout.

To do this, I'm trying to make a 3x3 grid in a tablelayout. As I'm sure you guessed, each square has 33% width and height of the tablelayout.

This is what I got:

DisplayMetrics metrics;
int totalScreenSizeH; 
int totalScreenSizeW;

TableLayout contentTable;
LinearLayout contentLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Get the UI detail
    metrics  = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    totalScreenSizeH = metrics.heightPixels;
    totalScreenSizeW = metrics.widthPixels;



    contentTable=(TableLayout)findViewById(R.id.tableLayout1);
    contentLayout=(LinearLayout)findViewById(R.id.contentLayout);

    int tableWidth =contentTable.getWidth();
    int tableHeight=contentTable.getHeight();

    int layoutWidth=contentLayout.getWidth();
    int layoutHeight=contentLayout.getHeight();

    Log.v("Table Width",""+tableWidth);
    Log.v("Table Height",""+tableHeight);

    Log.v("Layout Width",""+layoutWidth);
    Log.v("Layout Height",""+layoutHeight);

So just to explain this a bit better. The linearLayout contains the tablelayout which has 3 table rows. I figured that I would be able to get the width and height of these but for some reason, both are coming back as 0 in size. One I have the width and height dims, Im going to save that variable and the create the widgets based on those variables/3.

So 2 questions:

1: is this the right way to handle dynamically sized widgets. Or is this the wrong way to handle a problem like this?

2: Any ideas why those 2-4 variables are all coming back as 0?

OVERTONE
  • 11,797
  • 20
  • 71
  • 87
  • 1
    I don't know the exact answers, but I can point you to a resource that will help you tremendously with supporting both Tables and Phones (and various dimensions), as well as support a Dashboard layout (which sounds like something you are describing, with your 3x3 grid). The Google 2011 IO application has some great guidelines for this, and I recommend taking at look at the source and seeing what they have done here: http://code.google.com/p/iosched/ – Jack Jan 22 '12 at 18:40

2 Answers2

1

I would advise against doing pixel-perfect designs on android, due to the vast amount of resolutions and densities you have to deal with.

My suggestion would be to look at android:layout_width/height="wrap_content" and android:layout_weight="INTEGER" that will help you make floating 3x3. You can also achieve this with a GridView

Community
  • 1
  • 1
jlindenbaum
  • 1,891
  • 18
  • 28
  • Gridview actually looks like what Im trying to do. I thought It was only available in 4.0. Turns out thats actually gridlayout. Will gridview contain things other than primitive types? like instead of strings, can it store fragments or other layouts? – OVERTONE Jan 22 '12 at 19:33
  • 1
    If you extend the adapter you're putting into the GridView and just manipulate the getView of that adapter, you can put pretty much anything you want in there. – jlindenbaum Jan 22 '12 at 19:38
0

I linked to the source in a comment to the OP, but for the 3x3 Grid, follow some practices Roman has done with his custom DashboardLayout implementation (Apache License FTW). It will correct for horizontal and vertical spacing on various screen dimensions: https://gist.github.com/882650.

If it doesn't fit your use case, I highly suspect you can modify it to do so.

Jack
  • 1,250
  • 1
  • 14
  • 26