• 6
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to convert TextView to EditText in android?

I want to convert TextView into Edit Text and vice versa in a button click event in android. I don't know how to achieve this , please help if anyone knows the solution.

Regards, Rajapandian.K

Best to use a ViewSwitcher

...
    <ViewSwitcher
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_switcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/clickable_text_view"
            android:clickable="true"
            android:onClick="TextViewClicked"
            android:text="@string/some_value" />

        <EditText
            android:id="@+id/hidden_edit_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/some_value" />
    </ViewSwitcher>
...

Then you can switch the views by

public void TextViewClicked() {
    ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.my_switcher);
    switcher.showNext(); //or switcher.showPrevious();
    TextView myTV = (TextView) switcher.findViewById(R.id.clickable_text_view);
    myTV.setText("value");
}
  • 76
Reply Report
    • I honestly think out of all of them, this answer was the best. Its so much simpler. I am curious. When should this be used? I read the doc, and it says for animation type stuff. But in this case, it makes sense to use it. So is there a reason to not use it in this case?
      • 2
    • I see you've used android:onClick="TextViewClicked", however as I'm using a Fragment, it searches the activity of that Fragment and therefore doesn't find the method. How do I fix this so it finds the method from the Fragment and not the parent activity?

I never tried it but you can (probably) use EditText and set the background to that of TextView. Then onfocus change you can simply swap backgrounds based on what you want to see at any given moment. I'll try to implement it and see if it'll work

  • 0
Reply Report

What I think best is to actually use Framelayout concept.

Such that original layout looks like

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
.
.
.
</Linearlayout>

Place it inside Framelayout like this.

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
.
.
.
</Linearlayout>

<!-- The Magic is here -->
<View

    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>



</FrameLayout>

Here the LinearLayout will hold all your EditText View. Also remember to make background of EditText View null.

Like this

<EditText 
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:id="@+id/editInp"
        android:hint="@string/hello_world"
        android:background="@null"
        android:singleLine="true"
        android:inputType="textCapWords"/>

or

<EditText 
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:id="@+id/editInp"
        android:hint="@string/hello_world"
        android:background="@android:drawable/transparent"
        android:singleLine="true"
        android:inputType="textCapWords"/>
  • 0
Reply Report