some of the components goes out of screen - android

I have developed an android app that works fine, but the problem is that when I run my application on small screen phones then some components goes out of screen. So please tell what should I do with my app so that it fits on every screen?
Should I create all of these folders (given below) and copy-paste all of my app's layout files in each of these folders? Or I need to design different layout files for each of these folders?
I am new to android, please help by giving a simple example.
Folders:
res/layout/my_layout.xml
res/layout-small/my_layout.xml
res/layout-large/my_layout.xml
res/layout-xlarge/my_layout.xml
res/layout-xlarge-land/my_layout.xml
res/drawable-mdpi/my_icon.png
res/drawable-hdpi/my_icon.png
res/drawable-xhdpi/my_icon.png
XML of MainActivity of My Code:
<?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: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="com.example.appdeveloper.appname.MainActivity"
android:background="#ff003b"
android:focusableInTouchMode="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/appLogoImage_id"
android:src="#drawable/applogo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="1000dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="50dp"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/loginPassword_id"
android:textAlignment="center"
android:hint="Enter Application Password"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:layout_below="#+id/appLogoImage_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="50dp"
android:background="#android:color/transparent"
android:visibility="visible"
android:textCursorDrawable="#drawable/white_cursor" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="REGISTRATION"
android:id="#+id/regTitle_id"
android:layout_below="#+id/appLogoImage_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#ffffff"
android:textAlignment="center"
android:layout_marginTop="50dp"
android:visibility="invisible"
android:textStyle="bold" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions|text"
android:ems="10"
android:id="#+id/regName_id"
android:layout_below="#+id/loginPassword_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Your Good Name"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_marginTop="0dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/regPassword_id"
android:layout_below="#+id/regName_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Application Password"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/regVerifyPass_id"
android:layout_below="#+id/regPassword_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Verify Password"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/regRingCode_id"
android:hint="Ringing Codeword"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regVerifyPass_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/regLocationCode_id"
android:hint="Location Codeword"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/regRingCode_id"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/regNum1_id"
android:hint="1st Trusted Number"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regLocationCode_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:phoneNumber="false" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/regNum2_id"
android:hint="2nd Trusted Number"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regNum1_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:phoneNumber="false" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/regNum3_id"
android:hint="3rd Trusted Number"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regNum2_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:phoneNumber="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:id="#+id/regButton_id"
android:layout_below="#+id/regNum3_id"
android:layout_marginTop="15dp"
android:layout_alignLeft="#+id/appLogoImage_id"
android:layout_alignStart="#+id/appLogoImage_id"
android:layout_alignRight="#+id/appLogoImage_id"
android:layout_alignEnd="#+id/appLogoImage_id"
android:visibility="invisible" />
</RelativeLayout>

