I just want to add a new line somehow to my linear layout:
layout = (LinearLayout) findViewById (R.id.layout);
... //some other code where I've appended some strings already
final TextView nline = new TextView(this);
nline.setText(Html.fromHtml("<br>")); //i also tried: nline.setText("\n");
layout.addView(nline);
But this just adds a few spaces. Can someone help me out? Thanks.
First you need to make your TextView to be multiline. And then use simple "\n" string for linebreak.
final TextView nline = new TextView(this);
nline.setSingleLine(false);
nline.setText("first line\n"+"second line\n"+"third line");
If you just want to have some empty space between two other views, you could do this in your XML (assuming you're using XML for the layout). Something like this could work, basically putting in a View with a transparent background and given height. This is assuming you have whatever parameters you want in your TextViews.
<TextView />
<View android:background="#00000000"
android:layout_height="12dp" //or whatever density pixel height you want
android:layout_width="fill_parent" />
<TextView />
Also, in what you tried above... you could try a space and newline... that might work.
nline.setText(" \n");
You may need to set the InputType to TYPE_TEXT_FLAG_MULTI_LINE using the setInputType() method of TextView
tv.setInputType(tv.getInputType()|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
You also could just set some margin/padding on the other views. I think that TextViews should not be misused as spacers.
Simple as:
String hello = getResources.getString(R.string.hello);
String world = getResources.getString(R.string.world);
textView.setText(hello + "\n" + world);
Simple by giving "\n" data is displayed in a new line
txtView.setText("Latitude :" + latitude + "\nLongitude :" + longitude);
Related
So in my TextView I have several phone numbers separated by slash sign like this for example:
6723098 / 52378529 / (021)854745
Now when I used android:autoLink="phone", only the first number is clickable to automatically dial, how do I set that all three numbers to be clickable and callable?
Thx in advance.
Java
public class ShowRestActivity extends Activity {
String rest_tel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_rest);
TextView restTelLabel = (TextView) findViewById(R.id.restTelLabel);
restTelLabel.setText("6723098 / 52378529 / (021)854745");
Linkify.addLinks(restTelLabel, Patterns.PHONE, "tel:");
}
}
xml
<TextView
android:id="#+id/restTelLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginBottom="10dp"
android:layout_below="#+id/restNameLabel"
android:autoLink="phone"/>
These are a portion regarding the phone number..
Solution
Remove android:autoLink="phone" from the TextView's attributes.
try this...
TextView textView = (TextView) findViewById(R.id.textviewid);
textView.setText("6723098 / 52378529 / (021)854745");
Linkify.addLinks(textView, Patterns.PHONE, "tel:");
Why not generate 3 (N) TextViews from the input by tokenizing them. Seems harmless to do, numbers may be limited only to the first number found.
I am sure that there is a lot of material on the web about the problem I am having, but since I am new to Android app development, I don't really know how to form the question (don't know the terms).
There are two buttons and a textView in my app:
<TextView
android:layout_width="50dp"
android:layout_height="100dp"
android:text="P1"
android:id="#+id/history"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
When the buttons are pressed, a string is appended to textView (in a new line):
switch (v.getId()) {
case R.id.button1:
counter -= 20;
stack.push(20);
history.append("\r\n " + seznam.peek());
break;
case R.id.button2:
counter -= 1;
stack.push(1);
history.append("\r\n " + seznam.peek());
break;
default: break;
}
At the moment, when there is not enough space for new lines (100dp), only the old ones are visible, the new ones are not.
I would like, that the newest line is always visible, and that I had the option to see the old lines(via swipe).. (something like iframe without scrollbar)
You don't need to use a ScrollView actually.
Just set the
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
properties of your TextView in your layout's xml file.
Then use:
yourTextView.setMovementMethod(new ScrollingMovementMethod());
in your code.
It scrolls automatically without any issues.
I'm having an issue with an textview in which i have some text that is supposed to be left aligned and some text that is supposed to be right aligned.
Here´s my attempt.
String LeftText = "LEFT";
String RightText = "RIGHT";
String resultText = LeftText + " " + RightText;
SpannableString styledResultText = new SpannableString(resultText);
styledResultText.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), LeftText.length() + 1, LeftText.length() + 2 + RightText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(styledResultText);
And here´s the xml for the textview.
<TextView
android:id="#+id/titleBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
However this is not working. All the text is getting left aligned. What am I missing here?
You can use TabStopSpan to solve the problem pretty easily if rightText is doesn't need to be right justified against the edge.
final TextView tv = ...
int column = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 200.0, getResources().getDisplayMetrics());
SpannableStringBuilder textspan = new SpannableStringBuilder("Charges\nPrice\t$25.00\nShipping\t$155.08\nTax\t$5.11\n");
textspan.setSpan(new TabStopSpan.Standard(column), 0, textspan.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(textspan);
if you want it moved near to the right edge of your text view you can set the Tab stop after layout using a global layout listener. Then you can use the width of the textview to set a tabstop near the edge.
If you want it right aligned you just need to make a class extending ReplacementSpan that will justify the text for you..
I've gone down this road myself...unfortunately, from my research I don't believe it's possible to have two alignment spans for a single line of text. You'll have to split it out into two different TextView objects.
I have a TextView within a ScrollView, which currently scrolls to the bottom of the TextView.
The TextView is filled dynamically constantly updating (the TextView is essentially acting as an actions console).
However, the problem I am having is that when the dynamic text is added to the ScrollView, the user can scroll past the text into black space, which is increasing everytime more content is added to the TextView.
I have tried various different apporaches however none of these gave the right outcome. I cannot use maxLines or define height of the layouts as I need this to be dynamic for the various screen sizes, which the number of lines visible constantly changing.
I had also orginally done this progromatically, however this was crashing at random time and therefore would like to keep it in my layout (better usabilty), example code below:
final int scrollAmount = update.getLayout().getLineTop(update.getLineCount()) - update.getHeight();
if(scrollAmount > 0)
{
update.scrollTo(0, scrollAmount);
}
The code below is my current layout xml being used to automatically scroll my TextView to the bottom as content is added:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/spacer2"
android:layout_below="#+id/spacer1"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/battle_details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12dp"
android:layout_gravity="bottom" />
</LinearLayout>
</ScrollView>
EDIT - This is the code I am using to add text to my TextView:
private void CreateConsoleString()
{
TextView update = (TextView)findViewById(R.id.battle_details);
String ConsoleString = "";
// BattleConsole is an ArrayList<String>
for(int i = 0; i < BattleConsole.size(); i++)
{
ConsoleString += BattleConsole.get(i) + "\n";
}
update.setText(ConsoleString);
}
EDIT 2 - I add content to the BattleConsole like this:
BattleConsole.add("Some console text was added");
CreateConsoleString();
To sum up my only issue is the ScrollView and/or TextView is adding blank space to the bottom rather than stop the user from scrolling at the last line of text. Any help or guidence as to where I am going wrong would be much appreciated.
It looks like that when you call
BattleConsole.get(i)
it sometimes returns an empty String so you are just basically adding new lines to your TextView.
You can do this for example:
StringBuilder consoleString = new StringBuilder();
// I'm using a StringBuilder here to avoid creating a lot of `String` objects
for(String element : BattleConsole) {
// I'm assuming element is not null
if(!"".equals(element)) {
consoleString.append(element);
consoleString.append(System.getProperty("line.separator")); // I'm using a constant here.
}
}
update.setText(consoleString.toString());
If you could post the code of BattleConsole I could help you more.
As a footnote: it is encouraged to use camelCase in java. Only class names start with capital letters in java according to the convention.
Alright, I'm trying to create textviews dynamically with strings i have in an array. Everything works right now besides when i create the textviews instead of them going down they each stay on the same line and run off the screen. I want each textview i create under the next. Code Below works just need it to create under the next instead all on one line.
public void GenList(){
DataBase entry = new DataBase(this);
entry.open();
String data = entry.getData();
int datanumber = entry.FindShit();
if(datanumber == 0 || datanumber == 1){
setContentView(R.layout.nowordlist);
}else{
int length = entry.results.length;
View linearLayout = findViewById(R.id.sayLinear);
for(int i=0;i<length;i++)
{
TextView value = new TextView(this);
value.setText(entry.results[i]);
value.setId(i);
value.setTextSize(50);
value.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(value);
}
}
}
You'll need to change the orientation of the LinearLayout (R.id.sayLinear) you're adding the TextViews to. By default the orientation is set to 'horizontal', which will make the TextViews appear next to each other on a single line. Try changing it to 'vertical':
<LinearLayout
<!-- other attributes -->
android:orientation="vertical" />