-1

I have a an editText whose background I have set to transparent to make it look like a textView.initial editText

On the Onfocuschanged method, when in focus I set the background resource as

ph.setBackgroundResource(android.R.drawable.edit_text);

after focus

And when focus is removed I want to make it look like a textView again, so I set the background as

 ph.setBackgroundColor(Color.TRANSPARENT);

moved text

But the text in the editText stays in the position of the previous background and does not return to its original position.

EDIT: Added xml code for the editText

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:text="Medium Text"
        android:id="@+id/ph"
        android:layout_alignBottom="@+id/textView12"
        android:layout_toRightOf="@+id/textView12"
        android:layout_toEndOf="@+id/textView12" />
Isj
  • 2,020
  • 1
  • 14
  • 20

3 Answers3

0

Try to replace

ph.setBackgroundColor(Color.TRANSPARENT);

with

ph.setBackground(null);

Edit:

add the following lines after setting the background to transparent.

RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_BASELINE, R.id.textView12);
ph.setLayoutParams(params);
Derek Fung
  • 8,171
  • 1
  • 25
  • 28
0

After you sets ph.setBackgroundResource(android.R.drawable.edit_text); you change an img src field of EditText. Try manipulate only with Drawable. Change ph.setBackgroundResource(android.R.drawable.edit_text)

to

ph.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.edit_text)); 

and then replace

ph.setBackgroundColor(Color.TRANSPARENT);

with

ph.setBackgroundDrawable(null);

Slampy
  • 326
  • 1
  • 7
  • Ok. Have a look at http://stackoverflow.com/questions/9017903/edittext-that-looks-like-textview-inside-a-listview – Slampy Aug 31 '15 at 10:42
  • Ya I saw it, I guess this will be my fallback if I cant figure out the above one. – Isj Sep 01 '15 at 06:01
0

So I finally solved this problem by using

android:layout_alignBaseline="@+id/textView12"

instead of

android:layout_alignBottom="@+id/textView12"

Baseline allignment actually alligns the 2 vies text to text. So when I change the background from transparent to android.R.drawable.edit_text it alligns itto the texView12 according to the position of the text not the background.

Isj
  • 2,020
  • 1
  • 14
  • 20