I have a layout which looks perfect in portrait mode but looks worst in landscape mode, I also created different folder for landscape. Still now able to make it perfect for all screens in landscape.
Here is the xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top|center"
android:orientation="vertical"
android:padding="5dp" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/gray"
android:gravity="center" >
<TextView
android:id="#+id/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/imageView1"
android:layout_alignParentTop="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/imageView1"
android:text="Choose Mode"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/question_image" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/menu_image" />
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="#+id/button1"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="1" />
<Button
android:id="#+id/button2"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="2" />
<Button
android:id="#+id/button3"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="3" />
<Button
android:id="#+id/button4"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="4" />
<Button
android:id="#+id/button5"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="5" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="#+id/button6"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="6" />
<Button
android:id="#+id/button7"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
<Button
android:id="#+id/button8"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="8" />
<Button
android:id="#+id/button9"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="9" />
<Button
android:id="#+id/button10"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="#+id/button11"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="8" />
<Button
android:id="#+id/button12"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
<Button
android:id="#+id/button13"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="9" />
<Button
android:id="#+id/button14"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
<Button
android:id="#+id/button15"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="8" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="#+id/button16"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
<Button
android:id="#+id/button17"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
<Button
android:id="#+id/button18"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
<Button
android:id="#+id/button19"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
<Button
android:id="#+id/button20"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="#+id/button21"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
<Button
android:id="#+id/button22"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
<Button
android:id="#+id/button23"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
<Button
android:id="#+id/button24"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
<Button
android:id="#+id/button25"
style="#style/box_sky_blue"
android:layout_weight="1"
android:text="7" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center" >
<ImageView
android:id="#+id/imageButton1"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:background="#drawable/reset" />
<ImageView
android:id="#+id/imageButton2"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_weight="0.25"
android:background="#drawable/check_solution" />
<ImageView
android:id="#+id/imageButton3"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_weight="0.25"
android:background="#drawable/hint" />
<ImageView
android:id="#+id/imageButton4"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_weight="0.25"
android:background="#drawable/next" />
</LinearLayout>
</LinearLayout>
</ScrollView>
For portrait
For Landscape
Do i need to remove the weight from those four icons, help me out :(
The main problem I see is the ImageView's.
First off , never use "fill_parent" values since they are deprecated, instead use "match_parent".
Then, you need to remove weight property from them aswell. That's why they try to fill the screen by stretching which makes them look out of proportion.
Use fixed dp (density pixel) size (width/height) for the images, so that they maintain a proportion, no matter the size or the display quality.
You can then, use different sizes and element display for both landscape and portrait using layout and layout-land folders, both storing different layout compositions with the same name so that they get called by the android framework depending on orientation.
You have official documentation about supporting multiple screen sizes / orientation (more specifically) here.
on the imageview of last 4 button add this attributes -
android:adjustViewBounds="true"
android:scaleType="fitCenter"
Related
A picture will help understand what I'm talking about:
I have three horizontal LinearLayouts, each of which contains six Buttons:
On the first row, each button has its layout_width set to "0dp" and it's layout_weight set to "1", except for button 4 where it's "2". The result is pretty much exactly what I'd expect.
The second row is identical to the first, except that all of the layout_width values are set to "wrap_content". My expectation was that each button would be assigned a width according to how much space it needs, and then the leftover space would be distributed equally among them, giving extra space to button 4. However, button 4 is actually smaller than its peers. Why is that happening?
In the third row, I deleted all of the weights and left the widths set to "wrap_content". I would have expected that each button would only be as wide as it needs to be, with some blank space at the end of the row. Instead, all of the buttons are actually too wide, and don't even fit on the screen!
Can anybody explain what I'm missing here?
By comparison, the next three rows are identical to the first three, except that Button has been changed to TextView. These rows all look exactly as I would expect.
For reference, the layout.xml file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SecondFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button12"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="#+id/button13"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:id="#+id/button14"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:id="#+id/button15"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="4" />
<Button
android:id="#+id/button16"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<Button
android:id="#+id/button17"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/button12"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="1" />
<TextView
android:id="#+id/button13"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="2" />
<TextView
android:id="#+id/button14"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="3" />
<TextView
android:id="#+id/button15"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="4" />
<TextView
android:id="#+id/button16"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="5" />
<TextView
android:id="#+id/button17"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="6" />
</LinearLayout>
</LinearLayout>
OK, figured it out. Leaving this up in case it helps someone in the future.
Buttons have a minWidth attribute which is 88dp by default. Changing this to zero made everything come out the way it should.
My buttons on 2.3 are not showing like the suppose to.
Anyone could help me?
This is how they are shown on 2.3:
And this is on 4.4 how it should be:
This is my layout on V9:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/numkeyBoard"
>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
style="#style/test.Button.Num"
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="1"
android:tag="1" />
<Button
style="#style/test.Button.Num"
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="2"
android:tag="2" />
<Button
style="#style/test.Button.Num"
android:text="3"
android:id="#+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:tag="3" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
style="#style/test.Button.Num"
android:text="4"
android:id="#+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:tag="4" />
<Button
style="#style/test.Button.Num"
android:text="5"
android:id="#+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:tag="5" />
<Button
style="#style/test.Button.Num"
android:text="6"
android:id="#+id/button_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:tag="6" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
style="#style/test.Button.Num"
android:text="7"
android:id="#+id/button_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:tag="7" />
<Button
style="#style/test.Button.Num"
android:text="8"
android:id="#+id/button_8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:tag="8" />
<Button
style="#style/test.Button.Num"
android:text="9"
android:id="#+id/button_9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:tag="9" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
style="#style/test.Button.Num"
android:text="C"
android:id="#+id/button_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:tag="C" />
<Button
style="#style/test.Button.Num"
android:text="0"
android:id="#+id/button_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:tag="0"
/>
<Button
style="#style/test.Button.Num"
android:text="OK"
android:id="#+id/button_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:enabled="false"
android:tag="ok" />
</TableRow>
EDIT:
When I add a number second number to 1 2 3 ... until 9 then the buttons are all the same.
How can I fix this without adding a 2 digit?
Try adding android:stretchColumns="*" to the attributes defined under TableLayout
Here are snapshots of what my application looks like
It fits on some devices and not on others:
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.
keep layout:weight =1 for all buttons and set the layout width for table row fill_parent
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.
I have set up 4 linear layouts: 2 vertical ones, side by side and 2 horizontal ones, arranged vertically.
This will give me a total of 3 vertical ones.
Then end goal is to have one layout for multiple screen sizes.
I set the weight of all 4 layouts to "1" to fill evenly.
I have tried to use android:layout_height="wrap_content", but it does not fill the entire background.
If I use android:layout_height="fill_parent", the layouts get clipped.
Here is screenshot of "wrap_content"
Here is screenshot of "fill_parent"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff606060"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SimpleRGB_Main" >
<LinearLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:text="Scene 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="Scene 2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="Scene 3" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button3"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="Scene 4" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button4"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="Scene 5" />
</LinearLayout>
<LinearLayout
android:id="#+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/relativeLayout1"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_toRightOf="#+id/button1"
android:layout_weight="1"
android:text="Scene 6" />
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button6"
android:layout_toRightOf="#+id/button2"
android:layout_weight="1"
android:text="Scene7" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button7"
android:layout_toRightOf="#+id/button3"
android:layout_weight="1"
android:text="Scene8" />
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button8"
android:layout_toRightOf="#+id/button4"
android:layout_weight="1"
android:text="Scene9" />
<Button
android:id="#+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button9"
android:layout_toRightOf="#+id/button5"
android:layout_weight="1"
android:text="Scene10" />
</LinearLayout>
<LinearLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RECORD" />
<Button
android:id="#+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="PLAY" />
</LinearLayout>
<LinearLayout
android:id="#+id/relativeLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout3"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RECORD1" />
<Button
android:id="#+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="PLAY1" />
</LinearLayout>
Here is what I am trying to attempt. Green is the layouts filled with buttons and or widgets.
Putting layout weights on children of RelativeLayout does not work. Weights only work on LinearLayout (that is, children of linear layout, not linear layout itself). Try starting by making your root layout linear, not RelativeLayout.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff606060"
android:weightSum="6"
android:layout_gravity="fill_horizontal|center_vertical"
tools:context=".SimpleRGB_Main" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:weightSum="2"
android:layout_weight="1">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:text="Scene 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="Scene 6" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="2">
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="Scene 2" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button3"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="Scene 7" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:weightSum="2">
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button4"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="Scene 3" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_toRightOf="#+id/button1"
android:layout_weight="1"
android:text="Scene 8" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="2">
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button6"
android:layout_toRightOf="#+id/button2"
android:layout_weight="1"
android:text="Scene 4" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button7"
android:layout_toRightOf="#+id/button3"
android:layout_weight="1"
android:text="Scene 9" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_weight="1">
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button8"
android:layout_toRightOf="#+id/button4"
android:layout_weight="1"
android:text="Scene 5" />
<Button
android:id="#+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button9"
android:layout_toRightOf="#+id/button5"
android:layout_weight="1"
android:text="Scene 10" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:weightSum="2">
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RECORD" />
<Button
android:id="#+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RECORD 1" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:weightSum="2">
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="PLAY " />
<Button
android:id="#+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="PLAY 1" />
</TableRow>
</TableLayout>
graphical view
Hi Guys I'd like to create a menu page with buttons like in the picture
http://i.stack.imgur.com/q1Bx7.jpg
I already tried with linear layouts and table layouts, but it wasn't working. Can anyone help me? The buttons should be relative in size so the page is shown correctly on tablets and on smartphones.
Edit: This is my code
<TableLayout
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_weight="0.50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_weight="0.50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button3"
android:layout_weight="0.50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_weight="0.50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
</TableLayout>
this is how i did it in my app.. you can reference it. it does what you showed in the image.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#null"
android:text="1"
android:textColor="#0099CC"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#null"
android:text="2"
android:textColor="#0099CC"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="10sp"
android:layout_marginTop="10dip"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="#+id/btn3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#null"
android:text="3"
android:textColor="#0099CC"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="#+id/btn4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#null"
android:text="4"
android:textColor="#0099CC"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>