ScrollView contents update - java

I have a programmatically generated ScrollView with a TextView inside of it. I wish to update the text in the TextView from time to time and some have implemented a handler function to update the TextView GUI element. This seems to call/work correctly.
However, currently the only way I have found to actually get the TextView to show the appended information is to call:
consoleText.append("New text to add to TextView");
// then:
myScrollView.removeView(myTextView);
myScrollView.addView(myTextView);
This is not particularity optimal and was wondering how else I can refresh the contents of the ScrollView to show my newly added information...
Also:
invalidate();
postvalidate();
Do not seem to do anything - the TextView object has the new text in it (looking in debug) it's just not drawing it to the screen unless I call the add/remove function.
Thanks for any information/help you can give
FR

You should call invalidate() and requestLayout() on the TextView, and perhaps the ScrollView.

Related

ViewStub - Show view with dynamic content

This may be a dumb question, so my apologies if so; I'm fairly new to Android.
But anyway - I have a working ViewStub, which is replaced by different layouts depending different situations. It's working fine with regards to showing the correct layout when I call the setLayoutResource() method, and then setVisibility to VISIBLE. However, I now need some of the content in this view that is being shows to be dynamic (i.e. I need to set it via code rather than just show a static layout).
Is this even possible? The setLayoutResource() method only takes a static layout-resource ID, but I need that layout XML file to be able to have it's TextViews contain non-static text that comes from some code that I have ready to utilize. How should this be approached if possible? I understand the concept of having a Java class, and inflating the XML to attach itself to it to update the fields, but I can't see how that relates to my code at hand, since it's simply a layout resource int I need to set for the setLayoutResource() method in ViewStub.
I can post existing code if needed, but I'm not sure it do much more than clutter up the post. For reference - All I have is a simple layout XML file with some TextViews, and then my main XML containing the ViewStub, which is part of a custom Dialog. The user is able to instantiate the Dialog and set the layout, which in turn sets the layout of the ViewStub. This is the layout in which I need the dynamic content to be used.
Any advice would be greatly appreciated. Thanks!
Turns out this wasn't too difficult to accomplish. I just needed to use the ID of the TextView layouts after inflating the ViewStub to get a copy of the actual TextViews, then I was easily able to set their text to whatever kind of dynamic/custom text I desired.
I also needed to comment out the code that shows it via the .VISIBLE call, and instead do the following (the .inflate() line of code accomplishes the same thing as setting it to VISIBLE):
View inflatedView = dialog.myStubView.inflate();
TextView myTextView = (TextView) inflatedView.findViewById(R.id.my_text_view);
myTextView.setText("Dynamic/Custom Text");

Add textview in Cocos2d really need

I need to implement TextView widget in cocos2d-android-1 and don't know how please help
TextView textView = null;
textView.setText(R.string.billing_not_supported_message);
addChild(textView);
This code not working because addChild needs node. Please help really need.
I completely agree that the android version of cocos2d needs some serious 'standard UI' features adding to it.
At the moment, your only real option is to take a similar approach to my previous answer here
ANDROID:How to open web page in class extends CCLayer
whereby you have a layout which will put a textview on the screen (or you construct it yourself in your handler), and you use a handler from the activity which your scene is running in to show/hide it.
It's clunky and horrible but it works. In my field designer app i faced the same problem, but i also had to have a custom background to the text field, which resized with the text field, that had a rough edge, and the text view had to fall inside that rough edge so all the text was visible on the main bit of the background.
i achieve that using this same technique, but i created a layout xml file so that i had control over how the textview and it's background were displayed.
(note to show/hide the textview i had to give its root layout a constant ID and check for if that ID existed, and was visible, as when people touched outside it, i needed to make it vanish)

Android how to update textview on UI

I have a textview in an activity. I am changing its visibility.
if(CheckBookmark(Constants.versusHeading.get(curVCount))) {
bookmark.setVisibility(View.VISIBLE);
bookmark.setText("Bookmarked");
}
else {
bookmark.setVisibility(View.GONE);
}
but UI is not updated. I have tried bookmark.invalidate() and bookmark.postinvalidate() but didn't work. If I change textview visibility how do I refresh UI? Activity is still alive and running.
Okay I think I need to add more details. My bad should have mentioned in the first place....Initially when activity is displayed bookmark is visible but have no text.
Then user creates a bookmark. Checkbookmark returns true if bookmark is successfully created. In that case I add bookmarked as text. I am setting it to visible as it wasn't working. If I quit activity and come back then bookmark text is displayed.
In general it should work the way you have it. The UI will update.
You need to make sure your if else is returning correctly.
Try setting the textview to VISIBILE intially to see if it appears and then try to toggle the TextView's visibility
As Pramod says, try
setVisibility(View.INVISIBLE)
instead of
setVisibility(View.GONE)

Value in edittext don't show until it get focus. Android

