Clear TextView from text and background - android

I have a TextView called choice1 that I use for both text and image.
I know that this:
choice1.setText("");
clears it from text and this
choice1.setBackgroundResource(0);
clears it from any image.
Is there a way to clear it completely in 1 command or do I always have to run both commands to clear the TextView?

If you reaaaaally wanted to I guess you could make your own method..
private void clearTextView(TextView tv) {
tv.setText("");
tv.setBackgroundResource(0);
}
And then
clearTextView(choice1);

There is no single command to do both together, you have to call it separately.

Related

AsyncTask OnPostExecute not updating TextView

I have an AsyncTask running. I have a TextView that I mimic the message a Toast initially produces.
I want to clear the TextView upon success in OnPostExecute but it not doing so. The task complete Toast works fine. How do I set the TextView in the OnPostExecute to blank? The user is still on the display screen where the TextView is.
Code is as follows for an error condition:
#Override
protected void onPostExecute(Void result)
{ FetchingImage=0;
if(webLoadError>0)
{
TextView text = (TextView) findViewById(R.id.textView2);
String temp=" ";
text.setText(temp);
Toast.makeText(getApplicationContext(), "Image not available from the internet.\nDefault or last image loaded.\nTry again later.",Toast.LENGTH_LONG).show();
}
}
Try something like:
((TextView) findViewById(R.id.textView2)).setText("");
EDIT:
try making a variable outside the onCreate like TextView text; and then inside the onCreate put: text = (TextView) findViewById(R.id.textView2);
and then just put text.setText(""); inside the onPostExecute method.
See if that works.
I know this thread is old, but I think I found the solution (at least it worked for me), perhaps it help others:
I had a view with 3 textviews with default values receiving values from a webservices from "doInBackground" method of a AsyncTask and later changing the text in them on "onPostExecute" method. The issue was that only one of the three textviews was showing the new text from the ws in the first execution of code (it's a simple application implementing the Zxing barcode reader reading a barcode from a product and obtaining the price from a webserver, anyway), the next executions (after the app is opened) was updating the three textviews normally.
So I noticed that the only textview that was updating its value in the first execution had its parameter "android:textIsSelectable" = true, the other two was false. Bingo, changing this parameter to true in the other 2 textviews solved the issue.

TextView.setMaxLines not working?

In my app I have a screen where I display some text and then a photo. The text is variable in length (sometimes none at all, sometimes a lot), so I wanted to have it set up so the text never takes up more than a few lines (but can be scrolled) leaving enough room for the image below.
My view component for this part is created programatically, and I've adjusted the code to have the following (currently in my text-setting method, but the same thing happens if it's in the initial view-create code)
public void SetDescription(String description)
{
mTxtDescription.setText(Html.fromHtml(description));
mTxtDescription.setClickable(true);
mTxtDescription.setMaxLines(5);
mTxtDescription.setLines(5); //this makes no difference either!
mTxtDescription.setSingleLine(false);
mTxtDescription.setScrollbarFadingEnabled(true);
mTxtDescription.setScrollBarStyle(VERTICAL);
mTxtDescription.setMovementMethod(ScrollingMovementMethod.getInstance());
mTxtDescription.invalidate(); //adding this made no difference...
}
However it doesn't work- long text still fills the whole screen and the image has vanished due to being pushed down to a height of 0. How can I get the text to never be more than 5 lines?
Try removing the call to setSingleLine. And use setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE). It'd also put this call before the setMaxLines and setLines call to be sure.
Note: setLines overrides the settings of setMaxLines and setMinLines.
The TextView has many issues surrounding the various calls to how it should display multiple, ellipses, etc.
The setSingleLine(false) seemes to reset the setMaxLines command. Try to move the setSingleLine command before the setText. That worked for me.
The below code is working fine for me
txt = (TextView)findViewById(R.id.textview);
txt.setMaxLines(5);
txt.setMovementMethod(new ScrollingMovementMethod());
txt.setScrollContainer(true);
txt.setText("Example Text");
txt.setTextColor(Color.WHITE);
txt.setScrollbarFadingEnabled(true);
in xml inside textview
android:scrollbars="vertical"

How to show the name of the widget on a TextView?

Let me explain:
I need to show the name of any building block either it is imagebutton, edittext in my textview field depending upon which of above written will be hover over by the user.
So that my textview could behave like some dynamic display plate.
Any help will highly be appreciated.
mrana..
So something that you can do. Since there it no "setText" for imageviews, you can do something like
String name = "imageview";
imageView.setTag(name);
Then in your onFocusedChangedListener call the following method
void displayInTextView(View selectedView) {
String viewName = (String) selectedView.getTag();
mDisplayText.setText(viewName);
}
Since this is a touch device, "hovering" will not be possible. One solution is to show the name on when long-press. See this solution https://stackoverflow.com/a/4433441/1227692
EDI: Thanks Frank and mrana for pointing out. I agree and take back my comment.

LInkify block onclick event

I have some TextView objects that I have onclick listeners assigned to. The onclick listeners work fine unless I run Linkify.addLinks on the TextView objects, at which point the onclick event never happens. This happens regardless of whether anything in the TextView is actually linkified. Is there a way to have both events happen, or at least be able to predict whether anything is or would be linkified in the text?
I don't know if you found an answer or not as this question is rather old, but I just found it while looking for another problem (not exactly similar) and I can help (a bit): there is an easy way to "detect" whether Linkify modifies the text: just compares it with the original. Something like:
String originalText = textView.getText().toString();
Linkify.addLinks(textView, Linkify.ALL);
String linkifiedText = textView.getText().toString();
if (originalText.equals(linkifiedText)) {
// linkify did not do anything...
}
Linkify.addLinks() returns a boolean which indicates if it found links in the text or not. There is no need to do a before/after comparison like Guillaume suggested.

TextView.setText() followed by TextView.invalidate() doesn't update text in view

I have no idea why this doesn't work. The TextView is defined from an tag in the view. The base TextView doesn't have text set and I want to set it in the View on display.
I have tried placing the below in onCreate and onStart but it doesn't seem to work. The last two lines are just for debugging. I can verify that the header does get the text. The thing is, the TextView doesn't actually get updated. Any ideas?
TextView header=(TextView) findViewById(R.id.acheader);
header.setText(R.string.accounts);
header.invalidate();
header=(TextView) findViewById(R.id.acheader);
String blah=(String) header.getText();
Try again removing the text in 4th line
header=(TextView) findViewById(R.id.acheader);
header.invalidate() is not needed.
Instead of String blah = (String) header.getText() try
String blah = heager.getText().toString();
And why are you verifying a "setText()" on text view using code? Why can't you check the
actual output?
The above code might not work the way you are trying to use it, because the redraw of text view is handled by the framework and generally it tries to group item updates (Dirty rectangles to be specific) and update them all at once. It may do it well after your function exits, Try to validate visually, thats the best way.

Categories

Resources