Insert an image into EditText

Source: Internet
Author: User

Main. xml is as follows:

[Html]
<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"
Tools: context = ". MainActivity">
 
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerHorizontal = "true"
Android: layout_marginTop = "20dip"
Android: textSize = "18sp"
Android: text = "test: inserting images into EditText"/>

<EditText
Android: id = "@ + id/editText"
Android: layout_width = "fill_parent"
Android: layout_marginLeft = "30dip"
Android: layout_marginRight = "30dip"
Android: layout_height = "180dip"
Android: layout_marginTop = "50dip"
Android: hint = "insert emoticon"
/>

<Button
Android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_marginLeft = "30dip"
Android: layout_marginRight = "30dip"
Android: layout_height = "80dip"
Android: layout_marginTop = "250dip"
Android: hint = "insert emoticon"
/>
 
</RelativeLayout>

<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"
Tools: context = ". MainActivity">

<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerHorizontal = "true"
Android: layout_marginTop = "20dip"
Android: textSize = "18sp"
Android: text = "test: inserting images into EditText"/>

<EditText
Android: id = "@ + id/editText"
Android: layout_width = "fill_parent"
Android: layout_marginLeft = "30dip"
Android: layout_marginRight = "30dip"
Android: layout_height = "180dip"
Android: layout_marginTop = "50dip"
Android: hint = "insert emoticon"
/>

<Button
Android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_marginLeft = "30dip"
Android: layout_marginRight = "30dip"
Android: layout_height = "80dip"
Android: layout_marginTop = "250dip"
Android: hint = "insert emoticon"
/>

</RelativeLayout>
MainActivity is as follows:

[Java]
Package com.cn;
Import android. OS. Bundle;
Import android. text. SpannableString;
Import android. text. style. ImageSpan;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. app. Activity;
Import android. graphics. BitmapFactory;
/**
* Demo description:
* Insert an image into EditText
*/
Public class MainActivity extends Activity {
Private EditText mEditText;
Private Button mButton;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
MEditText = (EditText) findViewById (R. id. editText );
MButton = (Button) findViewById (R. id. button );
MButton. setOnClickListener (new ClickListenerImpl ());
}
 
Private class ClickListenerImpl implements OnClickListener {
@ Override
Public void onClick (View v ){
ImageSpan imageSpan = new ImageSpan (MainActivity. this,
BitmapFactory. decodeResource (getResources (), R. drawable. ic_launcher ));
SpannableString spannableString = new SpannableString ("test ");
SpannableString. setSpan (imageSpan, 0, spannableString. length (), SpannableString. SPAN_MARK_MARK );
MEditText. append (spannableString );
}
 
}
 
}

Package com.cn;
Import android. OS. Bundle;
Import android. text. SpannableString;
Import android. text. style. ImageSpan;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. app. Activity;
Import android. graphics. BitmapFactory;
/**
* Demo description:
* Insert an image into EditText
*/
Public class MainActivity extends Activity {
Private EditText mEditText;
Private Button mButton;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
MEditText = (EditText) findViewById (R. id. editText );
MButton = (Button) findViewById (R. id. button );
MButton. setOnClickListener (new ClickListenerImpl ());
}

Private class ClickListenerImpl implements OnClickListener {
@ Override
Public void onClick (View v ){
ImageSpan imageSpan = new ImageSpan (MainActivity. this,
BitmapFactory. decodeResource (getResources (), R. drawable. ic_launcher ));
SpannableString spannableString = new SpannableString ("test ");
SpannableString. setSpan (imageSpan, 0, spannableString. length (), SpannableString. SPAN_MARK_MARK );
MEditText. append (spannableString );
}

}

}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.