Unless you soft code your dimensions (margin, padding, textsize) in dimen.xml file, your app will have different layout in phones having different ppi (pixel per inch).
So add all your margins specially the one having 1000dp in Dimens.xml.
Reason: Android handles all the cases (hdpi,mdpi,xhdpi, ....) on its own by going through dimens.xml file and in your case you are focusing on the preview screen in XMl layout editor that is having a fixed ppi and ignoring the cases of other screens having different ppi.
Your layout file
<?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: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="com.example.appdeveloper.appname.MainActivity"
android:background="#ff003b"
android:focusableInTouchMode="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/appLogoImage_id"
android:src="#drawable/applogo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/margin_0" />
<EditText
android:layout_width="wrap_content"
android:layout_height="#dimen/margin_50"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/loginPassword_id"
android:textAlignment="center"
android:hint="Enter Application Password"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:layout_below="#+id/appLogoImage_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="#dimen/margin_50"
android:background="#android:color/transparent"
android:visibility="visible"
android:textCursorDrawable="#drawable/white_cursor" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="REGISTRATION"
android:id="#+id/regTitle_id"
android:layout_below="#+id/appLogoImage_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#ffffff"
android:textAlignment="center"
android:layout_marginTop="#dimen/margin_50"
android:visibility="invisible"
android:textStyle="bold" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions|text"
android:ems="10"
android:id="#+id/regName_id"
android:layout_below="#+id/loginPassword_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Your Good Name"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_marginTop="#dimen/margin_0"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/regPassword_id"
android:layout_below="#+id/regName_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Application Password"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_marginTop="#dimen/margin_10"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/regVerifyPass_id"
android:layout_below="#+id/regPassword_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Verify Password"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_marginTop="#dimen/margin_10"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/regRingCode_id"
android:hint="Ringing Codeword"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regVerifyPass_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="#dimen/margin_10"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/regLocationCode_id"
android:hint="Location Codeword"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/regRingCode_id"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="#dimen/margin_10"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/regNum1_id"
android:hint="1st Trusted Number"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regLocationCode_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="#dimen/margin_10"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:phoneNumber="false" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/regNum2_id"
android:hint="2nd Trusted Number"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regNum1_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="#dimen/margin_10"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:phoneNumber="false" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/regNum3_id"
android:hint="3rd Trusted Number"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regNum2_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="#dimen/margin_10"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:phoneNumber="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:id="#+id/regButton_id"
android:layout_below="#+id/regNum3_id"
android:layout_marginTop="#dimen/margin_15"
android:layout_alignLeft="#+id/appLogoImage_id"
android:layout_alignStart="#+id/appLogoImage_id"
android:layout_alignRight="#+id/appLogoImage_id"
android:layout_alignEnd="#+id/appLogoImage_id"
android:visibility="invisible" />
</RelativeLayout>
Your dimens.xml in res/values
<resources>
<dimen name="margin_10">10dp</dimen>
<dimen name="margin_0">0dp</dimen>
<dimen name="margin_15">15dp</dimen>
<dimen name="margin_30">30dp</dimen>
<dimen name="margin_50">50dp</dimen>
<dimen name="margin_20">20dp</dimen>
</resources>

Related

Text boxes on top of each other?

I'm designing my app and ive come across some sort of bug. On the design view of my application everything looks fine but when i run the emulator its got two of my text boxes on top of each other and im unsure of how to fixing it ?
This is my xml code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#drawable/gradient"
tools:context=".Registration_form">
<ImageView
android:id="#+id/imageView2"
android:layout_width="240dp"
android:layout_height="298dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="-1dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="60dp"
app:srcCompat="#drawable/hotellogo" />
<EditText
android:id="#+id/editText1"
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginTop="308dp"
android:layout_marginEnd="34dp"
android:layout_marginRight="34dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_fn"
android:ems="10"
android:hint="Full name"
android:inputType="textPersonName"
android:textColor="#E1F7F7"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#807D7D"
android:textSize="16dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginTop="362dp"
android:layout_marginEnd="35dp"
android:layout_marginRight="35dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_user"
android:ems="10"
android:hint="Username (email)"
android:inputType="textEmailAddress"
android:textColor="#E1F7F7"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#807D7D"
android:textSize="16dp" />
<EditText
android:id="#+id/editText3"
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:layout_marginEnd="35dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="285dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_pass"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textColor="#E1F7F7"
android:textColorHint="#807D7D" />
<EditText
android:id="#+id/editText4"
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:layout_marginEnd="35dp"
android:layout_marginRight="35dp"
android:layout_marginBottom="239dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_phone"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
android:textColor="#E1F7F7"
android:textColorHint="#807D7D" />
<EditText
android:id="#+id/editText5"
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginTop="507dp"
android:layout_marginEnd="34dp"
android:layout_marginRight="34dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_room"
android:ems="10"
android:hint="Room number"
android:inputType="textPersonName"
android:textColor="#E1F7F7"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#807D7D"
android:textSize="16dp" />
This is what it looks like:
This is what it looks like so far
You must use the attribute android:layout_below to position a view below another view inside a RelativeLayout.
I set the margins between the edit texts to 10dp, you can change them if you want:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#drawable/gradient"
tools:context=".Registration_form">
<ImageView
android:id="#+id/imageView2"
android:layout_width="240dp"
android:layout_height="298dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="-1dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="60dp"
app:srcCompat="#drawable/hotellogo" />
<EditText
android:id="#+id/editText1"
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_below="#id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_fn"
android:ems="10"
android:hint="Full name"
android:inputType="textPersonName"
android:textColor="#E1F7F7"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#807D7D"
android:textSize="16dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_below="#id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_user"
android:ems="10"
android:hint="Username (email)"
android:inputType="textEmailAddress"
android:textColor="#E1F7F7"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#807D7D"
android:textSize="16dp" />
<EditText
android:id="#+id/editText3"
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_below="#id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_pass"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textColor="#E1F7F7"
android:textColorHint="#807D7D" />
<EditText
android:id="#+id/editText4"
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_below="#id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_phone"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
android:textColor="#E1F7F7"
android:textColorHint="#807D7D" />
<EditText
android:id="#+id/editText5"
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_below="#id/editText4"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#11000000"
android:drawableLeft="#drawable/ic_action_room"
android:ems="10"
android:hint="Room number"
android:inputType="textPersonName"
android:textColor="#E1F7F7"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#807D7D"
android:textSize="16dp" />
</RelativeLayout>