I'm building an app that get its layout based on external data. The layout is split into different blocks depending on the indata. These blocks are displayed with the help of a viewflipper. In each block there is, for the time being, one or more "textview" and "edittext". On the first page of the flipper all data is showing as it should in the textviews and edittexts. But on the other pages in the viewflipper the value in the edittexts is not showing until the edittext get focused. I have no idea why. So to be clear. The values for all edittexts is actually there, but doesn't show until the edittext get focused. The problem is the same on all devices I have runned the app on (emulator, HTC Desire, HTC Wildfire). Does anyone know how to fix this problem?
Here is the class that generate the layout:
public class ModuleLayout extends LinearLayout {
public ModuleLayout(Context context, ArrayList<BaseModuleItem> itemList) {
super(context);
TableLayout tempTable = new TableLayout(context);
for (int i = 0; i < itemList.size(); i++)
{
TableRow tempRow = new TableRow(context);
TextView tempTextView = new TextView(context);
tempTextView.setText(itemList.get(i).getName());
EditText tempEditText = new EditText(context);
tempEditText.setText(itemList.get(i).getItemValue());
tempRow.addView(tempTextView);
tempRow.addView(tempEditText);
tempTable.addView(tempRow);
}
tempTable.setColumnStretchable(1, true);
this.addView(tempTable);
}
}
Here is a picture of the problem in action.
The left picture displays all its values fine. The right picture is on the second position in the viewflipper and does not display the values in the edittexts, with the exception for the first that have focus. I should also mention that after an edittext has gotten focus it continues to display the value even if I fling to other views in the viewflipper and then back.
That Layout from how it appears here is not on the UI thread meaning no matter what you change you won't see anything until the UI thread (also known as the Main Thread) updates. The UI/Main thread is what all your activities run through so it's easy to think that stuff just updates automatically (since most of the previous work you have done is also somewhere within the Activity, I am assuming). The onFocusChanged event (clicking in the edittext) will update the UI thread by calling invalidate() on itself. You can force it to update by calling EditText.invalidate() if you need to, but it's usually best to have it update naturally after creation. Without seeing the rest of your code this is the best I can do for you, I hope this helps :)
Having not seeing your full code I'm making a bit of a guess-suggestion but have you tried textView.invalidate() (if calling from GUI thread) or textView.postInvalidate() (if calling from another thread)?

Dynamically Displaying Text with TextView (Android)

Basically what I want to do in my Android app is use TextView to display two different pieces of text at once. So in code, I want to be able to do something like this:
LinearLayout ll = new LinearLayout(this);
TextView text = new TextView(this);
text.setTextColor(0xFF000000);
text.setGravity(Gravity.CENTER_HORIZONTAL);
text.setTextSize(20f);
text.setText("Text1");
text.setTextSize(14f);
text.setColor(0xFF0000FF);
text.setText("\nText2");
ll.addView(text);
To clarify, I am trying to display a black "Text1" and a blue "Text2" at once using only a single TextView. Obviously this doesn't work out using the code above. I've considered using a second TextView but that seems like a waste of effort and memory to me. I'm sure the brilliant minds here can come up with the best solution to this.
Thank you very much in advance for your time and your assistance.
There are two options for you.
One is
Spannable
and other is
fromHtml (String source)
So that you can get your desired output.
I think with the current version of the code, you can see only the latest text (Text2).
If you want to have multiple look and feel for two texts, I would suggest use 2 separate TextViews. It would add more flexibility.
If you are not going to change this UI code later, then you can consider Html.toHtml() in setText() call.
It seems the problem is with:
LinearLayout.addView(text);
You are trying to add a view to a LinearLayout, but the layout doesn't exist (in the current activity). You need to add the TextView to a Layout defined in the .xml you are using. Suppose you have a LinearLayout with id "linearlayout01" in the xml file "activity1.xml", you would do something like:
setContentView(R.layout.activity1);
// Create and adjust TextView text
...
LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout01);
layout.addView(text);
Once a View is added to a ViewGroup of which LinearLayout is a descendant you do not need to readd it to update its display. If you preform any changes on a view that requires it to change its display it will handle all the proper notifications about required redraws or relayouts by calling View#invalidate and View#requestLayout where appropriate.
In addition, because all UI changes are handled on the same thread you do not need to worry about calling multiple methods that will update the UI. This is because of two reasons, first, the execution of the redraws will not occur until your code is finished, second, android has optimizations built in that combines multiple invalidate calls into one.
So, the only thing you need to worry about is getting a proper reference to your TextView instance and then you can call all the methods on it that you need to make it display what you wish.
Since you are creating your Views manually and not from xml you need to add your root ViewGroup to the Activity by calling Activity#setContentView.
Edit:
Then you're going to need to learn about SpannableString and SpannableStringBuilder. There is some very brief documentation here: Selecting, Highlighting, or Styling Portions of Text
when do you plan to update the textview ? If it is on click of a button then get a reference to the textview and in the onClickListener() update the text, color, etc whatever you want to do.
After seeing your other comments, I think SpannableString is what you are looking for

Categories