Nida Iqbal
Nida Iqbal

Reputation: 41

Improper placing of relativelayout fields

Only textView1 is appearing on screen, button and edittext box is not appearing. Please check my code. I am new to android development. I believe that the placement of edittext and button is incorrect.

<?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:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textView1" />

    <EditText
        android:id="@+id/edit_message"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="@string/edit_message"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_message"
        android:layout_marginTop="49dp"
        android:layout_toRightOf="@+id/edit_message"
        android:onClick="sendMessage"
        android:text="@string/button_send" />


</RelativeLayout>

Upvotes: 3

Views: 118

Answers (3)

codeMagic
codeMagic

Reputation: 44571

RelativeLayout doesn't have an orientation property...remove that. Also, I think you want to remove the marginTop properties if putting them below other Views. You may want to use padding instead. I'm not sure if you can be to the right of and below the same view but that may work. That's all I see at the moment

Note

fill_parent is depricated. It's ok to use for now but you might as well get accustomed to using match_parent instead

I got your code working with the suggestions I gave. See below. Note with a height of 10dp you may not see it because that isn't very big

<?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" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="textView1" />

<EditText
    android:id="@+id/edit_message"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:ems="10"
    android:hint="edit_message"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/edit_message"
    android:onClick="sendMessage"
    android:text="button_send" />
</RelativeLayout>

Upvotes: 1

Nida Iqbal
Nida Iqbal

Reputation: 41

Correct code. just remove

android:layout_alignParentLeft="true"
 android:layout_marginTop="10dp"

from Edittext and use padding instead of marginTop

<?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:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="0dp"
    android:background="#000000"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textView1"
        android:textColor="#C8C8C8"
        android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/edit_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+string/textView1"
            android:hint="@string/edit_message"
           />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/edit_message"
            android:layout_below="@+id/edit_message"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:onClick="sendMessage"
            android:text="@string/button_send" 
            android:textColor="#FFFFFF"
            />

</RelativeLayout>

Upvotes: 0

TronicZomB
TronicZomB

Reputation: 8747

Everything appears to be good, except why don't you try anchoring your TextView somewhere, say the parent top left:

android:layout_alignParentTop="true"
android:layout_alignPatentLeft="true"

Also, you may want to research this more, but I feel that it is highly, highly discouraged to use the xml onClick and instead register the onClickListener in the java code.

Upvotes: 0

Related Questions