textview interview questions
Top textview frequently asked interview questions
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)
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)
This question already has an answer here:
How can I set Text Color of a text view to #bdbdbd
programatically?
Source: (StackOverflow)
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)
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)
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)
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 the text horizontally and vertically in a TextView
in Android, so that it appears exactly in the middle of the TextView
?
Source: (StackOverflow)
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)
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 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)
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)
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)