0

I want to replace the content of the linear layout which has button and textview with an editText type="phone" after an onclick event of button. They're all in the located in the same page.

Is there a way to do that?

woninana
  • 3,409
  • 9
  • 42
  • 66
  • LinearLayout has methods to clear all elements from it, after which you should be able to add new views programmatically. – G_V Jan 28 '15 at 12:45
  • what i understand from your question, just play with "editable" property of EditText and you get what you want. follow this link : http://stackoverflow.com/questions/660151/how-to-replicate-androideditable-false-in-code Note: no need of another textview, one edit text work here perfectly. – Manish Jan 29 '15 at 09:12

4 Answers4

1

Use following code to remove all Views.

lauout.removeAllViews();
SANJAY GUPTA
  • 1,564
  • 13
  • 21
0

You'll have to make two separate fragments and use a fragmenttransaction to replace one with the other.

See fragmenttransactions: http://developer.android.com/reference/android/app/FragmentTransaction.html

vipluv
  • 607
  • 5
  • 8
0

What I can understand from your question

1) There are 3 views Button, TextView and EditText under one LinearLayout

Like this:

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Button" />

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

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone" // with input type phone
        android:visibility="gone" />

</LinearLayout>

And You want to remove the textview and replace it with Edit Text on Button Click like this

tv=(TextView)findViewById(R.id.textView1);
et=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
    b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                tv.setVisibility(View.GONE);
                et.setVisibility(View.VISIBLE);

            }
        });
A.R.
  • 2,631
  • 1
  • 19
  • 22
0

place all of your three elements in the layout in your xml_layout, and set the EditText visibility to "gone", then when clicking the Button you mentioned, just set the Button and the TextView visibility to "gone" and the EditText visibility to "visible":

<LinearLayout android:id="@+id/layout"
    .
    .
    .>

    <Button android:id="@+id/button"
        .
        .
        .
        android:visibility="visible"/>

    <TextView android:id="@+id/text"
        .
        .
        .
        android:visibility="visible"/>

    <EditText android:id="@+id/edit_text"
        .
        .
        .
        android:inputType="phone"
        android:visibility="gone"/>

</LinearLayout>

when clicking the Button:

public OnClickListener onButClick = new OnClickListener() {
    @Override
    public void onClick(View v) {

        button.setVisibility(View.GONE);
        text.setVisibility(View.GONE);  
        text_view.setVisibility(View.VISIBLE);     

    }
};
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118