I am trying to do the following :
t1 is an edit text
a and b are two integers
t1.setText(a+b);
but this is not working in android but it works perfectly with javaswing
A simple solution would be:
t1.setText("" + (a+b));
or maybe:
t1.setText((a+b).ToString());
please do like the following
EditText et=(EditText)findViewById("ID of your EDITTEXT");
int c=a+b;
String s=c.toString();
et.setText(s);
This'll work fine :) If it works vote me :)
but this is not working in android but it works perfactly with
javaswing
It will not, because you're actually calling setText(int resId). See here. Calling this method will search a string in xml resources (E.g. strings.xml). Every id of a string (E.g. #strings/hello) will be compared to resId, If resId matches the string's id that string will be displayed to that widget or else you wil get a ResourceNotFoundException
To display the actual integer, convert it first to String
t1.setText(String.valueOf(a+b))
You are doing like this t1.setText(a+b); it search this id (a+b) in resource file like strings.xml that is not available. So it will throws a exception ResourceNotFoundException..
So to set the number in text view you need to convert it into string.
In Android you need to do like this:-
int sum = a + b;
String sumString = String.valueOf(sum);
t1.setText(sumString);
OR
t1.setText((a+b) + "");
Related
I want to put extra value from intent to other intent. But in other intent, app get all value. Example:
mAddress.setText(" from " + address);
String put_address = mAddress.getText().toString();
editIntent.putExtra("put_address", put_address);
is it possible to cut text "from" and get only address variable ???
you can split a string like
str = "From address#dd.com";
String modified = str.replace;
now splitstr contain your split strings
splitStr[1] contains "address#dd.com"
Can also use
str.substring(str.indexOf(" ")+1);
By the way, you can use jagapathi's answer. In his example he uses regular expression.
Regular expressions can help to parse, find, cut substrings using a particular pattern. In his code he splits string by any space character.
But, imho, the simplest solution is to create a substring using this code:
'put_address.substring(7);'
use one of these solutions:
String input = put_address.trim().substring(5);
*** note: 5 is index of real address first character;
String input = put_address..split(" ")[1];
I want to print ŒHI 5¹ in my TextView. So I have simply written :
tv.setText("Welcome to " + R.string.app_name)
where R.string.app_name is <string name="app_name"><b>ŒHI 5¹</b></string>
But the strange thing is textview is showing a number
The number is: 2131230755
I have no idea why this is happening.Please help.
use getString(R.string.app_name)
or
getResources().getString(R.string.app_name);
R.string.app_name is just a long number genereted to identify that resource. The result of string + long is just that long concatenated to that string. so you need to get the string corresponding to that identifier.
Actually every resource (layout, drawable, array, string) gets an identifier, these are put in the R file. layout identifiers are kept together in an inner class called layout, strings in string and so on.
use :
getString(R.string.app_name);
or if you are not in an activity then
mContext.getResource().getString(R.string.app_name);
You can't call direct R.string for your requirement .
Pass getResource().getString
Returns the string value associated with a particular resource ID
Finally
getResource().getString(R.string.your_string);
This may seem like a stupid question, but for some reason i just can't figure it out- i have a few textViews i add text from an Object all the string fields are added fine. but when i try to add a int to the textView it crashes my application.
heres my code
firstLineOfAddress.setText(addline);
town.setText(town2);
county.setText(county2);
postCode.setText(post);
// the three strings up above work fine if i comment the three below out
// ask , current, done are all ints
askingPrice.setText(ask);
currentOffer.setText(current);
doneUpValue.setText(done);
set like this:
textView.setText(10+"");
In your code replace this:
askingPrice.setText(ask);
currentOffer.setText(current);
doneUpValue.setText(done);
with
askingPrice.setText(ask+"");
currentOffer.setText(current+"");
doneUpValue.setText(done+"");
The problem is that you have to convert your int to String. To convert your int to String, use :
Integer.toString(myInt)
or
String.valueOf(myint)
So it would be askingPrice.setText(Integer.toString(myInt))
when you add any Integer number the call this...
textview.setText(""+value);
OR
textview.setText(String.valueOf(value));
where value is a Integer.
You have to set the text value as a String. Convert it as so:
askingPrice.setText(String.valueOf(ask));
currentOffer.setText(String.valueOf(current));
doneUpValue.setText(String.valueOf(done));
try to convert it to a String perhaps .
askingPrice.setText(String.format("%d",ask));
well trying above scenario like this
int text=0;
TextView tv = (TextView)findViewById(R.id.test);
tv.setText(text);
throws exception as
(26955): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.highactivity/com.example.highactivity.MainActivity}: android.content.res.Resources NotFoundException: String resource ID #0x0
because while passing integer in set text it takes as resource id.At run time search for resource id =0 but there wont be any resource id like that so it throws exception will leads to app crashes.convert int to string if you need to display integer
I am having trouble with setting text in a text view with format and multiple values.
holder.car.setText(R.string.mycar + lm.getCarName() + R.string.year + lm.getYear());
this is giving me " 2143545 Camero 2143213 1977 "
I have tried few other "solutions" from the web
holder.car.setText(getString(R.string.mycar) + lm.getCarName() + getString(R.string.year) + lm.getYear()); << not work, getString undefine>>
I even tried String.valueOf(R.string.mycar); getResources().getText(R.String.mycar), still it didn't work.
It would be great if someone can help me, thanks
Try this
holder.car.setText(getResources().getString(R.string.mycar));
I think you're trying to use parameters in your string.
Try this:
<string name="mycar">Car: %1$s Year: %2$s</string>
String mycar = getString(R.string.mycar);
mycar = String.format(mycar, lm.getCarName(), lm.getYear());
You should get:
Car: Camaro Year: 1977
If you want to set your textview just a string from your string.xml file,
mytextview.setText(R.String.mycar);
If you want to set your textview with combination of some strings or integers, (better than first way)
int number=5;
mytextview.setText(getResources().getString(R.String.mycar) + " " +number + " " + getResources().getString(R.String.mysecondcar));
R.string.mycar and R.string.year are only IDs for resources. For this reason you get the numbers (IDs are numeric).
To get string from resources you need to use this construction:
String myCar = getResources().getString(R.string.mycar);
and now the myCar variable holds the string you put in strings.xml file under the mycar name.
the method getResources() belongs to Context. If you run your code outside an Activity, use the context instance to get the string, like this:
String myCar = context.getResources().getString(R.string.mycar);
Try this. If you're fetching string in class without extending Activity Get using your Context
holder.car.setText(context.getResources().getString(R.string.mycar));
If you're extending Activity
holder.car.setText(yourActivity.this.getResources().getString(R.string.mycar));
Hope this helps you..
You have to retrieve your resources first, and the call the medthod getString(int), not getText, has you have put.
So, it should be:
getResources().getString(R.String.mycar);
The R class contains kind of pointers to your ressources, so you can not directly use them, use getResources().getString(), as others say.
I faced an interesting problem today. I have 4 strings which I need to show on app on random basis. So I simply added the strings to my string.xml and was setting my textview to show the text as
textView.setText(R.string.text_1);
or (if random number was 2)
textView.setText(R.string.text_2);
and so on
I observed that the change is just in last character, so tried using reflection
Class c = Class.forName("com.startpage.mobile.R$string");
Field field = c.getDeclaredField("text_"+num); //num is 1/2/3/4
System.out.println("********** "+field.get(null));
Now the field.get(null) actually return the Id (hexadecimal number in R.java) value instead of string value I would expect.
Is there a way to fetch actual value of string using reflection or this is something in android which I will have to live with?
getResources.getString(resourceId);
R.string.text_2
will always return the hex number. To really get the string value, you have to try the following
MyActivity.this.getResources.getString(R.string.text_2);
You can simply request your resource ID from the resource manager:
Class c = Class.forName("com.startpage.mobile.R$string");
Field field = c.getDeclaredField("text_" + num);
int resId = (int)field.get(null);
String text = this.getResources().getString(resId);
textView.setText(text);
I suggest you to use array of strings and choose random item from it