2

I have 9 rows and 6 columns of buttons so i was using this code to change size of each button proportionally:

DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    height = displaymetrics.heightPixels;
    width = displaymetrics.widthPixels;

    int Orientation = getResources().getConfiguration().orientation;

    if(Orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        Button btn1 = (Button) findViewById(R.id.button1);
        btn1.setLayoutParams(new LayoutParams(height/10,width/7));
        Button btn2 = (Button) findViewById(R.id.button2);
        btn2.setLayoutParams(new LayoutParams(height/10,width/7));
        Button btn3 = (Button) findViewById(R.id.button3);
        btn3.setLayoutParams(new LayoutParams(height/10,width/7));
        Button btn4 = (Button) findViewById(R.id.button4);
        btn4.setLayoutParams(new LayoutParams(height/10,width/7

But this does not work size of buttons changes but they do not fit well. Beow is my xml code:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" >

<TableRow
    android:id="@+id/tableRow1"
    android:weightSum="1" >

    <TextView
    android:id="@+id/ResultText"
    android:gravity="right"
    android:textSize="20sp"
    android:background="#AAAAAA"
    android:layout_weight="1"
    android:textColor="#FFFFFF"
     />

</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="fill_parent"
    android:weightSum="1" >

    <TextView
    android:id="@+id/InputText"
    android:background="#BBBBBB"
    android:gravity="right"
    android:textColor="#FFFFFF"
    android:layout_weight="1"
    android:layout_marginTop="4dp"
    android:textSize="15sp" />


</TableRow>

<TableRow
    android:id="@+id/tableRow0"
    android:layout_width="wrap_content"
    android:weightSum="1" >

    <TextView
    android:id="@+id/memoryStatText"
    android:background="#BBBBBB"
    android:gravity="right"
    android:textColor="#FFFFFF"
    android:layout_weight="1"
    android:layout_marginTop="4dp"
    android:textSize="15sp"
    android:layout_height="wrap_content" />

</TableRow>

<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:weightSum="1" >

   <Button
    android:id="@+id/buttonMAdd"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_marginTop="10dp"
    android:layout_weight="0.166"     
    android:text="M+"
    android:onClick="ProcessInput"
    android:gravity="fill"/>

    <Button
    android:id="@+id/buttonMRemove"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_marginTop="10dp" 
    android:layout_weight="0.166"    
    android:text="M-"
    android:onClick="ProcessInput"
    android:gravity="fill" />

    <Button
    android:id="@+id/buttonMC"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_marginTop="10dp"
    android:layout_weight="0.166"     
    android:text="MC"
    android:onClick="ProcessInput"
    android:gravity="fill" />

In every case layout stretches horizontally perfectly on all devices, on larger devices however, lot of space remains vacant on bottom part of application. To just change height i changed my code to this:

Button btn1 = (Button) findViewById(R.id.button1);
btn1.setHeight(height/10));

But this reduces width of button for no reason.What am i doing wrong.My question basically is can i make my layout fit vertically as well as it fits horizontally.

Here are my images in two cases:enter image description here

Here is my second image: enter image description here

7
  • try a custom ViewGroup to layout your children
    – pskink
    Mar 16, 2014 at 8:49
  • I am a newbie and don't know how that would work, can you end me a link to some tutorial that is easy to follow. Mar 16, 2014 at 8:54
  • google for: custom viewgroup
    – pskink
    Mar 16, 2014 at 8:56
  • Is there any other way, i was thinking to consider creating custom view groups later after i have created three or four applications. Mar 16, 2014 at 8:59
  • i dont know better one
    – pskink
    Mar 16, 2014 at 9:03

1 Answer 1

0

Try setting your TableLayout height to match_parent:

<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:stretchColumns="*"> 

And make sure you remove the code based on orientation you have.

1
  • Columns fit perfectly the problem is with height of rows. Mar 16, 2014 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.