1

Given the following code:

<RelativeLayout
    android:id="@+id/rel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textPersonalInfoEmpty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="20dp"
        android:hint="Edit to set your personal info"
        android:textSize="16sp"
        android:visibility="gone" />

    <EditText
        android:id="@+id/editName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="15dp"
        android:hint="Name"
        android:visibility="gone"
        android:textSize="16sp" />

    <Button
        android:id="@+id/insertGameId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editName"
        android:layout_marginTop="20dp"
        android:text="Insert" />
</RelativeLayout>

The TextView and the EditText are visible depending the user interaction and both will never be visible at the same time. One per time only!

The problem is the third component(Button) need to change the attribute android:layout_below depending on which component is visible.

I need to this programmatically.

Any help?

Emerick
  • 309
  • 1
  • 10

2 Answers2

1

Use Relativelayout for your textview and edittext. And relativelayout's height:wrap-content Then give an id to your relativelayout

and then add this line your xml file for your button

<Button

        android:layout_below="@id/RealtiveLayoutid" />

Thats all

MehmetF
  • 61
  • 1
  • 14
0

First, use a ToggleButton instead of Button. It can have all the attributes that you assigned to it.

Next add this to the ToggleButton:

<ToggleButton
   ....
   android:textOn="@string/ifTextView"
   android:textOff="@string/ifTextBox"
   android:onClick="onToggleClicked"
/>

Then this for the activity

public void onToggleClicked(View v) {
    boolean on = ((ToggleButton) v).isChecked();

    if(on) {
        theTextView.setVisibility(View.VISIBLE);
        theTextBox.setVisibility(View.INVISIBLE);
    } else {
        theTextBox.setVisibility(View.VISIBLE);
        theTextView.setVisibility(View.INVISIBLE);
    }
}

Remember to import the required classes.

alesc3
  • 84
  • 11