0

This how I show the bullet list of strings (max 4 items) using the Unicode character, now I need to colour code the bullet to be red and i don't want to start the second line of the text to below the bullet point

I tried adding textView with drwableLeft but bullet not alliged to first line of the heading

android:drawablePadding="2dp" 
android:drawableLeft="@drawable/ic_red_bullet"

is there any way i can add text with colored bullet point

<string name="headlines">
\n\u25CF Conversion disorder - the mysterious condition dogged by doubt and stigma \n
\n\u25CF Baseless and ludicrous: Kremlin responds to Mueller probe \n
\n\u25CF Ibrahims back in court over alleged drug and tobacco imports \n
</string>


    Layout-file

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">




        <TextView
            android:id="@+id/textView2"
            android:layout_width="356dp"
            android:layout_height="wrap_content"
            android:text="@string/headlines"
            app:layout_constraintRight_toRightOf="parent"
            tools:layout_constraintTop_creator="1"
            android:textColor="@color/colorBlack"
            tools:layout_constraintRight_creator="1"
            android:layout_marginRight="12dp"
            android:layout_marginEnd="12dp"
            android:layout_marginTop="28dp"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintHorizontal_bias="1.0" />

    </android.support.constraint.ConstraintLayout>
Sam
  • 6,215
  • 9
  • 71
  • 90

3 Answers3

0

You can use Spannable (SpannableString) for this purposes. See that topic Set color of TextView span in Android

0

put this code in your activity...

  TextView TV = (TextView) findViewById(R.id.textView2);

    Spannable wordtoSpan = new SpannableString(" \u25CF Conversion disorder - the mysterious condition dogged by doubt and stigma ");

    wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    TV.setText(wordtoSpan);
  • this worked only for coloured the bullet but did not help me on this " I don't want to start the second line of the text to below the bullet point" – Sam Nov 01 '17 at 10:38
0

Finally,end up with writing a compound view.

    layout/bullet_textview.xml

    <?xml version="1.0" encoding="utf-8"?>
    <merge xmlns:android="http://schemas.android.com/apk/res/android">
        <TextView
            android:id="@+id/bullet_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/ic_fiber_manual_record_black_24dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            />

        <TextView
            android:id="@+id/data_text_view"
            android:paddingLeft="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/header1"
            android:textColor="@color/colorBlack"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="14sp" />
    </merge>
  =========  
    public class BulletTextView extends LinearLayout {

        private TextView dataText;

        String dataTextValue;
        public BulletTextView(Context context) {
            super(context);
            initializeViews(context);
        }

        public BulletTextView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);

            TypedArray typedArray  = context.obtainStyledAttributes(attrs, R.styleable.BulletTextView);
            dataTextValue = typedArray.getString(R.styleable.BulletTextView_data);

            initializeViews(context);
        }

        public BulletTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            TypedArray typedArray  = context.obtainStyledAttributes(attrs, R.styleable.BulletTextView);
            dataTextValue = typedArray.getString(R.styleable.BulletTextView_data);
            initializeViews(context);
        }

        public BulletTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            TypedArray typedArray  = context.obtainStyledAttributes(attrs, R.styleable.BulletTextView);
            dataTextValue = typedArray.getString(R.styleable.BulletTextView_data);
            initializeViews(context);
        }

        /**
         * Inflates the views in the layout.
         *
         * @param context
         *           the current context for the view.
         */
        private void initializeViews(Context context) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.bullet_textview, this);
        }

        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
            dataText = (TextView) this.findViewById(R.id.data_text_view);
            dataText.setText(dataTextValue);
        }

        public void setData(String  data){
            dataText.setText(data);
        }
    }

    values/attr.xml
===================
    <resources>
        <declare-styleable name="BulletTextView">
            <attr name="data" format="string" />
        </declare-styleable>
    </resources>


    and Usage
  ============  
    <LinearLayout
                android:layout_width="368dp"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <yourpackage.BulletTextView
                    android:id="@+id/firstHeader"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:orientation="horizontal" />

                <yourpackage.BulletTextView
                    android:id="@+id/secondHeader"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:orientation="horizontal"
                    bullettext:data="@string/title_dashboard" />

                <yourpackage.BulletTextView
                    android:id="@+id/thirdHeader"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:orientation="horizontal"
                    bullettext:data="@string/headlinesnow" />
            </LinearLayout>
Sam
  • 6,215
  • 9
  • 71
  • 90