Android is it possible to have a part of the layout scrollable

I am trying to make an android application and I need the middle part of my screen to be scrollable. When I set a ScrollView inside an Relative Layout my application crashes.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/backgroundcolor"
android:orientation="vertical">
<TextView
android:id="#+id/tvCijferHalen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView"
android:layout_alignParentEnd="true"
android:layout_alignStart="#+id/etWegingTehalenCijfer"
android:layout_alignTop="#+id/textView"
android:layout_marginEnd="26dp"
android:layout_marginStart="20dp"
android:background="?android:attr/colorPressedHighlight"
android:paddingStart="65dp"
android:paddingTop="15dp"
android:text="0"
android:textColor="?attr/editTextColor"
android:textSize="30dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/etWatStaan"
android:layout_alignEnd="#+id/etWatStaan"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="328dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:background="#android:color/holo_green_light"
android:paddingStart="65dp"
android:paddingTop="15dp"
android:text="0"
android:textColor="?attr/editTextColor"
android:textSize="30dp" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/tvCijferHalen"
android:layout_above="#+id/etWatStaan"
android:layout_alignStart="#+id/textView"
android:layout_alignEnd="#+id/tvCijferHalen">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:background="#color/backgroundcolor"
android:orientation="vertical">
<EditText
android:id="#+id/etcijfer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="185dp"
android:layout_marginTop="46dp"
android:ems="10"
android:hint="Cijfer 1"
android:inputType="numberDecimal"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="visible" />
<EditText
android:id="#+id/etweging1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/etcijfer1"
android:layout_alignBottom="#+id/etcijfer1"
android:layout_alignParentStart="true"
android:layout_marginEnd="28dp"
android:layout_marginRight="46dp"
android:layout_marginStart="187dp"
android:layout_toStartOf="#+id/bLess1"
android:ems="10"
android:hint="weging"
android:inputType="numberDecimal"
android:text="1"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="visible" />
<Button
android:id="#+id/bLess1"
android:layout_width="30dp"
android:layout_height="40dp"
android:background="#android:color/transparent"
android:drawableLeft="#android:drawable/ic_delete"
android:text="Button"
android:layout_alignTop="#+id/etweging1"
android:layout_alignParentEnd="true" />
<EditText
android:id="#+id/etcijfer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etcijfer1"
android:layout_alignParentStart="true"
android:layout_below="#+id/etcijfer1"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Cijfer 2"
android:inputType="numberDecimal"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="gone" />
<EditText
android:id="#+id/etweging2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etweging1"
android:layout_alignStart="#+id/etweging1"
android:layout_below="#+id/etweging1"
android:layout_marginTop="10dp"
android:layout_toStartOf="#+id/bLess2"
android:ems="10"
android:hint="weging"
android:inputType="numberDecimal"
android:text="1"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="gone" />
<Button
android:id="#+id/bLess2"
android:layout_width="30dp"
android:layout_height="40dp"
android:background="#android:color/transparent"
android:drawableLeft="#android:drawable/ic_delete"
android:text="Button"
android:layout_alignTop="#+id/etweging2"
android:layout_alignParentEnd="true"
android:visibility="gone"/>
<EditText
android:id="#+id/etcijfer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etcijfer1"
android:layout_alignParentStart="true"
android:layout_below="#+id/etcijfer2"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Cijfer 3"
android:inputType="numberDecimal"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="gone" />
<EditText
android:id="#+id/etweging3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etweging1"
android:layout_alignStart="#+id/etweging1"
android:layout_below="#+id/etweging2"
android:layout_marginTop="10dp"
android:layout_toStartOf="#+id/bLess2"
android:ems="10"
android:hint="weging"
android:inputType="numberDecimal"
android:text="1"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="gone" />
<Button
android:id="#+id/bLess3"
android:layout_width="30dp"
android:layout_height="40dp"
android:background="#android:color/transparent"
android:drawableLeft="#android:drawable/ic_delete"
android:text="Button"
android:layout_alignTop="#+id/etweging3"
android:layout_alignParentEnd="true"
android:visibility="gone"/>
<EditText
android:id="#+id/etcijfer4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etcijfer1"
android:layout_alignParentStart="true"
android:layout_below="#+id/etcijfer3"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Cijfer 4"
android:inputType="numberDecimal"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="gone" />
<EditText
android:id="#+id/etweging4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etweging1"
android:layout_alignStart="#+id/etweging1"
android:layout_below="#+id/etweging3"
android:layout_marginTop="10dp"
android:layout_toStartOf="#+id/bLess3"
android:ems="10"
android:hint="weging"
android:inputType="numberDecimal"
android:text="1"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="gone" />
<Button
android:id="#+id/bLess4"
android:layout_width="30dp"
android:layout_height="40dp"
android:background="#android:color/transparent"
android:drawableLeft="#android:drawable/ic_delete"
android:text="Button"
android:layout_alignTop="#+id/etweging4"
android:layout_alignParentEnd="true"
android:visibility="gone"/>
<EditText
android:id="#+id/etcijfer5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etcijfer1"
android:layout_alignParentStart="true"
android:layout_below="#+id/etcijfer4"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Cijfer 5"
android:inputType="numberDecimal"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="gone" />
<EditText
android:id="#+id/etweging5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etweging1"
android:layout_alignStart="#+id/etweging1"
android:layout_below="#+id/etweging4"
android:layout_marginTop="10dp"
android:layout_toStartOf="#+id/bLess4"
android:ems="10"
android:hint="weging"
android:inputType="numberDecimal"
android:text="1"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="gone" />
<Button
android:id="#+id/bLess5"
android:layout_width="30dp"
android:layout_height="40dp"
android:background="#android:color/transparent"
android:drawableLeft="#android:drawable/ic_delete"
android:text="Button"
android:layout_alignTop="#+id/etweging5"
android:layout_alignParentEnd="true"
android:visibility="gone"/>
<EditText
android:id="#+id/etcijfer6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etcijfer1"
android:layout_alignParentStart="true"
android:layout_below="#+id/etcijfer5"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Cijfer 6"
android:inputType="numberDecimal"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="gone" />
<EditText
android:id="#+id/etweging6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etweging1"
android:layout_alignStart="#+id/etweging1"
android:layout_below="#+id/etweging5"
android:layout_marginTop="10dp"
android:layout_toStartOf="#+id/bLess5"
android:ems="10"
android:hint="weging"
android:inputType="numberDecimal"
android:text="1"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="gone" />
<Button
android:id="#+id/bLess6"
android:layout_width="30dp"
android:layout_height="40dp"
android:background="#android:color/transparent"
android:drawableLeft="#android:drawable/ic_delete"
android:text="Button"
android:layout_alignTop="#+id/etweging6"
android:layout_alignParentEnd="true"
android:visibility="gone"/>
<EditText
android:id="#+id/etcijfer7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etcijfer1"
android:layout_alignParentStart="true"
android:layout_below="#+id/etcijfer6"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Cijfer 7"
android:inputType="numberDecimal"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="gone" />
<EditText
android:id="#+id/etweging7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etweging1"
android:layout_alignStart="#+id/etweging1"
android:layout_below="#+id/etweging6"
android:layout_marginTop="10dp"
android:layout_toStartOf="#+id/bLess6"
android:ems="10"
android:hint="weging"
android:inputType="numberDecimal"
android:text="1"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:visibility="gone" />
<Button
android:id="#+id/bLess7"
android:layout_width="30dp"
android:layout_height="40dp"
android:background="#android:color/transparent"
android:drawableLeft="#android:drawable/ic_delete"
android:text="Button"
android:layout_alignTop="#+id/etweging7"
android:layout_alignParentEnd="true"
android:visibility="gone"/>
</RelativeLayout>
</ScrollView>
<EditText
android:id="#+id/etWatStaan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/bNiewCijfer"
android:layout_alignEnd="#+id/etWegingTehalenCijfer"
android:layout_alignParentStart="true"
android:layout_marginEnd="200dp"
android:layout_marginRight="48dp"
android:ems="10"
android:hint="Wat wil je staan"
android:inputType="numberDecimal"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray" />
<EditText
android:id="#+id/etWegingTehalenCijfer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:ems="10"
android:hint="weging"
android:inputType="number"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:layout_alignBaseline="#+id/etWatStaan"
android:layout_alignBottom="#+id/etWatStaan"
android:layout_alignParentStart="true"
android:layout_marginStart="187dp" />
<Button
android:id="#+id/bNiewCijfer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="Cijfer Toevoegen"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView7"
android:layout_alignBottom="#+id/textView7"
android:layout_alignEnd="#+id/tvCijferHalen"
android:layout_alignStart="#+id/tvCijferHalen"
android:background="?android:attr/colorMultiSelectHighlight"
android:text="Te halen Cijfer"
android:textAlignment="center"
android:textColor="?attr/editTextColor" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/textView"
android:layout_marginTop="13dp"
android:background="#android:color/holo_green_dark"
android:text="Gemiddelde"
android:textAlignment="center"
android:textColor="?attr/editTextColor" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView9"
android:layout_alignBottom="#+id/textView9"
android:layout_alignEnd="#+id/ScrollView01"
android:layout_alignStart="#+id/tvCijferHalen"
android:layout_marginEnd="71dp"
android:text="Weging"
android:textAlignment="center"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/textView"
android:layout_alignStart="#+id/ScrollView01"
android:layout_alignTop="#+id/ScrollView01"
android:layout_marginTop="31dp"
android:text="Cijfer"
android:textAlignment="center"
android:textColor="#android:color/black" />
Can someone help me ? I am wondering if it's even possible to have only a part of the screen scrollable
Try this approach:
Step 1
Use a LinearLayout as your root element to enable you to use layout_weightattribute;
Step 2
Inside your layout, you can split it into 3 parts each with a layout_weight of .3
For example:
<LinearLayout
...
orientation="vertical"
... >
<LinearLayout
layout_weight=".3"
layout_width="match_parent"
layout_height="0dp" />
<ScrollView
layout_weight=".3"
layout_width="match_parent"
layout_height="0dp" />
<LinearLayout
layout_weight=".3"
layout_width="match_parent"
layout_height="0dp" />
</LinearLayout> !-- end of root layout here
I said .3 just in case you want to have the sections occupy the same size of the window; but you can set them to whatever you want according to your needs.
I hope this helps.
Note A ScrollView only accepts ONE child element!
Good luck!

