0

Here are snapshots of what my application looks like

Screenshot vertical

It fits on some devices and not on others:

Screenshot horizontal

As you can see, horizontally it fits really well no matter what. How can I make sure it fits perfectly as well vertically?

Here 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="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:orientation="vertical" >

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

    <TextView
    android:id="@+id/ResultText"
    android:layout_width="0dp"
    android:gravity="right"
    android:background="#AAAAAA"
    android:layout_weight="1"
    android:textSize="20sp"
    android:textColor="#FFFFFF"
    android:layout_height="fill_parent"
    android:maxLines="3"
    android:scrollbars="vertical" />"

</TableRow>

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

    <TextView
    android:id="@+id/InputText"
    android:layout_width="0dp"
    android:background="#BBBBBB"
    android:gravity="right"
    android:textColor="#FFFFFF"
    android:layout_weight="1"
    android:layout_marginTop="4dp"
    android:textSize="20sp"
    android:maxLines="3"
    android:scrollbars="vertical" />


</TableRow>

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

    <TextView
    android:id="@+id/memoryStatText"
    android:layout_width="0dp"
    android:background="#BBBBBB"
    android:gravity="right"
    android:textColor="#FFFFFF"
    android:layout_weight="1"
    android:layout_marginTop="4dp"
    android:textSize="15sp"
    android:maxLines="3"
    android:scrollbars="vertical" />       
</TableRow>

<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:weightSum="1" >

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

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

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

    <Button
    android:id="@+id/buttonMR"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.125"    
    android:text="MR"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/buttonClearAll"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp" 
    android:layout_weight="0.125"    
    android:text="C"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/buttonDel"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.125"     
    android:text="del"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/buttonPower"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.125"
    android:textSize="12sp"      
    android:text="pow"
    android:gravity="fill"
    android:onClick="ProcessInput"
    android:layout_height="match_parent" />

    <Button
    android:id="@+id/buttonFactorial"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.166"     
    android:text="x!"
    android:layout_height="match_parent"
    android:onClick="ProcessInput"
    android:gravity="fill" />



</TableRow>

<TableRow
    android:id="@+id/tableRow4"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:weightSum="1" >

    <Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"     
    android:text="1"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"    
    android:text="2"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/button3"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"     
    android:text="3"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/button4"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"    
    android:text="4"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/button5"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"     
    android:text="5"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/button6"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp" 
    android:layout_weight="0.1"    
    android:text="6"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/button7"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"     
    android:text="7"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/button8"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp" 
    android:layout_weight="0.1"    
    android:text="8"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/button9"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"     
    android:text="9"
    android:onClick="ProcessInput"
    android:gravity="fill"
    android:layout_height="match_parent" />"

    <Button
    android:id="@+id/button0"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp" 
    android:layout_weight="0.1"    
    android:text="0"
    android:onClick="ProcessInput"
    android:gravity="fill"
    android:layout_height="match_parent" />

</TableRow>

<TableRow
    android:id="@+id/tableRow5"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:weightSum="1"
    android:layout_weight="0.1" >


    <Button
    android:id="@+id/buttonAdd"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"    
    android:text="+"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/buttonSubtract"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"     
    android:text="-"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/buttonDivide"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"    
    android:text="/"
    android:onClick="ProcessInput"
    android:gravity="fill"
    android:layout_height="match_parent" />

    <Button
    android:id="@+id/buttonMultiply"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"     
    android:text="x"
    android:onClick="ProcessInput"
    android:gravity="fill"
    android:layout_height="match_parent" />

    <Button
    android:id="@+id/buttonPeriod"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"     
    android:text="."
    android:onClick="ProcessInput"
    android:gravity="fill"
    android:layout_height="match_parent" />

    <Button
    android:id="@+id/buttonSign"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"    
    android:text="±"
    android:onClick="ProcessInput"
    android:gravity="fill"
    android:layout_height="match_parent" />

    <Button
    android:id="@+id/buttonEqual"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"    
    android:text="="
    android:onClick="ProcessInput"
    android:layout_height="match_parent" />

    <Button
    android:id="@+id/buttonPercent"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp" 
    android:layout_weight="0.1"
    android:gravity="fill"    
    android:text="%"
    android:layout_height="match_parent"
    android:onClick="ProcessInput" />

    <Button
    android:id="@+id/buttonLog10"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp" 
    android:layout_weight="0.1"    
    android:text="log"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/buttonLoge"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.1"     
    android:text="ln"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

</TableRow>


