I just want to append a TextView element to my linearlayout in order to add a newline to the screen the users see on the phone. I tried:
final TextView newline = new TextView(this);
newline.setText("\n");
main_layout.addView(newline);
... but to no avail. In fact this code made my phone vibrate angrily for some reason.
Help much appreciated!
EDIT - typo removed, and main_layout is my linearlayout element to which I'm appending content (strings).
I'm a newbie myself, so maybe I'm missing something obvious here, but shouldn't your code read:
newline.setText("\n");
Or you could just do:
textView.append("\n");
to append a /n to an existing TextView element
First you need to add the TextView to your layout with the add() function. Better practice is to create a layout xml file for your view. It is much easier to work with.
And how is newLine getting '\n' text if you setting it on title_response?
And angry buzzing usually indicates that your application crashed. Welcome to the club :)
Try passing to the ctor of the TextView an AttributeSet as second parameter, setting the "lines" attribute. Just in order to make sure that the TextView is not created as "signle-line",
Related
i want to make a program. I have 16 textviews that includes letters. And i want to add small text to every textview -programmatically-. Please refer to the pictures..
firstly --> picture
after adding --> picture
You may do this:
Instead of getting textviews into the program, you can create a layout file which contains 2 text views. You may need to use "layers" to do this. One for big text, other one for the little text which contains the reduced size texts. Then inflate the layout file to a view by an inflater. Then use that view instead of textview. So you have little numbers on you table now.
I don't know about RecyclerView, may be that can be a better solution but as i said i dont know it so here is my answer, good luck at your project!
<TextView
android:text="123456789"
android:autoLink="phone">
</TextView>
I want to create this TextView from code, however I am encountering countless problems.
In the first place, I got halfway creating a TextView and adding this:
tw_phone.setAutoLinkMask(0x04);
This resulted in a clickable TextView, but when you clicked, a toast said "No application can perform this action", or something similar. I also tried with
Linkify.addLinks(tw_phone, Linkify.PHONE_NUMBERS); //and .ALL
but it gave me the same result.
When I decided to ask on StackOverflow, I tried to strip my code down incase there were something wrong with the way I have used Layouts (you never know), but now I'm not even able to make a TextView clickable. This is the code that, in my opinion, should work as it is just a stripped down version of what I am using deeper in my code.
TableLayout table = (TableLayout) findViewById(R.id.tableResult);
TableRow row = new TableRow(this);
TextView tw = new TextView(this);
tw.setText("123456789");
tw.setAutoLinkMask(Linkify.ALL);
row.addView(tw);
table.addView(row);
Can someone write a simple, small example of how you create a TextView, give it a number as text and then allows the user to click on it and choose whatever app they want to open the number with??
If you can point out whats wrong in my code aswell, that would be great, but I would much rather just get the answer straight away. The things I have tried are taken from other StackOverflow questions and answers.
TextView tv_contatti2 = new TextView(this); tv_contatti2.setText(contatti);
Linkify.addLinks(tv_contatti2, Linkify.PHONE_NUMBERS);
tv_contatti2.setLinksClickable(true);
where "contatti" has value +39012345678 with international prefix
In order to make text view clickable with any url try following code :
Linkify.addLinks(textView, Linkify.WEB_URLS)
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>);
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");
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