text appears in limited width even after using wrap_content

I have set width of all the components to wrap_content in MainActivity but wrap_content does not works, I mean when I type into EditText then text appears only in limited width.
I want the TextViews and EditTexts to expand their widths according to input text length.
Please help, what is issue with my code?
MainActivity XML:
<?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: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="com.example.appdeveloper.appname.MainActivity"
android:background="#ff003b"
android:focusableInTouchMode="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/appLogoImage_id"
android:src="#drawable/applogo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="You Can&apos;t Proceed Without Inserting Primary Sim (SIM-1) In Your Device!"
android:id="#+id/regNoSim_id"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textAlignment="center"
android:textColor="#ffffff"
android:visibility="invisible"
android:layout_below="#+id/appLogoImage_id"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="REGISTRATION"
android:id="#+id/regTitle_id"
android:layout_below="#+id/appLogoImage_id"
android:textColor="#ffffff"
android:textAlignment="center"
android:layout_marginTop="50dp"
android:visibility="invisible"
android:textStyle="bold"
android:layout_centerHorizontal="true"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/loginPassword_id"
android:overScrollMode="never"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions|text"
android:ems="10"
android:id="#+id/regName_id"
android:hint="Your Name"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_marginTop="0dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/regPassword_id"
android:layout_below="#+id/regName_id"
android:hint="Application Password"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/regVerifyPass_id"
android:layout_below="#+id/regPassword_id"
android:hint="Verify Password"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/regRingCode_id"
android:hint="Ringing Codeword"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regVerifyPass_id"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/regLocationCode_id"
android:hint="Location Codeword"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regRingCode_id"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/regNum1_id"
android:hint="1st Trusted Number"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regLocationCode_id"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/regNum2_id"
android:hint="2nd Trusted Number"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regNum1_id"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/regNum3_id"
android:hint="3rd Trusted Number"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regNum2_id"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/regEmail_id"
android:hint="Email Address"
android:textAlignment="center"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:background="#android:color/transparent"
android:layout_below="#+id/regNum3_id"
android:layout_marginTop="10dp"
android:textCursorDrawable="#drawable/white_cursor"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="230dp"
android:layout_height="wrap_content"
android:text="Register"
android:id="#+id/regButton_id"
android:visibility="invisible"
android:layout_centerHorizontal="true"
android:layout_below="#+id/regEmail_id"
android:layout_marginTop="15dp"
android:layout_marginBottom="20dp" />
</RelativeLayout>
</ScrollView>
<EditText
android:layout_width="wrap_content"
android:layout_height="50dp"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/loginPassword_id"
android:textAlignment="center"
android:hint="Enter Application Password"
android:textColor="#ffffff"
android:textColorHint="#000000"
android:layout_below="#+id/appLogoImage_id"
android:layout_marginTop="50dp"
android:background="#android:color/transparent"
android:visibility="invisible"
android:textCursorDrawable="#drawable/white_cursor"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Forgotten Password?"
android:id="#+id/forgottenPass_id"
android:textAlignment="center"
android:textColor="#ffffff"
android:visibility="invisible"
android:layout_below="#+id/loginPassword_id"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:minHeight="30dp" />
</RelativeLayout>
Its because you have set android:ems="10" and that's the limitation remove it and see the difference!
You are thinking it's wrapped and adjust it's size because of your wrap_content but you have set android:ems !
Makes the TextView be exactly this many ems wide.
Refer : https://developer.android.com/reference/android/widget/TextView.html#attr_android:ems

