Textview alignment issue in Relativelayout - android

When I have this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:gravity="center_horizontal|top"
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="nl.dylaan.deroosterapp.MainActivity" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/app_version"
android:textColor="#android:color/white"
android:textSize="17sp" />
</RelativeLayout>
The text is in the center:
But when I add a new text to it:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:gravity="center_horizontal|top"
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="nl.dylaan.deroosterapp.MainActivity" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/app_version"
android:textColor="#android:color/white"
android:textSize="17sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_marginTop="33dp"
android:text="#string/app_name_cap"
android:textColor="#android:color/white"
android:textSize="35sp" />
</RelativeLayout>
The new text is now in the center but the textView2 is not? I need to have it both in center:
Does anyone knows why this is not working?

Remove android:gravity="center_horizontal|top" from your RelativeLayout, it's messing up with the rules.
And to align center all View's use android:layout_centerHorizontal="true" on both View's:
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/app_version"
android:textColor="#android:color/white"
android:textSize="17sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_marginTop="33dp"
android:text="#string/app_name_cap"
android:layout_centerHorizontal="true"
android:textColor="#android:color/white"
android:textSize="35sp" />

Try my code for textview2
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="#string/app_version"
android:textColor="#android:color/white"
android:textSize="17sp" />
It doesn't work because of the center horizontal layout

Related

android: position a textview below another textview

How to set texView2 below textView1?
I want to add second text view below first text view.
I tried this Layout Code below:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UserID"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:text="User name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="16sp"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:background="#FFFFFF"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Add Friend"
android:textColor="#0099CC" />
</LinearLayout>
any help would be appreciated...
thank you...
use Relative Layout rather than Linear layout Or Set Linear Layout Orientation Vertical.
Using Relative Layout your Code Should like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UserID"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:text="User name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="16sp"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:background="#FFFFFF"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Add Friend"
android:textColor="#0099CC" />
</RelativeLayout>
Or use LinearLayout Set Orientation To vertical if you want all Item Below one another than: your Code should be.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="8dp"
android:paddingBottom="8dp"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UserID"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="16sp"
/>
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_below="#+id/linearLayout"
android:background="#FFFFFF"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Add Friend"
android:textColor="#0099CC" />
</RelativeLayout>
read this artical for linear layout and relative layout
LinearLayout
RelativeLayout
Just change the orientation of linearLayout to vertical and its go
<?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="match_parent"
android:orientation="vertical"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UserID"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="16sp"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:background="#FFFFFF"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Add Friend"
android:textColor="#0099CC" />

EditText moves TextView out of the screen when they keyboard is opening

I have an EditText on the bottom of my layout und a few TextViews on the top. When I click on the EditText it moves from the bottom above the keyboard which is good. But the TextViews also move up and out of the screen so you can't see them until you close the keyboard again. How can I avoid this but still have my EditText above the keyboard? I'm using a RelativeLayout.
Thanks!
<?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:id="#+id/activity_main"
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.ncss.tyfby.Feeling"
android:background="#1baa84">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:text="I am"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/IamTV"
android:layout_weight="0.10"
android:textSize="40sp"
android:textStyle="normal|bold"
android:gravity="bottom" />
<EditText
android:layout_width="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/iamAdjective"
android:background="#android:drawable/editbox_background_normal"
android:hint="powerful"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="10dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/send"
android:layout_weight="0.10"
android:src="#android:drawable/ic_menu_send"
android:background="#drawable/transparent"
android:onClick="send"
android:layout_marginLeft="5dp" />
</LinearLayout>
<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iam1"
android:textSize="18sp" />
<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iam2"
android:textSize="18sp"
android:layout_below="#+id/iam1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iam4"
android:textSize="18sp"
android:layout_below="#+id/iam3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iam5"
android:textSize="18sp"
android:layout_below="#+id/iam4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iam3"
android:textSize="18sp"
android:layout_below="#+id/iam2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
You may explore windowSoftInputMode that you can configure for your Activity in the AndroidManifest.xml file: activity | Android Developers.
Perhaps windowSoftInputMode="adjustResize" might be useful for you.
In addition to that you will need to change your layout XML to something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context="com.ncss.tyfby.Feeling"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="#1baa84">
<ScrollView
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:orientation="vertical">
<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iam1"
android:textSize="18sp" />
<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iam2"
android:textSize="18sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iam4"
android:textSize="18sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iam5"
android:textSize="18sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:text="I am"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/iam3"
android:textSize="18sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:text="I am"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/IamTV"
android:layout_weight="0.10"
android:textSize="40sp"
android:textStyle="normal|bold"
android:gravity="bottom" />
<EditText
android:layout_width="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/iamAdjective"
android:background="#android:drawable/editbox_background_normal"
android:hint="powerful"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="10dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/send"
android:layout_weight="0.10"
android:src="#android:drawable/ic_menu_send"
android:background="#drawable/transparent"
android:onClick="send"
android:layout_marginLeft="5dp" />
</LinearLayout>
</LinearLayout>
You should put your entire layout inside a ScrollView and force that to be above of my_edit_text:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/my_edit_text">

