Nikhil
Nikhil

Reputation: 167

Replace EditText with android.support.v7.widget.CardView tag

I have an EditText in my layout file. Once user clicks on this field, I want it to get replaced with the below element tag. How do I do it ?

From:

<EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:text="Name"
            android:ems="10"
            android:layout_alignParentTop="true"
            android:layout_marginTop="26dp" />

To:

<android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <fragment
                android:id="@+id/autocomplete_fragment"
                android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    </android.support.v7.widget.CardView>

Upvotes: 0

Views: 613

Answers (4)

Sanjit Singh
Sanjit Singh

Reputation: 276

Give id to both views and initially hide CardView by setting visibility to GONE

<EditText
        android:id="@+id/edittext"        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_marginTop="26dp" />

<android.support.v7.widget.CardView
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone">

        <fragment
            android:id="@+id/autocomplete_fragment"
            android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
      </android.support.v7.widget.CardView>

In your java class change the visibility of cardview and edittext when user click

mEditText.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
      mEditText.setVisibility(View.GONE);
      mCardView.setVisibility(View.VISIBLE);
    }
});

Upvotes: 1

Deepesh
Deepesh

Reputation: 533

Implement edittext's OnClickListener and inside onClick() method hide your edittext and unhide cardview by calling method edittext.setVisibility(View.INVISIBLE) and cardview.setVisibility(View.VISIBLE)

Upvotes: 0

KlevisGjN
KlevisGjN

Reputation: 691

You should add them both in your layout and by default set CardView visibility to GONE.

Then in your class, where you handle user click, set EditText visibility to View.GONE and CardView visibility to View.VISIBLE

Upvotes: 0

Luci
Luci

Reputation: 1396

The simplest solution is to embed them in a LinearLayout or something similar and to change the visibility on user click. Hide the EditText and show the CardView.

Upvotes: 0

Related Questions