Hi guys,
Today i am going to share about text around the images in android.which is used to place some texts around some image.
Starting
from api 8 (Android 2.2) has a new interface LeadingMarginSpan2,
which lets you create text indent for the first N rows. The
picture created indented 50 pixels for the first 3 rows.
Using this code
1.First create xml that include one image and textview
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:gravity="left|right" />
</RelativeLayout>
2.Using Spannable String and apply a new style in textview.
MainActivity.java
package com.example.demo;
import android.text.Layout;
import android.text.SpannableString;
import android.text.style.LeadingMarginSpan.LeadingMarginSpan2;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
public class MainActivity extends Activity {
Context c;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState)
{
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Drawable dIcon = getResources().getDrawable(R.drawable.mine);
int leftMargin = dIcon.getIntrinsicWidth()+30;
ImageView icon = (ImageView) findViewById(R.id.imageView1);
icon.setBackgroundDrawable(dIcon);
String values = getString(R.string.con);
SpannableString ss = new SpannableString(values);
ss.setSpan(new Margin(6, leftMargin), 0, ss.length(), 0);
TextView messageView = (TextView) findViewById(R.id.textView1);
messageView.setText(ss);
}
class Margin implements LeadingMarginSpan2
{
{
private int margin;
private int lines;
Margin(int lines, int margin)
{
{
this.margin = margin;
this.lines = lines;
}
@Override
public void drawLeadingMargin(Canvas arg0, Paint arg1, int arg2,
int arg3, int arg4, int arg5, int arg6, CharSequence arg7,
int arg8, int arg9, boolean arg10, Layout arg11)
{
{
// TODO Auto-generated method stub
}
@Override
public int getLeadingMargin(boolean arg0)
{
{
// TODO Auto-generated method stub
if (arg0) {
/*
* This indentation is applied to the number of rows returned
* getLeadingMarginLineCount ()
*/
return margin;
}
else
{
else
{
// Offset for all other Layout layout ) { }
/*
* Returns * the number of rows which should be applied * indent
* returned by getLeadingMargin (true) Note:* Indent only
* applies to N lines of the first paragraph.
*/
return 0;
}
}
@Override
public int getLeadingMarginLineCount()
{
{
// TODO Auto-generated method stub
return lines;
}
}
}
Happy Coding...
1 comments:
Write commentsExcellent article. Ó€'m experiencing many Ö…f thee issues as well..
ReplyEmoticonEmoticon