How Can I Do Text Box(left side)

Look the left side, how can I do it? It's only text; user can't click it because it's for info.
you need to set:
android:layout_alignParentLeft="true"
to what ever you want to align to the left
like i did in my example that looks a bit like yours, although i couldn't clearly understand what you wanted.
<?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="apps.radwin.testprojectone.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Thats Only for text.(notClickable)"
android:textColor="#f00000"
android:layout_alignParentLeft="true"
android:id="#+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="-"
android:id="#+id/textView2"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="-"
android:id="#+id/textView3"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="-"
android:id="#+id/textView4"
android:layout_below="#+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView5"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true" />
</RelativeLayout>
the result of this would be an image like that:
You can create layout like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:text="That's pnly for text(not clickable)"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginLeft="10dp"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Info"/>
<LinearLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:text="Hello World"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

Relative layout in android

I am a newbie in android who has started android just today.I am facing some problem in Layouts.
I want the layout to be as follows.
1(TextView) 2(TextView)
3(EditView) 4(EditView)
Here is what I have tried.The Top 1 & 2 represents TextView and 3
&4 represents EditView.Can we achieve this only with relative layout.
<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"
android:background="#drawable/amicare"
tools:context="com.example.ambulancetrack.MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/reg_no"
android:id="#+id/tvRegNo"
android:textColor="#21F6D0"
android:layout_toLeftOf="#+id/tvPhoneNo"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RAHAT Phone No."
android:textColor="#21F6D0"
android:layout_alignParentRight="true"
android:id="#+id/tvPhoneNo"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvRegNo"
android:background="#1E4F56"
android:textColor="#FFFFFF"
android:id="#+id/evRegNo"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/evRegNo"
android:background="#1E4F56"
android:textColor="#FFFFFF"
android:id="#+id/evPhoneNo"
/>
</RelativeLayout>
Please Help!!!Thanks in Advance
Ckeck this variant (in your case):
UPDATE:
<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"
android:background="#drawable/amicare"
tools:context="com.example.ambulancetrack.MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/reg_no"
android:id="#+id/tvRegNo"
android:textColor="#21F6D0"
android:layout_toLeftOf="#+id/tvPhoneNo"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RAHAT Phone No."
android:textColor="#21F6D0"
android:layout_alignParentRight="true"
android:id="#+id/tvPhoneNo"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvRegNo"
android:background="#1E4F56"
android:textColor="#FFFFFF"
android:id="#+id/evRegNo"
android:layout_toLeftOf="#+id/evPhoneNo"
android:layout_toStartOf="#+id/evPhoneNo" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvPhoneNo"
android:layout_below="#+id/tvPhoneNo"
android:background="#1E4F56"
android:textColor="#FFFFFF"
android:id="#+id/evPhoneNo"
/>
Here is your code.
<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.ambulancetrack.MainActivity">
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvRegNo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/tvPhoneNo"
android:layout_weight="1"
android:text="reg_no"
android:textColor="#21F6D0" />
<TextView
android:id="#+id/tvPhoneNo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:text="RAHAT Phone No."
android:textColor="#21F6D0"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout1"
android:orientation="horizontal">
<EditText
android:id="#+id/evRegNo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/tvRegNo"
android:layout_weight="1"
android:background="#1E4F56"
android:textColor="#FFFFFF" />
<EditText
android:id="#+id/evPhoneNo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/evRegNo"
android:layout_weight="1"
android:background="#1E4F56"
android:textColor="#FFFFFF" />
</LinearLayout>
Test it and accept the answer if it is useful.
try this way
<RelativeLayout
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="abce"
android:id="#+id/tvRegNo"
android:layout_toLeftOf="#+id/tvPhoneNo"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RAHAT Phone No."
android:layout_alignParentRight="true"
android:id="#+id/tvPhoneNo"
/>
<EditText
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_below="#+id/tvRegNo"
android:hint="avc"
android:layout_alignParentLeft="true"
android:textColor="#FFFFFF"
android:id="#+id/evRegNo"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="avcd"
android:layout_toRightOf="#+id/evRegNo"
android:layout_alignParentRight="true"
android:layout_below="#+id/tvPhoneNo"
android:textColor="#FFFFFF"
android:id="#+id/evPhoneNo"
/>
</RelativeLayout>
Change main layout type to Linear with Vertical orientation. Then add 2 more Linear layout with Horizontal orientation inside and put your Textviews there. Use Weight attribute to control the size of each layout. Like that:
<?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="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"></LinearLayout>
</LinearLayout>
Add one more layout to you main Vertical layout and set it weight to 10, so it will take most space and your textviews will be close to each other. Dont forget to set android:layout_height="0dp"of your inner layouts to make weight work right.
You can archive this using LinearLayout also using android:layout_weight, like:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Text View 1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Text View 2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Text View 3" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Text View 4" />
</LinearLayout>
</LinearLayout>
Your thinking is right,we can use one RelativeLayout to achieve your goal,but your last EditText's attrs maybe wrong.Please try this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/amicare"
tools:context="com.example.ambulancetrack.MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/reg_no"
android:id="#+id/tvRegNo"
android:textColor="#21F6D0"
android:layout_toLeftOf="#+id/tvPhoneNo"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RAHAT Phone No."
android:textColor="#21F6D0"
android:layout_alignParentRight="true"
android:id="#+id/tvPhoneNo"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvRegNo"
android:background="#1E4F56"
android:textColor="#FFFFFF"
android:id="#+id/evRegNo"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvPhoneNo"
android:layout_below="#+id/tvPhoneNo"
android:background="#1E4F56"
android:textColor="#FFFFFF"
android:id="#+id/evPhoneNo"
/>
</RelativeLayout>
Just add
android:layout_alignLeft="#+id/tvPhoneNo"
android:layout_below="#+id/tvPhoneNo"
to make sure evPhoneNo layouts below of tvPhoneNo and align left of tvPhoneNo.
This should do the work. You can change text color and other attributes as you want.but this is the basic structure of your question
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="137dp"
android:text="EditView"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:layout_alignTop="#+id/editText"
android:text="EditView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="TextView"
android:id="#+id/textView1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="TextView"
android:id="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>