Android SDK: RelativeLayout - Button WILL NOT center horizontally, even with Layout_CenterHorizontal=true

I really hope somebody can help me here because I am going to start pulling my hair out if this keeps doing what it's doing.
I'm trying to design a layout and I want all of my stuff to be centered. So far, everything is. However, the Save button I created will not center itself for the life of it. It consistently aligns its left edge with the center of the screen, rather than the center of the button itself.
Below is a screenshot of my simple design and the xml behind it... Any help will be GREATLY appreciated!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_settings"
tools:context="com.hypertom.weeklymealgenerator.SettingsActivity"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/strSettingsTitle"
android:id="#+id/textView"
android:textIsSelectable="false"
android:textSize="30sp"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/textView2" />
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/strSettingsNumMeals"
android:id="#+id/textView2"
android:layout_below="#+id/textView"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp"
android:layout_marginLeft="10dp"
android:gravity="right"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/strSettingsThresholdMin"
android:id="#+id/textView3"
android:layout_below="#+id/textView2"
android:layout_alignEnd="#+id/textView2"
android:layout_marginTop="10dp"
android:gravity="right"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/strSettingsThresholdMax"
android:id="#+id/textView4"
android:layout_below="#+id/textView3"
android:layout_alignEnd="#+id/textView3"
android:layout_marginTop="10dp"
android:gravity="right"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/strSettingsNumServings"
android:id="#+id/textView5"
android:layout_below="#+id/textView4"
android:layout_alignEnd="#+id/textView4"
android:layout_marginTop="10dp"
android:gravity="right"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/strSettingsNumSubstitutes"
android:id="#+id/textView6"
android:layout_below="#+id/textView5"
android:layout_alignEnd="#+id/textView5"
android:layout_marginTop="10dp"
android:gravity="right"
android:textSize="18sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnSettingsSave"
android:id="#+id/button4"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="200dp" />
<EditText
android:layout_width="65dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText"
android:layout_alignBottom="#+id/textView2"
android:layout_toEndOf="#+id/textView2"
android:layout_marginBottom="-12dp"
android:textSize="18sp"
android:gravity="center" />
<EditText
android:layout_width="65dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText2"
android:layout_alignBottom="#+id/textView3"
android:layout_alignStart="#+id/editText"
android:textSize="18sp"
android:layout_marginBottom="-12dp"
android:gravity="center" />
<EditText
android:layout_width="65dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText3"
android:layout_above="#+id/textView5"
android:layout_alignStart="#+id/editText2"
android:layout_marginBottom="-12dp"
android:textSize="18sp"
android:gravity="center" />
<EditText
android:layout_width="65dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText4"
android:layout_above="#+id/textView6"
android:layout_alignStart="#+id/editText3"
android:textSize="18sp"
android:layout_marginBottom="-12dp"
android:gravity="center" />
<EditText
android:layout_width="65dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText5"
android:layout_alignBottom="#+id/textView6"
android:layout_alignStart="#+id/editText4"
android:layout_marginBottom="-12dp"
android:textSize="18sp"
android:gravity="center" />
</RelativeLayout>
Something like this ? It can be done much easier in LinearLayout though.
<RelativeLayout 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">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="strSettingsTitle"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textIsSelectable="false"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:id="#+id/linearlayout">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="10dp"
android:layout_centerInParent="true">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="# of Meals"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#+id/textView2"
android:layout_marginTop="10dp"
android:gravity="right"
android:text="Threshold Min"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#+id/textView3"
android:layout_marginTop="10dp"
android:gravity="right"
android:text="Threshold Max"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#+id/textView4"
android:gravity="right"
android:layout_marginTop="10dp"
android:text="Num of Servings"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp" />
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:gravity="right"
android:layout_height="30dp"
android:layout_below="#+id/textView5"
android:layout_marginTop="10dp"
android:text="Num of Substitutes"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="10dp">
<EditText
android:id="#+id/editText"
android:layout_width="65dp"
android:layout_height="30dp"
android:ems="10"
android:inputType="number"
android:textSize="18sp" />
<EditText
android:id="#+id/editText2"
android:layout_width="65dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/editText"
android:ems="10"
android:inputType="number"
android:textSize="18sp" />
<EditText
android:id="#+id/editText3"
android:layout_width="65dp"
android:layout_height="30dp"
android:layout_below="#+id/editText2"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="number"
android:textSize="18sp" />
<EditText
android:id="#+id/editText4"
android:layout_width="65dp"
android:layout_height="30dp"
android:layout_below="#+id/editText3"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="number"
android:textSize="18sp" />
<EditText
android:id="#+id/editText5"
android:layout_width="65dp"
android:layout_height="30dp"
android:layout_below="#+id/editText4"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="number"
android:textSize="18sp" />
</RelativeLayout>
</LinearLayout>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerInParent="true"
android:layout_below="#+id/linearlayout"
android:text="Save" />
</RelativeLayout>

