Tuesday, 20 November 2012

How to add a image at the end of a multiline TEXTVIEW...

     Activity_main.xml    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F5A9F2"
     >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:textStyle="bold"
        android:layout_centerVertical="true"
        android:id="@+id/text"
        android:text="i want to add a image from drawable at the end of a multiline TextView "
        />

</RelativeLayout>


     MainActivity.java
 package com.imageendoftext;
import android.app.Activity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView text;
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
      text=(TextView)findViewById(R.id.text);
      
      ImageSpan is = new ImageSpan(getApplicationContext(), R.drawable.btn);
     
      SpannableString texts = new SpannableString(text.getText().toString().concat(""));
       texts.setSpan(is,texts.length()-1,texts.length(),0);
      
       text.setText(texts);

    }
}

         

Output

      
     
      

No comments:

Post a Comment