Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the exact text from Edit Text and set into text view in android

My problem is this I want to get exact text from Edit text with font that set in Edit text as well as Text size,text color and text style like bold, italic and underline.

till now i used Spannable like this Spannable messageText; and get the text from EditText like this

 messageText = editText.getText();

and set into textview

 textView.setText(messageText);

But in this case it return only the simple string not color,font,size and style.

EditText

   <EditText
        android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="180dp"
        android:inputType="textMultiLine"
        android:singleLine="false"
        android:tag="no"
        android:textColor="#000000"
        android:textSize="15sp"
        android:textStyle="normal"
        android:typeface="normal" />

TextView

<TextView
            android:id="@+id/preview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text=""
            android:textColor="#000000"
            android:textSize="15sp"
            android:textStyle="normal"
            android:typeface="normal" />

Help me, Thanks

like image 620
Naveen Kumar Avatar asked Dec 28 '12 05:12

Naveen Kumar


2 Answers

If you are setting the background color of the edit text like this

 editText.setBackgroundDrawable(new PaintDrawable(Color.YELLOW));

Then,

Do this to get the background color.

PaintDrawable drawable = (PaintDrawable) editText.getBackground();
int color = drawable.getPaint().getColor();

And then set this color to the textView.

textView.setBackgroundColor(color); 
like image 77
Narendra Pal Avatar answered Oct 13 '22 10:10

Narendra Pal


Hi I have ran sample project for you..I think you are expecting this answer..

This is xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

     <EditText
        android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="180dp"
        android:inputType="textMultiLine"
        android:singleLine="false"
        android:tag="no"
        android:textColor="#1DA237"
        android:textSize="15sp"
        android:textStyle="italic"
        android:typeface="normal" />


    <TextView
            android:id="@+id/preview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="subbu"
            android:textColor="#1DA237"
            android:textSize="15sp"
            android:textStyle="italic"
            android:typeface="normal" 
            android:paddingBottom="50dp"
            android:layout_below="@+id/message"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"

        android:layout_marginRight="29dp"
        android:layout_marginTop="93dp"
        android:text="Button" 
        android:layout_below="@+id/preview"/>

</RelativeLayout>

Activity code:

 final EditText text=(EditText)findViewById(R.id.message);

        Button b=(Button)findViewById(R.id.button1);
        final TextView t=(TextView)findViewById(R.id.preview);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                t.setText(text.getText().toString());

            }
        });

I think this is what you are expecting.

like image 29
Subburaj Avatar answered Oct 13 '22 08:10

Subburaj