I am making a simple calculator using LinearLayout.
The problem is that it looks like this :
Instead of like this:
I simply want it to look like in the second image. I don't get why it doesn't.
Please, can someone help me with this?
Here's the pastie with the xml.
It's the android:baselineAligned="false" that you have to apply to all of your nested LinearLayouts.
See enhanced layout here.
use this layout:
use layout weightSum and layout_weight properly in your child buttons to align them with required space:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.example.tsvetan.exercise_4_calculator.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/clear"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="50dp"
android:layout_height="48dp"
android:layout_marginEnd="11dp"
android:layout_marginRight="11dp"
android:onClick="clear"
android:text="C"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="#+id/number"
android:layout_width="306dp"
android:layout_height="56dp"
android:layout_gravity="end"
android:ems="10"
android:inputType="numberSigned|numberDecimal"
android:text="0"
android:textAlignment="viewEnd"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:weightSum="5">
<Button
android:id="#+id/but7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".8"
android:onClick="seven"
android:text="7" />
<Button
android:id="#+id/but8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".8"
android:onClick="eight"
android:text="8" />
<Button
android:id="#+id/but9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".8"
android:onClick="nine"
android:text="9" />
<Button
android:id="#+id/plus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.2"
android:elevation="0dp"
android:onClick="onOperatorClick"
android:padding="0px"
android:text="+"
android:textAlignment="center"
android:textSize="30sp"
android:textStyle="bold" />
<Button
android:id="#+id/multiply"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.2"
android:onClick="onOperatorClick"
android:text="*"
android:textAlignment="center"
android:textSize="36sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:weightSum="5">
<Button
android:id="#+id/but4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".8"
android:onClick="four"
android:text="4" />
<Button
android:id="#+id/but5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".8"
android:onClick="five"
android:text="5" />
<Button
android:id="#+id/but6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".8"
android:onClick="six"
android:text="6" />
<Button
android:id="#+id/minus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.2"
android:onClick="onOperatorClick"
android:padding="0px"
android:text="-"
android:textAlignment="center"
android:textSize="36sp"
android:textStyle="bold" />
<Button
android:id="#+id/division"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.2"
android:onClick="onOperatorClick"
android:text="/"
android:textAlignment="center"
android:textSize="36sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:weightSum="5">
<Button
android:id="#+id/but1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".8"
android:onClick="one"
android:text="1" />
<Button
android:id="#+id/but2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".8"
android:onClick="two"
android:text="2" />
<Button
android:id="#+id/but3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".8"
android:onClick="three"
android:text="3" />
<Button
android:id="#+id/equals"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.4"
android:onClick="onOperatorClick"
android:padding="0px"
android:text="="
android:textAlignment="center"
android:textColorLink="#android:color/holo_orange_dark"
android:textSize="36sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:weightSum="5">
<Button
android:id="#+id/but0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.6"
android:onClick="zero"
android:text="0" />
<Button
android:id="#+id/butMult"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".8"
android:onClick="point"
android:text="."
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
output:
You don't need to put height to all of your buttons. Just put the height of linear layout and the rest will be match_parent. You will get what you want:
Related
How do i make the numpad layout as shown in the uploaded picture.
This is my current code which would not show anything in the design view. I used this code previously on other views which work but this time it does not work and none of the buttons were visible at all in the design view.And also how do i make the 0 button as it is in the picture. This is my 2nd time asking questions in stackoverflow so do let me know if my questions are not clear and also english is not my first language. Thank you in advanced!
Edit: I have to make this layout through xml coding and not the usual thing
where the users click on the textview and the numpad of the phone
shows up. I hope this clarify the doubts of my question. I have to
make the clear, cancel and exit button as well.
<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"
android:weightSum="4"
android:layout_weight="4"
android:background="#drawable/background"
tools:context=".insert_amount">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp">
<VideoView
android:id="#+id/videoview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player"
android:textSize="30dp"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:textColor="#color/white"
/>
<TextView
android:id="#+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display ID"
android:textSize="30dp"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/content_box"
android:layout_marginTop="10dp"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/in"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top Up Amount (IN)"
android:textSize="30dp"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
/>
<TextView
android:id="#+id/textviewnumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="0.00"
android:textSize="40dp"
android:textStyle="bold"
android:gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="4"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#drawable/login_signoutbutton"
android:layout_height="fill_parent"
android:text="1"
/>
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:background="#drawable/login_signoutbutton"
android:text="2"
/>'
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#drawable/login_signoutbutton"
android:layout_height="fill_parent"
android:text="3"
/>
<ImageButton
android:id="#+id/buttonclear"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:scaleType="fitCenter"
android:background="#drawable/login_signoutbutton"
android:src="#drawable/clear_button"
android:layout_margin="20dp"
/>
Try this
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Player"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="12345679"
android:textColor="#FFFFFF" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:src="#drawable/kid_goku" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Top up Amount" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Top up Amount" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="7" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="0" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="9" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginHorizontal="2dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:background="#9b5006"
android:drawableLeft="#drawable/ic_camera"
android:text="Clear"
android:textColor="#FFFFFF"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginHorizontal="2dp"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:background="#9b5006"
android:drawableLeft="#drawable/ic_camera"
android:text="Cancel"
android:textColor="#FFFFFF"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginHorizontal="2dp"
android:layout_marginTop="12dp"
android:layout_weight="1"
android:background="#9b5006"
android:drawableLeft="#drawable/ic_camera"
android:text="Next"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
OUTPUT
Your layout height exceeding the device display. You have to use ScrollView here. Also for making the number keypad using GridLayout link will be better choice and you can easily get '0' button position using this view.
when I try my app on my computer all the buttons are in the correct place.
but when I testing my app on my phone all the buttons changed.
how can I fix that?
This is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<Space
android:id="#+id/s1"
android:layout_width="match_parent"
android:layout_height="40dp"/>
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/s1"
android:textSize="30sp" />
<Space
android:id="#+id/s2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#id/tv"/>
<LinearLayout
android:id="#+id/ll789"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/s2"
android:orientation="horizontal">
<Button
android:id="#+id/buttonSeven"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"
android:textSize="20sp" />
<Button
android:id="#+id/buttonEight"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"
android:textSize="20sp" />
<Button
android:id="#+id/buttonNine"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
android:textSize="20sp" />
<Button
android:id="#+id/buttonDivide"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll456"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ll789"
android:orientation="horizontal">
<Button
android:id="#+id/buttonFour"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"
android:textSize="20sp" />
<Button
android:id="#+id/buttonFive"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"
android:textSize="20sp" />
<Button
android:id="#+id/buttonSix"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"
android:textSize="20sp" />
<Button
android:id="#+id/buttonMultiply"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll123"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ll456"
android:orientation="horizontal">
<Button
android:id="#+id/buttonOne"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
android:textSize="20sp" />
<Button
android:id="#+id/buttonTwo"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"
android:textSize="20sp" />
<Button
android:id="#+id/buttonThree"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"
android:textSize="20sp" />
<Button
android:id="#+id/buttonSubtract"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ll123"
android:orientation="horizontal">
<Button
android:id="#+id/buttonDot"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."
android:textSize="20sp" />
<Button
android:id="#+id/buttonZero"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
android:textSize="20sp" />
<Button
android:id="#+id/buttonEqual"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="
android:textSize="20sp" />
<Button
android:id="#+id/buttonAdd"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/llC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ll0"
android:orientation="horizontal">
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/buttonClear"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C"
android:textSize="20sp" />
</LinearLayout>
I think maybe that's happened because of my phone in Hebrew and my computer in English.
This is most likely happening because the <application> tag in your AndroidManifest.xml file specifies android:supportsRtl="true". Assuming this attribute is there, any time the user (i.e. you) has their phone set to a right-to-left language, the contents of your LinearLayouts will be reversed.
You can set this attribute to false or you can add android:layoutDirection="ltr" to your LinearLayouts.
Maybe a duplicate, but I haven't found a solution yet. So, I have a vertical LinearLayout with a header which is a horizontal layout and a main content section (FrameLayout). Within the header I got three children of which one should be left align, the other one right aligned and the third on should fill the space in between.
I think I have to use a RelativeLayout instead of a LinearLayout, but if I do so, it fill's the parent completely and nothing else is visible.
Any suggestion? Basically the two button inside the red rectangle should be aligned to the right.
Here's the XML.
<LinearLayout
android:id="#+id/sort_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:text="#string/sort_by"
android:textColor="#color/white80"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/view_group"
style="#android:style/ButtonBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/background_dark"
android:orientation="horizontal"
android:padding="0dp" >
<ImageButton
android:id="#+id/btn_view_grid"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:onClick="onShowGrid"
android:src="#drawable/ic_action_view_as_grid" />
<ImageButton
android:id="#+id/btn_view_list"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:onClick="onShowGrid"
android:src="#drawable/ic_action_view_as_list" />
</RadioGroup>
<RadioGroup
android:id="#+id/sort_group"
style="#android:style/ButtonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_dark"
android:gravity="center"
android:orientation="horizontal"
android:padding="0dp" >
<Button
android:id="#+id/btn_sort_popular"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_popular"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_new"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_newest"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_hard"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_hardest"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_easy"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_easiest"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
</RadioGroup>
</LinearLayout>
<FrameLayout
android:id="#+id/preview_fragment"
android:background="#android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
something similar may help
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/sort_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:text="sort_by"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/sort_group"
style="#android:style/ButtonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_dark"
android:gravity="center"
android:orientation="horizontal"
android:padding="0dp" >
<Button
android:id="#+id/btn_sort_popular"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="sort_popular"
android:textColor="#FFFF"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_new"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:textColor="#FFFF"
android:text="sort_newest"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_hard"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:textColor="#FFFF"
android:text="sort_hardest"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_easy"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:textColor="#FFFF"
android:text="sort_easiest"
android:textSize="20sp" />
</RadioGroup>
<LinearLayout
android:id="#+id/sort_layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal"
android:paddingBottom="10dp" >
<ImageButton
android:id="#+id/btn_view_grid"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_weight="1"
android:gravity="right"
android:onClick="onShowGrid"
android:src="#drawable/ic_action_view_as_grid" />
<ImageButton
android:id="#+id/btn_view_list"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_weight="1"
android:gravity="right"
android:onClick="onShowGrid"
android:src="#drawable/ic_action_view_as_list" />
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/preview_fragment"
android:background="#android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I finally figured out how to achieve this. I had to use the following values inside a RelativeLayout instead of a LinearLayout correctly:
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/view_group"
android:layout_toRightOf="#+id/label_sort"
Here's the XML:
<RelativeLayout
android:id="#+id/sort_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/label_sort"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:fontFamily="sans-serif-condensed"
android:text="#string/sort_by"
android:gravity="center"
android:textColor="#color/white80"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/view_group"
style="#android:style/ButtonBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#android:color/background_dark"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:padding="0dp" >
<ImageButton
android:id="#+id/btn_view_grid"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:onClick="onShowGrid"
android:src="#drawable/ic_action_view_as_grid" />
<ImageButton
android:id="#+id/btn_view_list"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:onClick="onShowList"
android:src="#drawable/ic_action_view_as_list" />
</RadioGroup>
<RadioGroup
android:id="#+id/sort_group"
style="#android:style/ButtonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/view_group"
android:layout_toRightOf="#+id/label_sort"
android:background="#android:color/background_dark"
android:gravity="center"
android:orientation="horizontal"
android:padding="0dp" >
<Button
android:id="#+id/btn_sort_popular"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_popular"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_new"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_newest"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_hard"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_hardest"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
<Button
android:id="#+id/btn_sort_easy"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="sans-serif-condensed"
android:onClick="onSortChanged"
android:text="#string/sort_easiest"
android:textColor="#drawable/sort_button_selector"
android:textSize="20sp" />
</RadioGroup>
</RelativeLayout>
<FrameLayout
android:id="#+id/preview_fragment"
android:background="#android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Android Studio 0.4.5
Hello,
I have created a dialog box from an Activity. Which displays like this:
The cancel has a missing 'l' and the delete has a missing 'e'.
I have tried to adjust the text size, but that didn't work. I have been playing around with it. But can't seem to find a solution.
Here my xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ivRabbitLogo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#ff20a3ff"
android:contentDescription="Logo of Application" />
<EditText
android:id="#+id/etUpdateDlgName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="text"
android:hint="Name"
android:textSize="22sp" />
<EditText
android:id="#+id/etUpdateDlgPhone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="phone"
android:hint="Phone"
android:textSize="22sp" />
<EditText
android:id="#+id/etUpdateDlgEmail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textEmailAddress"
android:hint="Email"
android:textSize="22sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/btnCancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:textSize="20sp"
android:layout_weight="1" />
<Button
android:id="#+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
android:textSize="20sp"
android:layout_weight="1" />
<Button
android:id="#+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete"
android:textSize="20sp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Many thanks for any suggestions,
Have a look at this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivRabbitLogo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#ff20a3ff"
android:contentDescription="Logo of Application"
android:scaleType="center" />
<EditText
android:id="#+id/etUpdateDlgName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Name"
android:inputType="text"
android:textSize="22sp" />
<EditText
android:id="#+id/etUpdateDlgPhone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Phone"
android:inputType="phone"
android:textSize="22sp" />
<EditText
android:id="#+id/etUpdateDlgEmail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Email"
android:inputType="textEmailAddress"
android:textSize="22sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
android:textSize="20sp" />
<Button
android:id="#+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save"
android:textSize="20sp" />
<Button
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Delete"
android:textSize="20sp" />
</LinearLayout>
you should not use android:layout_weight="1" in BUtton(childs), use android:weight="1" instead.
Use this...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivRabbitLogo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#ff20a3ff"
android:contentDescription="Logo of Application"
android:scaleType="center" />
<EditText
android:id="#+id/etUpdateDlgName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Name"
android:inputType="text"
android:textSize="22sp" />
<EditText
android:id="#+id/etUpdateDlgPhone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Phone"
android:inputType="phone"
android:textSize="22sp" />
<EditText
android:id="#+id/etUpdateDlgEmail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Email"
android:inputType="textEmailAddress"
android:textSize="22sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="#+id/btnCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
android:textSize="20sp" />
<Button
android:id="#+id/btnSave"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save"
android:textSize="20sp" />
<Button
android:id="#+id/btnDelete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Delete"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
Declare you button with following change,
Just add a weight tag with Button like below,
<Button
android:id="#+id/btnCancel"
android:layout_width="0dp" // Change here
android:layout_height="wrap_content"
android:text="Cancel"
android:textSize="20sp"
android:layout_weight="1" />
<Button
android:id="#+id/btnSave"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Save"
android:textSize="20sp"
android:layout_weight="1" />
<Button
android:id="#+id/btnDelete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Delete"
android:textSize="20sp"
android:layout_weight="1" />
try this in your code...hopefully this will work..
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/btnCancel"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:textSize="20sp"
android:layout_weight="1" />
<Button
android:id="#+id/btnSave"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Save"
android:textSize="20sp"
android:layout_weight="1" />
<Button
android:id="#+id/btnDelete"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Delete"
android:textSize="20sp"
android:layout_weight="1" />
</LinearLayout>
I'm testing Android Studio and I'm trying to write a simple calculator just to test it andl learn Android.
What I'd like to achieve is something like this:
and I'd like to have this behavior with every device, either portrait or landscape.
What I've though is to make everything relative to the string "Result is 0:", so something like this:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="org.mennini.mycalc.MainActivity$PlaceholderFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Sum is: 0"
android:id="#+id/textView"
android:onClick="onButtonClick"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="#+id/button"
android:layout_marginTop="40dp"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/button"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="#+id/button3"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="#+id/button4"
android:layout_below="#+id/button"
android:layout_toRightOf="#+id/button2"
android:layout_marginTop="19dp"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/button5"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/button4"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="#+id/button6"
android:layout_alignTop="#+id/button4"
android:layout_toRightOf="#+id/button4"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/button7"
android:layout_below="#+id/button4"
android:layout_toRightOf="#+id/button5"
android:layout_marginTop="19dp"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/button8"
android:layout_alignTop="#+id/button7"
android:layout_toLeftOf="#+id/button7"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="#+id/button9"
android:layout_alignTop="#+id/button7"
android:layout_toRightOf="#+id/button7"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="#+id/button10"
android:layout_below="#+id/button7"
android:layout_alignLeft="#+id/button8"
android:layout_alignStart="#+id/button8"
android:layout_marginTop="19dp"
android:layout_toLeftOf="#+id/button9"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
android:id="#+id/button11"
android:layout_alignTop="#+id/button10"
android:layout_toRightOf="#+id/button10"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="#+id/button12"
android:layout_below="#+id/button10"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:onClick="onButtonClick" />
With an off number of columns is easy, but how can I deal with an even number, such in the example?
More, with Android Studio I've tried to use Table Layout and Grid Layout, but I haven't understood how to set a grid with specific number of rows and cols, i.e. a 5 rows x 4 columns grid, i.e. I can drag a GridLayout and set the number of rows and columns, but as I drag over it some controls, say a Button, I see underneath a green grid with a lot more cells than expected, so my intention to draw button on my 5x4 grid and have the grid itself horizontally centered gets frustrated.
Finally (thanks for reading up to here) the question: how can I deal with this situation? Is my idea of using all buttons relative to textview good or stupid?
Is my idea of using a GridLayout a good or stupid idea?
In which smart way can I deal with this problem????
Thanks a lot
you can use linearLayout and give needed weight.
here is my code which I gave 1/5=0.2 weight for each row and 1/4=0.25 for each column.
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="onButtonClick"
android:text="Sum is: 0"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="32dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:orientation="horizontal">
<Button
android:id="#+id/button14"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="+" />
<Button
android:id="#+id/button15"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="7" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="8" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="4" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="2" />
</LinearLayout>
<Button
android:id="#+id/button17"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="0" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:orientation="vertical">
<Button
android:id="#+id/button6"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="*" />
<Button
android:id="#+id/button7"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="9" />
<Button
android:id="#+id/button8"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="6" />
<Button
android:id="#+id/button9"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="3" />
<Button
android:id="#+id/button10"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="." />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:orientation="vertical">
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="/" />
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:text="Enter"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Have you ever thought about using TableLayout?
you can thought about Linear Layout with vertical orientation for first three columns (each row added to individual linear layout with Horizontal orientation, which means that you need for four linear layouts, then all added to first layout which Linear Layout with vertical orientation), then add the first Linear layout and the last column (which added first to another vertical layout) to another linear layout with Horizontal orientation!
Here also you can find good example:
http://mrbool.com/how-to-create-a-calculator-app-for-android/28100
Also here full xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/plus"
android:text="+"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/min"
android:text="-"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/mul"
android:text="*"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/seven"
android:text="7"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/eight"
android:text="8"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/nine"
android:text="9"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/four"
android:text="4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/five"
android:text="5"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/six"
android:text="6"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/one"
android:text="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/two"
android:text="2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/three"
android:text="3"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/one"
android:text="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/two"
android:text="2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/three"
android:text="3"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/zero"
android:layout_weight="2"
android:text="0"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/dot"
android:text="."
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/slash"
android:text="/"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "#+id/enter"
android:layout_weight="2"
android:text="Enter"
/>
</LinearLayout>
</LinearLayout>
regards #Sandman42,
<LinearLayout 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:orientation="vertical" 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=".Home" android:background="#fff" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="#string/disp" android:id = "#+id/display" android:hint="#string/dispHint" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="20dp"> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/seven" android:text="#string/seven" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/eight" android:text="#string/eight" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/nine" android:text="#string/nine" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/div" android:text="#string/div" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="20dp"> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/four" android:text="#string/four" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/five" android:text="#string/five" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/six" android:text="#string/six" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/mul" android:text="#string/mul" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="20dp"> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/one" android:text="#string/one" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/two" android:text="#string/two" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/three" android:text="#string/three" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/sub" android:text="#string/sub" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="20dp"> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/cancel" android:text="#string/cancel" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/zero" android:text="#string/zero" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/equal" android:text="#string/equal" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "#+id/add" android:text="#string/add" /> </LinearLayout> </LinearLayout>