I'm able to break a line using following code:
String str1 = "TEST1"; // length = 5
String str2 = "TEST2"; // length = 5
TextView textView = (TextView)findViewById( R.id.text_view );
textView.setText(str1 + '\n' + str2);
But the final text length is equal to 11.
Question:
Is there any special character or method that will allow me to reach the same result inside my TextView without increasing text length?
What I'm trying to achieve:
I have a data format, which is stored in JSON. It looks like
[{type: line, params: {line params}}, {type: text, params: {text params}, ...]
There is always line at the start
Each paragraph begins with line ( so it acts like a line separator which is stored at the beginning of line, not at the end )
Size of each line equals to 1, i.e. each line counts as a single character
Each paragraph ends with text's last character ( not '\n' )
There are some line params ( like BulletList, Numeric list, Paragraph )
I need a strict mapping between my TextView and source data, i.e. for each cursor position in my TextView I need to count how many characters preceed it in source data.
Take two TextViews and add one below another .Then you won't find any length problem.
like : <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:text="TextView2" />
</RelativeLayout>
String str1 = "TEST1";
String str2 = "TEST2";
TextView text=(TextView)vi.findViewById(R.id.text);
text.setText(str1);
text.append(Html.fromHtml(< br>));
text.append(str2);
Hope it works :)
For your question my answer will be no. But you could make your own TextView and change how it calculates the length of the text by for example ignoring "/n" when counting the length.
Well there is tricky way
String str1 = "TEST1"; // length = 5
String str2 = "TEST2"; // length = 5
textView = (TextView)findViewById( R.id.textView1 );
textView.setWidth(120);
textView.setTextSize(20);
textView.setText(str1 + str2);
//textView.getText().toString().length() length = 10
in XMl
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextView" />
Related
I am new to android development , i am trying to develop an app where user can keep a few text field empty,
However when user doesn't provide any input in the text field app crashes.
How do we handle empty text field in android
Following is my code for text Field.
<EditText
android:layout_width="40dp"
android:layout_height="40dp"
android:inputType="number"
android:ems="10"
android:id="#+id/editText1"
android:layout_weight="1"
android:background="#ffb7ffbf"/>`
java code:
TextView t1 = (TextView)findViewById(R.id.editText1);
a1 = Integer.parseInt(t1.getText().toString());
you should cast EditText instead of TextView.
EditText t1 = (EditText)findViewById(R.id.editText1);
Ensure if the TextBox is not empty before parsing the value to the int as
if (e.length()>0) {
int a1= Integer.parseInt(e.getText().toString());
}
Else you can get a java.lang.NumberFormatException: for Invalid int: "";
Try this:
TextView t1=(TextView)findViewById(R.id.editText1);
String aux = t1.getText.toString();
if(aux.length() > 0)
a1= Integer.parseInt(aux);
else
// the text is empty
getText.toString will bring you something always so it can be and string size 0, wich is empty. that will make the parseInt() throw an error because it won find a number in the string.
So you have to ask if the length of the string > 0, before the parse.
Declaration :
DecimalFormat mAmtFormat = new DecimalFormat("##,##,##,##0.00");
edtAmounts = (EditText) findViewById(R.id.txtAmounts);
From xml File Edit text as
<EditText
android:id="#+id/txtAmounts"
android:layout_height="50dip"
android:layout_marginLeft="10dip"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:inputType="numberDecimal"
android:singleLine="true"
android:textSize="18dip"
android:textStyle="bold"
android:width="170dip" />
From Back End mCurtotamt is 565656565(double)
Fetching Data From Sqlite DataBase:
edtAmounts.setText(String.valueOf(mAmtFormat.format(mDoubleformat
.parse(mCurtotamt).doubleValue())))
but the value set into the edit text as 56,56,57,000.00
what is happening over here.
When you get the value from the back end, do not get it as a String like this:
String mCurtotamt = cursor.getString(cursor.getColumnIndex("column_name"));
Instead, get it as a double directly, like this:
double mCurtotamt = cursor.getDouble(cursor.getColumnIndex("column_name"));
Then you don't need to parse it when you set the edit text, but can format it directly:
edtAmounts.setText(String.valueOf(mAmtFormat.format(mCurtoamt)));
The problem is being introduced when converting it to a String when you call cursor.getString().
Use this one for indian format like this "##,##,##0.00" :
static public String formatCurrency(Double doubleVal) {
return new DecimalFormat("##,##,##0.00").format(doubleVal);
}
This function return the value in correct given format. If you pass 5555555.00 value then function return 55,55,555.00 as a string.
I have problem in my android app:
I write a multiply code like 2223 * 3.456 that it will 7682.688 , my problem is that i don't want that text view display this 7682.688 but I want to display 7682.6.
I know about android:maxlength but when I use this it doesn't display my comment like "foot" beside of it , My question is that how can i limit the calculating of this?
My Textview in xml is:
<TextView
android:id="#+id/foot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="8"
android:textAppearance="?android:attr/textAppearanceLarge" />
and my multiply code in Activity is:
TextView point = (TextView)findViewById(R.id.foot);
if(ft1.getText().toString().length() == 0 ){return;}
int first = Integer.parseInt(ft1.getText().toString());
double equal = first *3.456;
String x = equal+" foot";
foot.setText(x);
you could use DecimalFormat to only display the number of decimals you want.
example
DecimalFormat formatDecimal = new DecimalFormat("#.00");
foot.setText(formatDecimal.format(YourCalculationResult));
This question already has answers here:
Android: TextView automatically truncate and replace last 3 char of String
(4 answers)
Closed 8 years ago.
Following is my textview in android layout and I want that after a certain width of text, it adds '...' at the end of string. I don't want to do it in java while setting the text but want this to be handled by this textview itself. Is it possible?
<TextView
android:id="#+id/list_contact_name"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/list_contact_icon"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:textColor="#222"
android:text="Sandeep Choudhary Mahabali"
android:textSize="18sp" />
add these attributes to your textview :
android:maxLines="1"
android:ellipsize="end"
Add these attributes to your TextView to restrict your TextView's text in one line and to add 3 dots ... at the end of line.
android:singleLine="true"
android:ellipsize="end"
Extend the TextView class, override setText and trim and add your "..." there. If desired, add an attribute that defines how long the text should be before being trimmed, then you can set each TrimmedTextView length in xml as well.
public class TrimmedTextView extends TextView {
#Override
public void setText(CharSequence text, BufferType type) {
int maxLength = 10;
String value = text.toString();
if (text.length() > maxLength) {
value = text.subSequence(0, maxLength) + "...";
}
super.setText(value, type);
}
}
I have a string which has delimiter in it. I want to know the best way to replace the delimiter with a new line. I have had various issues with using the String Tokenizer the main problem being NoSuchElementException. Basically my approach thus far is to retrieve data from a database Once this has been achieved I store each of the records in a string String question = c.getString(1);
Here is the string tokenizer StringTokenizer st = new StringTokenizer(question,"<ENTER>"); I loop through the tokens using a while loop
while (st.hasMoreTokens()) {
System.err.println(st.nextToken());
// quest.setText(String.valueOf(st.nextToken("<ENTER>")));
}
Working example in code
String in = "What is the output of: <ENTER><ENTER>echo 6 % 4;";
in=in.substring(in.indexOf("<ENTER>")+7,in.lastIndexOf("<ENTER>"));
String[] mSplitted= in.replaceAll("<ENTER><ENTER>", "<ENTER>").split("<ENTER>");
for(int i=0;i<mSplitted.length;i++)
{
System.out.println("values: "+mSplitted[i]);
quest.setText(String.valueOf(mSplitted[i]));
}
xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/quest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="TextView" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.08"
android:text="#string/hello" />
<Button
android:id="#+id/Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
output
can't you use split() of String.
String in = "<ENTER>title=Java-Samples<ENTER>" +
"<ENTER>author=Emiley J<ENTER>" +
"<ENTER>publisher=java-samples.com<ENTER>" +
"<ENTER>copyright=2007<ENTER>";
in=in.substring(in.indexOf("<ENTER>")+7,in.lastIndexOf("<ENTER>"));
String[] mSplitted= in.replaceAll("<ENTER><ENTER>", "<ENTER>").split("<ENTER>");
String mFinal="";
for(int i=0;i<mSplitted.length;i++)
{
System.out.println("values: "+mSplitted[i]);
mFinal= mFinal+ mSplitted[i];
}
quest.setText(mFinal);
The reason behind NoSuchElementException is that you are checking whether your Tokenizer has more tokens, but if it does, then you are reading two tokens at the same time.
Documentation says hasMoreTokens tests if there are more tokens available from this tokenizer's string.
If this method returns true, then a subsequent call to nextToken() with no argument will successfully return a token.
That means hasMoreTokens is only sure about the next token but not the token next to that!
Your condition should be..
while(st.hasMoreTokens()) {
String key = st.nextToken();
String val="";
if(st.hasMoreTokens())
val = st.nextToken();
System.out.println(key + "\n" + val);
}
As your condition will work for only even number of elements.