3

i would like to create a TextView with a colored background and space between each line, link this: enter image description here

I'm trying with SpannableString but i can't create transparent space between lines, any idea?

Thanks and guys sorry for my BAD english.

alfdev
  • 1,039
  • 1
  • 10
  • 23

3 Answers3

0

Try using lineSpacingExtra and lineSpacingMultiplier in your XML file.

Jas
  • 3,207
  • 2
  • 15
  • 45
0

If I am not taking wrong you want to add a transparent new line in your TextView ?

This can't be possible with single TextView as SpannableString will only effect the content of TextView and background of Textview is different from content of TextView. If you have to achieve this then you have to provide custom implementation for TextView which will draw a transparent background in onDraw method for your custom view when in text content it find new line.

Or other option is to render a new textview for each line of text.

VikasGoyal
  • 3,308
  • 1
  • 22
  • 42
0

Hi it should be done using add textview dynemically in layout and at that time you have to set properties of textview.You can do this like

TvEx.java

public class TvEx extends Activity {
LinearLayout llMain;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tv_ex);
    llMain = (LinearLayout) findViewById(R.id.llmainTvEx);

    final int N = 10; // total number of textviews to add

    final TextView[] myTextViews = new TextView[N]; // create an empty
                                                    // array;

    for (int i = 0; i < N; i++) {
        // create a new textview
        final TextView rowTextView = new TextView(this);
        LinearLayout.LayoutParams buttonLayoutParams = new  LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        buttonLayoutParams.setMargins(100, 20, 0, 0); // Set margins here
        // set some properties of rowTextView or something
        rowTextView.setText("This is row #" + i);
        rowTextView.setBackgroundColor(Color.WHITE);
        rowTextView.setLayoutParams(buttonLayoutParams);
        // add the textview to the linearlayout
        llMain.addView(rowTextView);
        // save a reference to the textview for later
        myTextViews[i] = rowTextView;
     }
   }
}

tv_ex.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:id="@+id/llmainTvEx"
   android:layout_height="match_parent"
   android:background="#A75653"
   android:orientation="vertical"></LinearLayout>
Sam
  • 462
  • 2
  • 13
  • Using this code you can add Textview Dynemically and set properties to that and whenever there is newline you have consider it as new string and can do what you want. – Sam Nov 18 '15 at 10:41
  • How to divide the text into rows? – alfdev Nov 19 '15 at 19:30
  • using split the String : http://stackoverflow.com/questions/3732790/android-split-string – Sam Nov 20 '15 at 06:28
  • But i need to split the string according to the width of the available space – alfdev Nov 20 '15 at 08:03
  • it's dynamic, it comes from a web service – alfdev Nov 20 '15 at 09:50
  • So what... If it is single string then for testing pass that string to understand that hows string that is and then i suggest you to how to split it in multiple string. – Sam Nov 20 '15 at 10:19