bted.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String textoDolars=dolars.getText().toString();
Double dolars=Double.parseDouble(textoDolars);
Double euros=dolars*1.19;
String textoEuros=String.valueOf(euros);
euros.setText(textoEuros);
}
});
In this part euros.setText(textoEuros);
can not resolve method 'setText(java.lang.String)'
How can ı handle this situation?
I tried another type of setText but ı couldnt find truth.Can you help me?
Since you haven't provided us with full code I cannot determine the correct answer but here are couple of possibilities.
Set your command to the following textview.setText("sometext")
You have to use "" when you are going to declare strings.
Dolars is not a textview
Is TextODolars a variable? If so, what kind? Maybe you have to use .toString to convert it
But please get into a habit of posting code for a clear answer
You are calling set text to a Double variable.
From your code: euros.setText(textoEuros);
Doubleis a wrapper class for the primitive type double and it does not have a method called setText.
You have a confusion there you probably are looking for a TextView on any subclass of it.
I think you wan to display the value on the screen(?).
First you need a TextView.
<TextView
android:id="#+id/output"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Now you can access it with some code to display the wanted value on the screen.
TextView output = (TextView)findViewById(R.id.output)
//call setText
output.setText(textoEuros);
Related
Sorry if this is a really obvious question, but I've been trying to find something like it in a number of places, and I'm not sure if it just doesn't exist or if I'm using the wrong language to describe what I need.
If, for example, I have a number of TextViews in different parts of my activity, and I want all of them to open the same activity when clicked on, how would I do this without specifying each of their IDs individually in an if statement in the Java? In HTML I would use a class attribute to group them together, but I can't find a similar feature in XML.
Thanks for your help!
If your intention is to prevent code repetition and tedious code repetition, then one way is to use ButterKnife. Something link below would work:
#OnClick({R.id.view_id1, R.id.view_id2, R.id.view_id3})
public void onClick(View view) {
// TODO: Handle click
}
Or, as the other answer suggests, you can have android:onClick attribute on each of those views which points to same method in Java code.
Write your xml code of EditText as follows
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Fourth EditText"
android:onClick="editTextClick"/>
And in Java file write method
public void editTextClick(View v) {
// does something very interesting
Log.d("MainActivity","EditText pressed");
}
I hope this will help you,
Happy Coding
Kindly help me to resolve this error as I'm new in this field and very much eager to know about it.
Its not button.OnClickListener(Listener). It's button.setOnClickListener(Listener)
Use mShowFactButton.setOnClickListener(Listener);
And you must follow some standard -
All reference should be start with small letter(Listener should be listener).
A function name always start with an small letter.
On stackoverflow use to copy paste your original code rather than attaching screenshots.
You should use
mShowFactButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Let me explain:
I need to show the name of any building block either it is imagebutton, edittext in my textview field depending upon which of above written will be hover over by the user.
So that my textview could behave like some dynamic display plate.
Any help will highly be appreciated.
mrana..
So something that you can do. Since there it no "setText" for imageviews, you can do something like
String name = "imageview";
imageView.setTag(name);
Then in your onFocusedChangedListener call the following method
void displayInTextView(View selectedView) {
String viewName = (String) selectedView.getTag();
mDisplayText.setText(viewName);
}
Since this is a touch device, "hovering" will not be possible. One solution is to show the name on when long-press. See this solution https://stackoverflow.com/a/4433441/1227692
EDI: Thanks Frank and mrana for pointing out. I agree and take back my comment.
This is week one of Android for me!
I'm programmatically creating a textview, two labels and a button in the same activity.
The idea is that the textview receives a string from the user.
The user clicks the button and the textview string is passed to a proc which returns a string result, and the string result is assigned to one of the labels.
public void onClick(View view) {
sresults = showPP(ttsymbol.getText().toString().trim());
}
But this doesn't work, because "the final local variable sresults cannot be assigned, since it is defined in an enclosing type"
I think I understand what is going wrong, but is there an alternative way of returning the results so that they can be displayed in the label? Or is it necessary to create a new dialog inside the onClick function to show them?
Thanks!
If you are sure of changing the value of sresults, then there is no point in making it final. final is used for constant values. You should directly set the value to the label view. The label view reference can be final.
public void onClick(View view) {
label.setText(ttsymbol.getText().toString().trim());
}
More code would be helpful here, how is sresults, showPP, etc defined? However, the message suggests you have defined sresults as "final" which means you can't change the value. Probably removing final would solve that problem.
I am trying to set the score in a game but it isn't doing anything at the moment.
I have declared a score variable:
static int score = 0;
When an answer is shown in another textview as "Correct" I want the score to increment and show this in another textfield which will be for the score.
So far I have tried this:
public void score(){
check.getText();
if(check.equals("Correct")){
score++;
Score.setText(String.valueOf(score));
}
check is a Textfield which shows Correct or Incorrect. Score is another textfield.
and then I put the method score() into an onclick, but this doesn't update the textfield.
I would appreciate any advice on this.
Thanks
Edit:
check.equals("Correct")
should be
check.getText().equals("Correct")
You don't heed the check.getText() method call that you have by itself. And you need to use this for your if statement
if(check.getText().toString().equals("correct))
getText returns the object type Editable which is not a String. If you are getting a force close please edit you question to include the stack trace from the LogCat output.