<TableRow
    android:id="@+id/tableRow6"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:weightSum="1"
    android:layout_weight="0.1" >

    <Button
    android:id="@+id/buttonSin"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.125" 
    android:gravity="fill"
    android:text="sin"
    android:onClick="ProcessInput"
    android:layout_height="match_parent" />

    <Button
    android:id="@+id/buttonCos"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp" 
    android:layout_weight="0.125"
    android:gravity="fill"    
    android:text="cos"
    android:onClick="ProcessInput"
    android:layout_height="match_parent" />

    <Button
    android:id="@+id/buttonTan"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.125" 
    android:gravity="fill"   
    android:text="tan"
    android:onClick="ProcessInput"
    android:layout_height="match_parent" />

    <Button
        android:id="@+id/buttonSinInverse"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp" 
        android:layout_weight="0.125"
        android:textSize="8sp"
        android:gravity="fill"   
        android:text="@string/sin_inv"
        android:onClick="ProcessInput"
        android:layout_height="match_parent"/>

<Button
    android:id="@+id/buttonCosInverse"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp" 
    android:layout_weight="0.125"
    android:gravity="fill" 
    android:textSize="8sp"  
    android:text="@string/cos_inv"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"/>

<Button
    android:id="@+id/buttonTanInverse"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp" 
    android:layout_weight="0.125"
    android:gravity="fill" 
    android:textSize="8sp"  
    android:text="@string/tan_inv"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"/>

<Button
    android:id="@+id/buttonSquare"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp" 
    android:layout_weight="0.125"    
    android:text="sqr"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />

    <Button
    android:id="@+id/buttonSquareRoot"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="0dp"
    android:layout_weight="0.125"    
    android:text="sqrt"
    android:onClick="ProcessInput"
    android:layout_height="match_parent"
    android:gravity="fill" />      

</TableRow>

</TableLayout>
</ScrollView>

Even if i write different XML for different screen layouts, i don't think i can change anything. Its because i can't change anything. I am not going to add more buttons or something like that. Also if i set Buttons, textviews etc. to an accurate size using dp it would still be a problem because then it would be device specific.

I have 9 rows in portrait mode and 7 in landscape mode.I was thinking to calculate height and width of screen before calling. setContentView(R.layout.main); Then calculate total height/9 for portrait mode and height/7 for landscape mode. Can anyone tell me how to proceed?That is how to know dynamically if layout is landscape or portrait , and how to assign height to rows.

[EDIT]

Now i have this

DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    double height = displaymetrics.heightPixels/9;
    double width = displaymetrics.widthPixels/7;

    int Orientation = getResources().getConfiguration().orientation;
    if(Orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        Set  height of all rows.
    }

How to set height of all rows.

user3404195
  • 189
  • 11

2 Answers2

0

keep layout:weight =1 for all buttons and set the layout width for table row fill_parent

user8938
  • 559
  • 1
  • 4
  • 12
0

Based on your other post, since you have different layouts, in the code you can determine orientation with the following code:

int orientation = getResources().getConfiguration().orientation
if(orientation == Configuration.ORIENTATION_LANDSCAPE)
//landscape code...
else
//portrait code...

I posted in both threads for you.

Parnit
  • 1,032
  • 2
  • 8
  • 16
  • I checked it i want to set height of different rows like this: DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); double height = displaymetrics.heightPixels/9; double width = displaymetrics.widthPixels/7; int Orientation = getResources().getConfiguration().orientation; if(Orientation == Configuration.ORIENTATION_LANDSCAPE) { Set height of all rows. } – user3404195 Mar 12 '14 at 16:11
  • You need to get each row and change the layout params: TableRowObject.setLayoutParams(new LayoutParams(widthValue, heightValue)); – Parnit Mar 12 '14 at 16:20
  • I wrote R.id.tablerow0.height but it was error how can i actually acess and set height of row. – user3404195 Mar 12 '14 at 16:23
  • TableRow tRow= (TableRow ) findViewById(R.id.tableRow0); tRow..setLayoutParams(new LayoutParams(widthValue, heightValue)); You need to do this for all the rows – Parnit Mar 12 '14 at 16:26
  • TableRow tRow = (TableRow) findViewById(R.id.tableRow0); tRow.setLayoutParams(width,individualHeight); With this code i got error thatsetLayoutParam is not applicable for arguments(int, int); – user3404195 Mar 12 '14 at 16:37
  • Whoops. Mistake by me. try this : tRow.setLayoutParams(new TableRow.LayoutParams(width,individualHeight)); – Parnit Mar 12 '14 at 16:44
  • Its working almost perfectly. but i need to consider height of actionbar and subtract it to fit the view exaclty. How can i find height of action bar – user3404195 Mar 12 '14 at 17:14
  • I would refer to http://stackoverflow.com/questions/12301510/how-to-get-the-actionbar-height – Parnit Mar 12 '14 at 17:21
  • I just read the answer but i am using appcompat v7 library and the code given there shows error. – user3404195 Mar 12 '14 at 17:22