开发者

Is there a way to hide text in a TextView?

开发者 https://www.devze.com 2023-03-19 23:47 出处:网络
Is 开发者_开发技巧there a way to hide some (but not all) of the text in a TextView?I tried setting the size to 0 with AbsoluteSizeSpan, but that doesn\'t have any visual effect that I see.(You can set

Is 开发者_开发技巧there a way to hide some (but not all) of the text in a TextView? I tried setting the size to 0 with AbsoluteSizeSpan, but that doesn't have any visual effect that I see. (You can set the size to 1, and you essentially get bumpy lines instead of readable text. Cute, but not quite what I'm after.)

By hide, I mean go away, not be visible and not take up space. Drawing text with the same color as the background isn't what I'm looking for.

I realize I can just replace the text in the TextView with only the text I want to display, but I'm already using spans to do a lot more dynamic styling, and something like a HiddenSpan would be useful. Does it exist?


I think you're looking for: TextView.setVisibility(View.GONE)


try by this code:

I have complete by this code..

<TextView
android:id="@+id/tvi" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="invisible"

or

    android:visibility="gone"
/>

or by code:-

   TextView txtView = (TextView)findViewById(R.id.tvi);
   txtView.setVisibility(View.INVISIBLE);

OR

  txtView.setVisibility(View.GONE);

May be this will help you


<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="0sp"
  android:textColor="@android:color/transparent" />

Transparent text color hides the visibility of the text. Add textSize to that to remove or minimize the space it takes up.


I just had this problem and wrote the following class:

public class NoDisplaySpan extends ReplacementSpan {

    public NoDisplaySpan() {}

    @Override
    public void draw(Canvas arg0, CharSequence arg1, int arg2, int arg3,
            float arg4, int arg5, int arg6, int arg7, Paint arg8) {}

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end,
            FontMetricsInt fm) {
        return 0;
    }
}

It seems to only work within a paragraph; I get an ArrayIndexOutofBoundsException thrown when I set the span to extend past a newline. I'd love to hear if anyone can figure that out.


just had this issue. Basically you just display a part of the string by using substring or whatever and if you click on the textview, you set the fulltext to it.

Hope it can help out someone going through that topic. (Can copy paste a snipet of code if required).


Not sure if you are still looking for an answer to this, as it is very old, but I found it while looking for something and thought I could help. Have you tried setting one button on the keyboard or a hardware button to switch between the visible TextView and a hidden one. You would have to play around with copying text from visible to hidden if you want to keep the visible as part of the hidden. If you want the hidden to be its own text, then this will be an easy solution.


I've figured out a way. Let's use SpannableString and Color.TRANSPARENT

For example: <> Original text : "Hello dude"

<> Expected text : "_____ dude"

  • Here i will use Kotlin:

val spannableString = SpannableString("Hello dude")

spannableString.setSpan(ForegroundColorSpan(Color.TRANSPARENT), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

yourTextView.text = spannableString


You can try to use something like that: https://gist.github.com/RicardAparicio/bdd6cd30410f9431e1da238202a1dfa9

Change some parameters if needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消