Gabriel
Gabriel

Reputation: 97

Button not displayed on emulator or on Device

I am a beginner on Android. I have created 2 activities. First activity has 1 button (say Button1) and 1 TextView (say TextView1) and the Second activity has 1 TextView (say TextView2). The text value for TextView in 1st activity is "Hello World!", when the Button1 is pressed opens up 2nd Activity passing the value of the TextView1 (i.e. "Hell World!"). The 2nd Activity assigns the obtained value to the TextView2 and displays.

All of this is working fine.

When I add a new Button (say Button2) to the 1st activity and write a onClick() method to change the text of TextView1 from "Hello World!" to "Hello Android!" and vice versa. Now when I run this on my device or emulator I cannot see this added button (Button2) it still works as earlier when I click Button1, but I cannot see Button2.

Can anyone help me on this ?

XML Layout : MainActivity

<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=".MainActivity" >

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/ourButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView"
    android:layout_below="@+id/textView"
    android:layout_marginRight="37dp"
    android:onClick="buttonClick"
    android:text="@string/button_string" />

<Button
    android:id="@+id/ourButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView"
    android:layout_below="@+id/textView"
    android:layout_marginLeft="50dp"
    android:focusable="false"
    android:onClick="buttonClick2"
    android:text="@string/button_string2" />

</RelativeLayout>

Upvotes: 0

Views: 3941

Answers (4)

user3011821
user3011821

Reputation:

      <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=".MainActivity" >

    <TextView
         android:id="@+id/textView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true"
         android:text="@string/hello_world" />

    <Button
         android:id="@+id/ourButton"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignRight="@+id/textView"
         android:layout_below="@+id/textView"
         android:layout_marginRight="37dp"
         android:onClick="buttonClick"
         android:text="@string/button_string" />

    <Button
         android:id="@+id/ourButton2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/ourButton"
         android:layout_marginLeft="50dp"
         android:focusable="false"
         android:onClick="buttonClick2"
         android:text="@string/button_string2" />

  </RelativeLayout>

Upvotes: 1

Aneez
Aneez

Reputation: 205

Please try to remove

    android:layout_marginRight="37dp"

and then try. Are you able to see output on xml graphical layout ? When I tried this on my xml I am able to see in Graphical Layout(I used the same code which you mentioned along with your question).

Upvotes: 1

Blo
Blo

Reputation: 11978

It's because you have android:layout_below="@+id/textView" twice. Try this instead:

<!-- below the previous view : ourButton -->
<Button
android:id="@+id/ourButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/textView"
android:layout_below="@id/ourButton"
android:layout_marginLeft="50dp"
android:focusable="false"
android:onClick="buttonClick2"
android:text="@string/button_string2" />

Also, don't use @+id/ for align your layout. You use it right when you add an id but when the id is created, you just have to align on it as android:layout_toLeftOf="@id/prevId" with @id.


Example:

Here the layout:

                         |---- text -----|
                                           <- both are below text
                         |---- view1 ----|| <- view align right
     view align left -> ||---- view2 ----| 

As you can see, the left and right alignment are on the "border" of the TextView because you set an alignment below the TextView. So it looks like this:

                       |------ text ------|
                                           <- both are below text
  view1 align left -> ||---- view 1&2 ----|| <- view2 align right  

Your views are overlapping each other. If you want to have the two buttons below the text and side by side, try to add a container, like this following:

<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:gravity="center_horizontal|center_vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/ourButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="50dp"
            android:onClick="buttonClick"
            android:text="@string/button_string" />

        <Button
            android:id="@+id/ourButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="buttonClick2"
            android:text="@string/button_string2" />

    </LinearLayout>

</RelativeLayout>

Hope this helps.

Upvotes: 2

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

// try this way
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    android:padding="5dp"
    tools:context=".MainActivity" >

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">
    <Button
        android:id="@+id/ourButton"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:onClick="buttonClick"
        android:layout_marginRight="5dp"
        android:text="@string/button_string" />

    <Button
        android:id="@+id/ourButton2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:onClick="buttonClick2"
        android:text="@string/button_string2" />
    </LinearLayout>
</LinearLayout>

Upvotes: 0

Related Questions