Android layout xml: center layout, element width portion, numbers only input for EditText
The layout xml file below is a RelativeLayout containing a LinearLayout which contains a EditText, a Button, and a TextView.
-
The LinearLayout is centered in the screen by android:layout_centerInParent=”true”, and this only works if it is inside the RelativeLayout.
EditText only accepts positive numbers and negative numbers by android:inputType=”numberDecimal|numberSigned”.
EditText, Button and TextView are lined up on the same line because they are in the LinearLayout, and the LinearLayout says android:orientation=”horizontal”.
The EditText will occupy 6/10 of the total width of the LinearLayout because its layout_weight is set to 6 and the layout_width is set to 0dp.
The Button will occupy 3/10 of the total width of the LinearLayout because its layout_weight is set to 3 and the layout_width is set to 0dp.
The TextView, will occupy 1/10 of the total width of the LinearLayout because its layout_weight is set to 1 and the layout_width is set to 0dp.
[code language=”xml”]
<RelativeLayout 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: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=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true">
<EditText
android:id="@+id/number_input"
android:layout_weight="6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal|numberSigned"
android:maxLength="15"
android:hint="Enter a number" />
<Button
android:id="@+id/submit_button"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Submit" />
<TextView
android:id="@+id/smiley_face"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=":)"
/>
</LinearLayout>
</RelativeLayout>
[/code]
Search within Codexpedia

Search the entire web
