ViewStub - Show view with dynamic content - java

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");

Related

Editing layout xml attributes not displaying changes, but able to by getting reference to the view objects and calling its layout changing methods

Essentially I can't get the View's layout attributes to register on the UI unless I programmatically call, for instance, textViewInstance.setTextColor(rainbow). Any ideas as to why?
Edit: I meant that the following code works:
messageTextView.setTextColor(getResources().getColor(R.color.headline));
BUT doing the following in the view's layout xml does NOT:
#+id/messageTextView
...
textColor="#color/headline"
Try This method...
tv.setTextColor(Color.parseColor("#C10000"));
OR
String rainbow = "#C10000";
tv.setTextColor(Color.parseColor(rainbow));

Programmatically access IntelliJ UI Designer created objects?

I have created a TextView in the UI designer, but I can't figure out how I should access it from the code. I have tried Go To Declaration but that just brings me to the XML file where the TextView is 'made'. Does anyone know how to do this? Help is very much appreciated!
This is independent of the IDE. First you need to "find" the TextView, then you can modify its properties:
TextView myTextView = (TextView) findViewById(R.id.yourid); // The ID is declared in the XML file as android:id atrribute.
myTextView.setText("New Text");
What do you mean by "access it from the code"? If you're talking about navigating from where it's referenced in the code to viewing it in the UI designer, newer versions of Intellij with Android support enabled put tabs at the bottom of the editor when you're editing XML files to let you switch between a text representation and a visual representation of layout files.
If you're talking about how to instantiate the view in code, post some samples of what you've been trying (the most common way is to use a LayoutInflater).
Edit:
Changing the actual text that's displayed in the TextView isn't an IDE-specific issue. You have two ways to do this (well, three if you count the visual and text views of the XML file as separate methods). You can set the text either in the XML file by setting the android:text attribute on the TextView widget, or in the code by calling setText(). Whichever way you decide to do it, you should consider not referring to your text as a raw string but as a String resource as described here.
Edit 2:
OK, you're looking for instructions on how to inflate the view in the first place to get access to it. This is what I answered initially, but here's a little more code. In your Activity (you do have an Activity set up, right?):
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.<your layout ID>, null);
RelativeLayout item = (RelativeLayout) view.findViewById(R.id.<your TextView's id>);

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)

Creating xml template for feed/list items in Android?

I'm helping a friend create an android app that will have screens with lists of info similar to a feed. I've been learning xml layout in Android and have some of the basics down, but don't have a lot of familiarity with doing the java stuff. I've successfully created includes to seperate layout files for compontents within a screen, but what I'm wondering is if such a component can be used as a kind of template for feed/list items that get inserted programmatically on the back end. IE, is there a way to have Android create a list and for each list item it uses the external xml as a template? Sorry if this is somewhat vague, I'm new to this and trying to understand what our options are. TIA!
Yes, every list item can be a custom layout. In fact you always have to define a layout for the list entries. You can either choose a prebuilt one from android.R.layout or you can use your own from R.layout. You can specify it when you create the list adapter in code.
Have a look at one of the ArrayAdapter constructors for example:
public ArrayAdapter (Context context, int resource, int
textViewResourceId)
Since: API Level 1 Constructor Parameters
context - the current context.
resource - The resource ID for a layout file containing a
layout to use when instantiating views.
textViewResourceId - The id of the TextView within the layout resource to be populated
The constructor takes a layout that will be used for the ListView childs. Works similar with other adapters.
What you usually do is inflating the layout inside getView() of the adapter though. When you did that, fill all the data you need into the views of the layout, and return the view.
Note that you get an argument called convertView. This is one of the older layouts you already inflated before. In most cases the user just scrolled down and that entry is not visible anymore. If this convertView is not null, you can fill your data in there instead of inflating the whole layout again (thats expensive).
You can find a working example inside the
ANDROID_SDK\samples\android-10\ApiDemos\src\com\example\android\apis\view\List5.java file. Also take a look at the other list examples in that folder.

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