text appears in limited width even after using wrap_content - android

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

Related

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!

some of the components goes out of screen

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>

Centering text within editText Android

I have a linearlayout with a textview, and edittext. How do I center the text within edittext. It appears to be drawn to the bottom as shown below.I want it to be consistent with the baseline similar to that of the textview to its left.
The layout code is as below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_marginTop="20dp"
android:orientation="horizontal"
>
<TextView
android:layout_width="75dp"
android:layout_height="30dp"
android:ems="10"
android:textAlignment="center"
android:text="NAME"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:singleLine="true"
android:id="#+id/tv_name"
android:background="#drawable/orange_left_rounded_corner"
/>
<EditText
android:layout_width="125dp"
android:layout_height="30dp"
android:ems="10"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:text=""
android:layout_gravity="right"
android:gravity="center_horizontal"
android:textColor="#000000"
android:textSize="16sp"
android:singleLine="true"
android:id="#+id/et_name"
android:background="#drawable/orange_right_rounded_corner"
/>
</LinearLayout>
Use android:gravity="center" to have the input be centered
Just do that
<LinearLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_marginTop="20dp"
android:orientation="horizontal"
>
<TextView
android:layout_width="75dp"
android:layout_height="30dp"
android:ems="10"
android:textAlignment="center"
android:text="NAME"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:singleLine="true"
android:id="#+id/tv_name"
android:background="#drawable/orange_left_rounded_corner"
android:gravity="center_vertical"
/>
<EditText
android:layout_width="125dp"
android:layout_height="30dp"
android:ems="10"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:text=""
android:layout_gravity="right"
android:gravity="center_horizontal"
android:textColor="#000000"
android:textSize="16sp"
android:singleLine="true"
android:id="#+id/et_name"
android:background="#drawable/orange_right_rounded_corner"
/>
Try with this code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_name"
android:layout_width="75dp"
android:layout_height="30dp"
android:background="#drawable/orange_left_rounded_corner"
android:ems="10"
android:gravity="center_vertical"
android:singleLine="true"
android:text="NAME"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<EditText
android:id="#+id/et_name"
android:layout_width="125dp"
android:layout_height="30dp"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:background="#drawable/orange_right_rounded_corner"
android:ems="10"
android:gravity="center_horizontal"
android:singleLine="true"
android:text=""
android:textAlignment="center"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>

Scrollbar not displaying on opening softkeyboard

