Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a TextView to EditText in android?

I am fetching the data from a database and I am appending it to a TextView. When I do long click on TextView I want to convert it to an EditText. This is how I set the data on my TextView:

TextView text = (TextView) vi.findViewById(R.id.menutext);
text.setText(itemnames[position]);//comes from database append to text view

Now I want to define a setOnLongClickListener to convert it into a EditText.

text.setOnLongClickListener(new OnLongClickListener() {

    public boolean onLongClick(View v) {                
        String edititemname=itemnames[position];
        System.out.println(edititemname);
        return true;                
    }
});

edititemname holds which item was pressed in a long click. I want to fill the same information into the EdiText. Please help me.

like image 379
Vinoth Avatar asked Apr 02 '12 12:04

Vinoth


1 Answers

As far as I know you can't convert one to another. What you can is: Have a TextView and an EditText created in xml. EditText is hidden when TextView is showing. Then, on your listener to the onCLick you can:

text.setVisibility(View.GONE); 
editText.setVisibility(View.VISIBLE);
editText.setText(edititemname);

The editText variable can be defined where you define the text. You have to use the findViewById.

like image 52
Tiago Almeida Avatar answered Jan 04 '23 06:01

Tiago Almeida