TextView is not visible

Please have a look at the following code
<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=".Game" >
<TextView
android:id="#+id/numberOfQuestionsLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:text="TextView" />
<TextView
android:id="#+id/numberOfCorrectAnswers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/numberOfQuestionsLeft"
android:layout_alignBottom="#+id/numberOfQuestionsLeft"
android:layout_marginLeft="34dp"
android:layout_toRightOf="#+id/numberOfQuestionsLeft"
android:text="TextView" />
<LinearLayout
android:id="#+id/linearOne"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#49494f"
android:layout_below="#+id/numberOfCorrectAnswers"
android:paddingTop="20dp"
>
<TextView
android:id="#+id/hint"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="30dp"
android:text="Medium Text"
android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>
This gives the following
As you can see, the hint textview which should appear inside the Linear Layout is not visible. Why is this? Please
Remove android:layout_marginBottom="30dp" from your hint text view :
<LinearLayout
android:id="#+id/linearOne"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#49494f"
android:layout_below="#+id/numberOfCorrectAnswers"
android:paddingTop="20dp" >
<TextView
android:id="#+id/hint"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Medium Text"
android:textColor="#ffffff" />
</LinearLayout >
Actually you dont need that Linear Layout, just do like this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android: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=".Game" >
<TextView
android:id="#+id/numberOfQuestionsLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:text="TextView" />
<TextView
android:id="#+id/numberOfCorrectAnswers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/numberOfQuestionsLeft"
android:layout_alignBottom="#+id/numberOfQuestionsLeft"
android:layout_marginLeft="34dp"
android:layout_toRightOf="#+id/numberOfQuestionsLeft"
android:text="TextView" />
<TextView
android:id="#+id/hint"
android:background="#49494f"
android:paddingTop="20dp"
android:layout_below="#+id/numberOfCorrectAnswers"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="30dp"
android:text="Medium Text"
android:textColor="#ffffff" />
</RelativeLayout >
You need to do this:
<LinearLayout
android:id="#+id/linearOne"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#49494f"
android:layout_below="#+id/numberOfCorrectAnswers"
android:paddingTop="20dp"
>
<TextView
android:id="#+id/hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:text="MediumText"
android:textColor="#ffffff" />
</LinearLayout>

Categories

Resources