My fragment consists of ScrollView.Initially the content of fragment is able to fit in screen so there is no need of scrollbar.But when i open softkeyboard half the content is hidden behind keyboard so i want that scrollbar should be displayed.But it is not displaying.I tried putting below line in manifest but same result-
android:windowSoftInputMode="stateVisible|adjustResize"
Code-
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/post_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/rel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
<EditText
android:id="#+id/ns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:hint="News Source Link*"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:background="#drawable/edittextbordergrey"
android:text=""
android:textColor="#color/cwTextPrimaryColor"
android:textColorHint="#color/actiontext"
android:textSize="14sp"
android:textCursorDrawable="#null"
android:singleLine="true" />
<TextView
android:id="#+id/autofill_text_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ns"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:singleLine="true"
android:text="Auto fill"
android:textSize="14sp"
android:textColor="#color/username" />
<EditText
android:id="#+id/headline_edit_text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="left"
android:hint="Headline that explains the real story*"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:textCursorDrawable="#null"
android:background="#drawable/edittextbordergrey"
android:text=""
android:layout_below="#+id/autofill_text_count"
android:textColor="#color/cwTextPrimaryColor"
android:textColorHint="#color/actiontext"
android:textSize="14sp"
android:maxLines="4"
android:scrollHorizontally="false"
android:maxLength="99" />
<TextView
android:id="#+id/headline_text_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/headline_edit_text"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:singleLine="true"
android:text="99 characters"
android:textSize="14sp"
android:textColor="#color/cwAccentColor" />
<RegularEditText
android:id="#+id/source_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:hint="Source*"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:background="#drawable/edittextbordergrey"
android:text=""
android:textCursorDrawable="#null"
android:textColor="#color/cwTextPrimaryColor"
android:layout_below="#+id/headline_text_count"
android:textColorHint="#color/actiontext"
android:textSize="14sp"
android:singleLine="true"
android:maxLength="25" />
<TextView
android:id="#+id/source_text_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/source_edit_text"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:singleLine="true"
android:text="25 characters"
android:textSize="14sp"
android:textColor="#color/cwAccentColor" />
<EditText
android:id="#+id/news_image_link_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edittextbordergrey"
android:gravity="left"
android:hint="Link to picture or upload below"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:layout_below="#+id/source_text_count"
android:text=""
android:textColor="#color/cwTextPrimaryColor"
android:textColorHint="#color/actiontext"
android:textSize="14sp"
android:textCursorDrawable="#null"
android:singleLine="true"
android:visibility="visible" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/top10s_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
<RelativeLayout
android:id="#+id/top10s_headline_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edittextbordergrey"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp">
<TextView
android:id="#+id/top10s_headline_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:singleLine="true"
android:text="Top 10 "
android:textSize="15sp"
android:textColor="#color/cwTextPrimaryColor" />
<EditText
android:id="#+id/top10s_headline_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingBottom="15dp"
android:paddingLeft="60dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textCursorDrawable="#null"
android:background="#android:color/transparent"
android:text=""
android:textColor="#color/cwTextPrimaryColor"
android:textColorHint="#color/actiontext"
android:textSize="14sp"
android:maxLength="93" />
</RelativeLayout>
<TextView
android:id="#+id/top10s_headline_text_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/top10s_headline_layout"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:singleLine="true"
android:text="93 characters"
android:textSize="14sp"
android:textColor="#color/cwAccentColor" />
<TextView
android:id="#+id/infoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/top10s_image_link_edit_text"
android:layout_marginTop="8dp"
android:gravity="center"
android:textSize="16sp"
android:visibility="gone"
android:maxLines="2"
android:text="Be sure to add an image before hitting "Post""/>
<EditText
android:id="#+id/top10s_image_link_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:hint="Link to picture or upload below*"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:background="#drawable/edittextbordergrey"
android:text=""
android:textCursorDrawable="#null"
android:textColor="#color/cwTextPrimaryColor"
android:layout_below="#+id/top10s_headline_text_count"
android:textColorHint="#color/actiontext"
android:textSize="14sp"
android:singleLine="true"
android:visibility="visible" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/openhumor_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
<EditText
android:id="#+id/openhumor_funny_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:background="#drawable/edittextbordergrey"
android:singleLine="true"
android:textCursorDrawable="#null"
android:text=""
android:textColor="#color/cwTextPrimaryColor"
android:textColorHint="#color/actiontext"
android:textSize="14sp"
android:maxLength="93" />
<TextView
android:id="#+id/openhumor_funny_edit_text_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/openhumor_funny_edit_text"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:singleLine="true"
android:text="93 characters"
android:textSize="14sp"
android:textColor="#color/cwAccentColor" />
<EditText
android:id="#+id/openhumor_image_link_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:hint="Link to picture or upload below*"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:background="#drawable/edittextbordergrey"
android:text=""
android:textCursorDrawable="#null"
android:textColor="#color/cwTextPrimaryColor"
android:layout_below="#+id/openhumor_funny_edit_text_count"
android:textColorHint="#color/actiontext"
android:textSize="14sp"
android:singleLine="true"
android:visibility="visible" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/post_layout"
android:layout_marginTop="15dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<ImageView
android:id="#+id/upload_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/camera_icon"
android:layout_marginRight="10dp"
android:padding="7dp"
android:layout_toLeftOf="#+id/post_button"
android:visibility="gone" />
<TextView
android:id="#+id/upload_image_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/upload_image"
android:singleLine="true"
android:text=""
android:visibility="gone"
android:textColor="#color/white" />
<TextButton
android:id="#+id/post_button"
android:layout_width="65dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:background="#drawable/cancel_button_bg"
android:text="Post"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="14sp"
android:visibility="gone" />
</RelativeLayout>
<ImageView
android:id="#+id/preview_imageview"
android:layout_width="140dp"
android:layout_height="90dp"
android:layout_below="#+id/post_layout"
android:layout_marginTop="15dp"
android:layout_marginLeft="5dp"
android:scaleType="centerCrop"
android:layout_marginRight="5dp" />
<ImageView
android:id="#+id/preview_imageview_delete"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="#+id/post_layout"
android:layout_alignRight="#+id/preview_imageview"
android:layout_alignTop="#+id/preview_imageview"
android:scaleType="centerCrop"
android:layout_marginRight="-10dp"
android:layout_marginTop="-10dp"
android:src="#drawable/preview_image_delete"
android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>
<View
android:id="#+id/empty_action"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:alpha="0.7"
android:visibility="gone" />
<ProgressBar
android:id="#+id/progress"
android:layout_width="#dimen/loading_view_progress_size"
android:layout_height="#dimen/loading_view_progress_size"
android:layout_marginRight="#dimen/loading_view_margin_right"
style="?android:progressBarStyle"
android:indeterminate="true"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
</ScrollView>

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>

Categories

Resources