EzDevInfo.com

textview interview questions

Top textview frequently asked interview questions

How to set the font style to bold, italic and underlined in an Android TextView?

I want to make a TextView's content bold, italic and underlined. I tried the following code and it works, but doesn't underline.

<Textview android:textStyle="bold|italic" ..

How do I do it? Any quick ideas?


Source: (StackOverflow)

Is there a way to make ellipsize="marquee" always scroll?

I want to use the marquee effect on a TextView, but the text is only being scrolled when the TextView gets focus. That's a problem, because in my case, it can't.

I am using:

  android:ellipsize="marquee"
  android:marqueeRepeatLimit="marquee_forever"

Is there a way to have the TextView always scroll its text? I've seen this being done in the Android Market app, where the app name will scroll in the title bar, even if it doesn't receive focus, but I couldn't find this being mentioned in the API docs.


Source: (StackOverflow)

Advertisements

How to set text color to a text view programmatically [duplicate]

This question already has an answer here:

How can I set Text Color of a text view to #bdbdbd programatically?


Source: (StackOverflow)

Shadow Effect for a Text in Android? [duplicate]

Possible Duplicate:
Android - shadow on text?

How can i make shadow effect text in a TextView.

Any Idea?


Source: (StackOverflow)

How to programmatically set maxLength in Android TextView?

I would like to programmatically set maxLength property of TextView as I don't want to hard code it in the layout. I can't see any set method related to maxLength.

Can anyone guide me how to achieve this?


Source: (StackOverflow)

How to add a line break in an Android TextView?

I am trying to add a line break in the TextView.

I tried suggested \n but that does nothing. Here is how I set my texts.

TextView txtSubTitle = (TextView)findViewById(r.id.txtSubTitle);
txtSubTitle.setText(Html.fromHtml(getResources().getString(R.string.sample_string)));

This is my String: <string name="sample_string">some test line 1 \n some test line 2</string>

It should show like so:

some test line 1
some test line 2

But it shows like so: some test line 1 some test line 2.

Am I missing something?


Source: (StackOverflow)

How to change a TextView's style at runtime

I have an android app on which, when the user taps a TextView, I would like to apply a defined style.

I thought to find a textview.setStyle() but it doesn't exists. I tried

textview.setTextAppearance();

but it does not work.


Source: (StackOverflow)

Is there a way to style a TextView to uppercase all of its letters?

I would like to be able to assign a xml attribute or style to a TextView that will make whatever text it has in ALL CAPITAL LETTERS.

The attributes android:inputType="textCapCharacters" and android:capitalize="characters" do nothing and look like they are for user inputed text, not a TextView.

I would like to do this so I can separate the style from the content. I know I could do this programmically but again I want keep style out of the content and the code.


Source: (StackOverflow)

How do I center text horizontally and vertically in a TextView on Android?

How do I center the text horizontally and vertically in a TextView in Android, so that it appears exactly in the middle of the TextView?


Source: (StackOverflow)

Auto Scale TextView Text to Fit within Bounds

I'm looking for an optimal way to resize wrapping text in a TextView so that it will fit within its getHeight and getWidth bounds. I'm not simply looking for a way to wrap the text- I want to make sure it both wraps and is small enough to fit entirely on the screen.

I've seen a few cases on StackOverflow where auto resizing was needed, but they are either very special cases with hack solutions, have no solution, or involve re-drawing the TextView recursively until it is small enough (which is memory intense and forces the user to watch the text shrink step-by-step with every recursion).

But I'm sure somebody out there has found a good solution that doesn't involve what I'm doing: writing several heavy routines that parse and measure the text, resize the text, and repeat until a suitably small size has been found.

What routines does TextView use to wrap the text? Couldn't those be somehow used to predict whether text will be small enough?

tl;dr: is there a best-practice way to auto-resize a TextView to fit, wrapped, in its getHeight and getWidth bounds?


Source: (StackOverflow)

how to put a border around an android textview

Is it possible to draw a border around a textview?


Source: (StackOverflow)

How to set the text color of TextView in code?

In XML, we can set a text color by the textColor attribute, like android:textColor="#FF0000". But how do I change it by coding?

I tried something like:

holder.text.setTextColor(R.color.Red);

Where holder is just a class and text is of type TextView. Red is an RGB value (#FF0000) set in strings.

But it shows a different color rather than red. What kind of parameter can we pass in setTextColor()? In documentation, it says int, but is it a resource reference value or anything else?


Source: (StackOverflow)

Is it possible to have multiple styles inside a TextView?

Is it possible to set multiple styles for different pieces of text inside a TextView?

For instance, I am setting the text as follows:

tv.setText(line1 + "\n" + line2 + "\n" + word1 + "\t" + word2 + "\t" + word3);

Is it possible to have a different style for each text element? E.g., line1 bold, word1 italic, etc.

The developer guide's Common Tasks and How to Do Them in Android includes Selecting, Highlighting, or Styling Portions of Text:

// Get our EditText object.
EditText vw = (EditText)findViewById(R.id.text);

// Set the EditText's text.
vw.setText("Italic, highlighted, bold.");

// If this were just a TextView, we could do:
// vw.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE);
// to force it to use Spannable storage so styles can be attached.
// Or we could specify that in the XML.

// Get the EditText's internal text storage
Spannable str = vw.getText();

// Create our span sections, and assign a format to each.
str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

But that uses explicit position numbers inside the text. Is there a cleaner way to do this?


Source: (StackOverflow)

Making TextView Scrollable in Android

I am displaying text in a textview that appears to be too long to fit into one screen. I need to make my TextView scrollable. How can i do that? Here is the code

final TextView tv = new TextView(this);
tv.setBackgroundResource(R.drawable.splash);
tv.setTypeface(face);
tv.setTextSize(18);
tv.setTextColor(R.color.BROWN);
tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
tv.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent e) {
        Random r = new Random();
        int i = r.nextInt(101);
        if (e.getAction() == e.ACTION_DOWN) {
            tv.setText(tips[i]);
            tv.setBackgroundResource(R.drawable.inner);
        }
        return true;
    }
});
setContentView(tv);

Source: (StackOverflow)

Android TextView Justify Text

How do you get the text of a TextView to be Justified (with text flush on the left- and right- hand sides)?

I found a possible solution here, but it does not work (even if you change vertical-center to center_vertical, etc).


Source: (StackOverflow)