Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8226025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:37:10+00:00 2026-06-07T15:37:10+00:00

I am trying to align the TextView around ImageView. I am using the following

  • 0

enter image description here

I am trying to align the TextView around ImageView. I am using the following code:

 private void createSpannableText(){
        TextView myTextView = (TextView) findViewById(R.id.textView);
        SpannableStringBuilder builder = new SpannableStringBuilder();
        builder.append(this.getText(R.string.loren__ipsum__max));
        int lengthOfPart1 = builder.length();
        builder.append(" ");
        builder.append(this.getText(R.string.lorem__ipsum));
        Drawable d = getResources().getDrawable(R.drawable.myImage);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); // <---- Very important otherwise your image won't appear
        ImageSpan myImage = new ImageSpan(d);
        builder.setSpan(myImage, 0, lengthOfPart1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        myTextView.setText(builder);
    }

But couldn’t get the exact result. What shall I do? Do I need to use SpannableStringBuilder in this case or there is another way. Please help. I used this post-http://majaxandroidtips.blogspot.in/2009/06/how-to-have-few-layout-elements-wrap_17.html
to have the solution.

P.S.: I also want 6dp margin around the ImageView

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:37:13+00:00Added an answer on June 7, 2026 at 3:37 pm

    You can achieve this by using the android.text.style.LeadingMarginSpan.LeadingMarginSpan2 interface which is available in API 8. Here is the article, not in English though, translate it using your browser. Besides you can download the source code of the example directly from here.

    Your layout:

    <?xml version="1.0" encoding="UTF-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp">
        <TextView
            android:textSize="18.0sp"
            android:id="@+id/message_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/text" />
        <ImageView
            android:src="@drawable/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/icon" />
    </RelativeLayout>
    

    Helper class implements LeadingMarginSpan.LeadingMarginSpan2

    class MyLeadingMarginSpan2 implements LeadingMarginSpan.LeadingMarginSpan2 {
        private int margin;
        private int lines;
    
        MyLeadingMarginSpan2(int lines, int margin) {
            this.margin = margin;
            this.lines = lines;
        }
    
        /* Возвращает значение, на которе должен быть добавлен отступ */
        @Override
        public int getLeadingMargin(boolean first) {
            if (first) {
                /*
                 * Данный отступ будет применен к количеству строк
                 * возвращаемых getLeadingMarginLineCount()
                 */
                return margin;
            } else {
                // Отступ для всех остальных строк
                return 0;
            }
        }
    
        @Override
        public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, 
                int top, int baseline, int bottom, CharSequence text, 
                int start, int end, boolean first, Layout layout) {}
    
        /*
         * Возвращает количество строк, к которым должен быть 
         * применен отступ возвращаемый методом getLeadingMargin(true)
         * Замечание:
         * Отступ применяется только к N строкам первого параграфа.
         */
        @Override
        public int getLeadingMarginLineCount() {
            return lines;
        }
    };
    

    Your activity code:

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            String text = getString(R.string.text);
    
            // Получаем иконку и ее ширину
            Drawable dIcon = getResources().getDrawable(R.drawable.icon);
            int leftMargin = dIcon.getIntrinsicWidth() + 10;
    
            // Устанавливаем иконку в R.id.icon
            ImageView icon = (ImageView) findViewById(R.id.icon);
            icon.setBackgroundDrawable(dIcon);
    
            SpannableString ss = new SpannableString(text);
            // Выставляем отступ для первых трех строк абазца
            ss.setSpan(new MyLeadingMarginSpan2(3, leftMargin), 0, ss.length(), 0);
    
            TextView messageView = (TextView) findViewById(R.id.message_view);
            messageView.setText(ss);
        }
    

    And finally here a demo result:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to align the following equations around the equal sign. What can
Problem: I am trying to align divs next to each other using float:left but
I'm trying to align an ImageView to the top of the screen with a
I'm trying to align a text horizontal and vertically. Using de horizontal axis, I
I am trying to align differently sized images in a line using the vertical-align
I was trying to align doubles using string.format and display them on a tooltip.
Below is a TextView followed by an ImageView contained in RelativeLayout. I'm trying to
Using iTextSharp i'm trying to align an image so that it gets embedded in
I'm trying to align the values. The code is, <section class=message success> Variable1: Value1<br/>
I am trying to align jquery ui-icons with text like following, but I notice

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.