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.
Related
I would like to get name from values resource file.
for example
values.xml
<string name="ind_ginger">Ginger</string>
<string name="ind_garlic">Garlic</string>
I am using them for the check boxes like
<CheckBox
android:id="#+id/c01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:text="#string/ind_garlic"
android:layout_alignTop="#+id/c02"
android:layout_alignRight="#+id/saveChanges"
android:layout_alignEnd="#+id/saveChanges" />
<CheckBox
android:id="#+id/c02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="81dp"
android:layout_marginStart="81dp"
android:checked="false"
android:text="#string/ind_ginger"
android:visibility="visible"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp" />
In application I need access String Name ( Please note value)
for (CheckBox item : checkBoxList){
if(item.isChecked())
{
//String text=item.getText().toString();String viewID = getResources().getResourceName(item.getId()); // gets me the name
String name = getResources().getResourceEntryName(item.getId());
String tName =
//item.getText().toString();
// String id = item.getTag().toString();
Toast.makeText(getApplicationContext(), tName,Toast.LENGTH_SHORT).show();
Log.d(viewID, TAG);
}
}
Is parsing the XML only way?
Try
String ginger = getResources().getString(R.string.ind_ginger)
Easiest way to get the "KEY" name is as following:
Log.e("KEY_NAME", getResources().getResourceEntryName(R.string.app_name));
Here you will get "app_name" as result.
This can be also help in support multi-language support feature.
You can use this to fetch all the String Keys in string.xml
Field[] fields = R.string.class.getFields();
String[] allStringNames = new String[fields.length];
for (int i =0; i < fields.length; i++) {
allStringNames[i] = fields[i].getName();
Log.e("String Key Name",""+allStringNames[i]);
}
Hope this will help
Just Store you string items in (And its better if you store your string file in in strings.xml and not value.xml )
strings.xml
<string name="ind_ginger">Ginger</string>
<string name="ind_garlic">Garlic</string>
Firstly if you need ind_ginger you need to change your code from this
<string name="ind_ginger">Ginger</string>
to
<string name="ind_ginger">ind_ginger</string>
and
String ginger = getResources().getString(R.string.ind_ginger)
to get the ind_ginger.
But what I can see from your code is you are using .getResourceEntryName(item.getId()) for that you need to create a String arraylist which would have all the itemId from strings.xml and then you can use them with item.get(position).
Here position is the position of the item in your array list.
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.
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));
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" />
I am attempting to collect data from a user in the form of an edittext. The user will input a string and click a button to perform the action below:
public String encode(String s){
String result = "";
String element = "";
HashMap<String, String> translate = new HashMap<String, String>();
//initializing translate
translate.put("A",".-");
translate.put("B","-...");
translate.put("C","-.-.");
translate.put("D","-..");
translate.put("E",".");
translate.put("F","..-.");
translate.put("G","--.");
translate.put("H","....");
translate.put("I","..");
translate.put("J",".---");
translate.put("K","-.-");
translate.put("L",".-..");
translate.put("M","--");
translate.put("N","-.");
translate.put("O","---");
translate.put("P",".--.");
translate.put("Q","--.-");
translate.put("R",".-.");
translate.put("S","...");
translate.put("T","-");
translate.put("U","..-");
translate.put("V","...-");
translate.put("W",".--");
translate.put("X","-..-");
translate.put("Y","-.--");
translate.put("Z","--..");
translate.put("1",".----");
translate.put("2","..---");
translate.put("3","...--");
translate.put("4","....-");
translate.put("5",".....");
translate.put("6","-....");
translate.put("7","--...");
translate.put("8","---..");
translate.put("9","----.");
translate.put("0","-----");
s = s.toUpperCase();
for(int i=0; i < s.length();i++)
{
element = (String) translate.get(String.valueOf(s.charAt(i)));
if(element == null)
result += String.valueOf(s.charAt(i));
else
result += element;
}
return result;
}
If the user hits "enter" on the keyboard of the phone it will insert a newline / carriage return. How can I address this so that it does add a new line? I wouldn't mind using the carriage return as a way to issue the command to change focus OUT OF the edittext area, but if not that then just not allow it to be used at all.
You should use android:singleLine="true" in your EditText's XML tag, like this:
<EditText
android:id="#+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
This will make the enter button change the focus to the next view in your UI, rather than inserting a new line in your EditText.
I would put this in the comments but I think my reputation is still not high enough to add comments.
Since android:singleLine="true" is now deprecated, another option is to use android:inputType="text". It's a slight change to #Tiago_Pasqualini's answer:
<EditText
android:id="#+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
/>
This too will make the enter button change focus to the next view in the UI and skip entering a new line into the EditText.