Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text is not visible when setmovementmethod is called on a textview with singleline is true

I have this textView in my app for which singleLine is set as true.

When I call setMovementMethod(LinkMovementMethod.getInstance()) on it and set some arabic or urdu text, the text is not visible. Although the textview is visible(i have checked by applying color on its background.

I tried using maxlines=1 instead of single line which fixes visibility problem, but my text being very long, the text in textview becomes scrollable instead of being ellipsized.

Am I doing something wrong? Is there a way to make text ellipsized instead of scrolling in case of maxlines=1?

Appreciate any help.

Edit 1. Adding Sample code:

JAVA

package com.example.invisibletextsample;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.widget.TextView;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView mTextView = (TextView)findViewById(R.id.tv1);
        String mString = "9876543210 دمية النص";
        mTextView.setText(mString);
        Linkify.addLinks(mTextView, Linkify.PHONE_NUMBERS);
        mTextView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

XML

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.invisibletextsample.MainActivity" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:background="@android:color/holo_red_light"
        android:singleLine="true"
        android:ellipsize="end" />

</RelativeLayout>
like image 951
samdroid Avatar asked Apr 17 '15 08:04

samdroid


People also ask

Which method is used to set the text in a TextView?

SetText(String, TextView+BufferType) Sets the text to be displayed using a string resource identifier.

How do you use TextView?

Android App Development for BeginnersA TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing.

How do I add a newline to a TextView in Android?

Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.


2 Answers

you restrict Maximum line a TextView can take when you constraint it with

singleLine = "true" ==> text should be only on line long

or

maxlines ="1" ==> maximum lines taken by TextView should be only 1 line

remove this lines from xml and your text should appear just fine. and trying putting your textview under a ScrollView that you may scroll it if test is really huge

like image 178
Pankaj Nimgade Avatar answered Oct 29 '22 18:10

Pankaj Nimgade


I meet the same problem.

So I don't need setMovementMethod

Expansion TextView,take over MovementMethod.

public class EllipsizeSpannableTextView extends TextView {

private Spannable spannable;

public EllipsizeSpannableTextView(Context context) {
    super(context);
}

public EllipsizeSpannableTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public EllipsizeSpannableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
public void setText(CharSequence text, BufferType type) {
    super.setText(text, type);
    if (text instanceof Spannable) {
        spannable = (Spannable) text;
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (spannable == null) {
        return super.onTouchEvent(event);
    }
    int action = event.getAction();

    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        x -= getTotalPaddingLeft();
        y -= getTotalPaddingTop();

        x += getScrollX();
        y += getScrollY();

        Layout layout = getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);

        ClickableSpan[] links = spannable.getSpans(off, off, ClickableSpan.class);

        if (links.length != 0) {
            ClickableSpan link = links[0];
            if (action == MotionEvent.ACTION_UP) {
                link.onClick(this);
            } else if (action == MotionEvent.ACTION_DOWN) {
                Selection.setSelection(spannable, spannable.getSpanStart(link),
                    spannable.getSpanEnd(link));
            }
            return true;
        } else {
            Selection.removeSelection(spannable);
        }
    }
    return super.onTouchEvent(event);
}}
like image 30
lingyfh Avatar answered Oct 29 '22 16:10

lingyfh