Different hint texSize in EditText fields

I have two EditText fields in my Activity. I set the textSize to "20dp" in both. However, one EditText shows the hint text with larger spacing in letters than another.
How do I solve this issue?
here is my XML code with the activity screen preview.
mainActivity.xml
<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=".MainActivity"
android:background="#3B5998"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/login"
android:background="#drawable/round_button"
android:height="90dp"
android:text="Login"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_marginTop="41dp"
android:layout_below="#+id/password"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="#+id/username"
android:layout_centerHorizontal="true"
android:background="#drawable/shape"
android:hint="Enter your username"
android:paddingLeft="5dp"
android:shadowRadius="5"
android:layout_marginTop="150dp"
android:textSize="20dp"
android:layout_marginBottom="0dp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="#+id/password"
android:background="#drawable/shape"
android:hint="Enter your password"
android:paddingLeft="5dp"
android:shadowRadius="5"
android:textSize="20dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:layout_below="#id/username"
android:inputType="textPassword"
/>
<TextView
android:layout_width="250dp"
android:layout_height="70dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="NxtStepz"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:gravity="center"
android:textSize="50dp"
android:textColor="#FFFFFF"
android:textStyle="italic|bold"
/>
Here is the link for the screenshot
You should never use the dp with text sizes. Always use sp. Try to replace your current EditTexts with those
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/username"
android:layout_centerHorizontal="true"
android:background="#drawable/shape"
android:hint="Enter your username"
android:paddingLeft="5dp"
android:shadowRadius="5"
android:textSize="20sp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:inputType="textPassword"
android:layout_marginTop="150dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/password"
android:layout_centerHorizontal="true"
android:background="#drawable/shape"
android:hint="Enter your password"
android:paddingLeft="5dp"
android:shadowRadius="5"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:layout_below="#id/username"
android:inputType="textPassword"
/>

Categories

Resources