setText - cannot resolve method - android

Pretty sure this is a stupid question but I can't figure this out.
I am trying to get an integer returned by a datepicker to a string. This code works where day is the integer of interest
dateButton = (Button) findViewById(R.id.dateButton);
dateButton.setText((Integer.toString(day));
This code gives me the error that cannot resolve method setText
String yearString = "";
yearString.setText(Integer.toString(year));
I don't understand why I cant convert the int to a string unless I use a view?

Is this, what you want to do.
int year = 2014;
String yearString = Integer.toString(year);
Because stText mthod is only for setting text on certain views on android likeTextView, EditText, Button.

You can set integer value by following ways if day is an integer value,
dateButton.setText(day+"");
or by
dateButton.setText(String.valueOf(day));
or
dateButton.setText(Integer.toString(day));

dateButton = (Button) findViewById(R.id.dateButton);
dateButton.settext(Interger.Valueof(day));

you have to instantiate the object before you call it view I am new to this also
So this
((TextView) findViewById(R.id.now_playing_text)).setText(trackTitle)
Becomes
TextView Title = (TextView) findViewById(R.id.now_playing_text);
Title.setText(trackTitle);
setText must be applied on an a class that contains in it or it supper classes the setText method.

Instead of
dateButton.setText((Integer.toString(day));
Try this
dateButton.setText(day+"");

Related

SetText in EditText Android error

I want to setText in EditView but it will error. I dont know why.
This is on OnCreate function.
name = (EditText) findViewById(R.id.input_name);
String editname = String.valueOf(getName());
MessageTo.message(this,editname);
name.setText(editname);
The string i will set in EditText name.
public String getName(){
int id = 1;
String data=dbhandler.getName(id);
return data;
}
If i remove this code from the OnCreate function
name.setText(editname);
It will have no error. The MessageTo.message code is where it pops a dialog of the String editname. It will show the String editname.
So i put back the code and change it into this code,
name.setText("testing");
But it still won't work.
I dont know why though. All other sources have EditText.setText("String") as how to setText in EditText but it won't work in my case.
LOGCAT ERROR:
09-20 21:00:36.677 1436-1436/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.organizer/com.organizer.ManageProfileActivity}: java.lang.NullPointerException
Your EditText name appears to be null. In your onCreate method, make sure you are calling setContentView(R.layout.YOUR_LAYOUT) where the layout parameter is an XML layout containing your EditText with the ID R.id.input_name.
You assign it later on with
name = (EditText) findViewById(R.id.input_name);
but if the layout does not contain this ID, name will be null, thus causing your error.
It is correct that you should be using the setText() method on the EditText.
Everything looks correct in terms of your codes, so I would suggest to check for the following things:
Are you calling setText anywhere else?
Is the id of your EditText correct (Is the id of your EditText input_name)
Are you sure you are looking at the right activity.
Are you initializing your name variable in the right scope? (Outside the onCreate Method)
Declare the EditText in the xml file
Find the EditText in the activity
Set the text in the EditText
And If you check the docs for EditText, you'll find a setText() method. It takes in a String and a TextView.BufferType. For example:
EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("Google is your friend.", TextView.BufferType.EDITABLE);
Or
Use +, the string concatenation operator:
ed = (EditText) findViewById (R.id.box);
int x = 10;
ed.setText(""+x);
or
String.valueOf(int):
ed.setText(String.valueOf(x));

Create a new random number each time an activity is opened

I'm doing a simple addition game on android studio. Every time the "addition" activity is opened I was hoping to generate two random numbers in two text boxes. However I can't get it to work and the text box appears blank every time I run the app and open the activity. Here's my code for one of the text boxes.
public void textview2(View View) {
Random addition1 = new Random();
int additionint1 = addition1.nextInt(100)+1;
TextView additionText1 = (TextView) findViewById(R.id.textView2);
String additionString1 = String.valueOf(addition1);
additionText1.setText(additionString1);
}
Change
String additionString1 = String.valueOf(addition1);
to
String additionString1 = String.valueOf(additionint1);
You are missing something here I believe,
In onCreate method, you should do something like this..
Random addition1 = new Random();
int additionint1 = addition1.nextInt(100)+1;
TextView additionText1 = (TextView) findViewById(R.id.textView2);
String additionString1 = String.valueOf(additionint1);
additionText1.setText(additionString1);
but the point is, it should be in onCreate so that when your activity is created, the number is generated and .setText for your required textView is called...
you can also consider the same if you want to handle other activity states..
PS: Note that you can have this code folded in function and called in overrided method onCreate
also, note String.valueOf(additionint1);

How to take in two different values in android?

basically what I've tried, is to create a XML with two input types decimal. Input A and Input B, and when I click add, these values will get passed onto a class method, and that method will do so and so.
The problem I have is that I have no way of passing the value from an XML file to a java file (I think)
I spent like an hour doing the XML thing only to find out I can't do it.
Can you possibly guide me as to how I might go about doing this?
EditText mEditText1 = (EditText)findViewById(R.id.number1i);
String val1 = mEditText1.getText().toString();
EditText mEditText2 = (EditText)findViewById(R.id.number2i);
String val2 = mEditText2.getText().toString();
Your XML contains two editText and one button?
in your java file you should do:
EditText e1 = (EditText)findViewById(R.id.e1);
EditText e2= (EditText) findViewById(R.id.e2);
and in xml set onClick attribute on button (example onClick="onClick")
in code:
public void onClick(View v){
e1.getText.toString(); e2.getText.toString(); //save it to string and
you have what you want}

How to refresh a view component run time in android

I want to change the text of Text view pragmatically in my android application, here is my code to set the string to the text view
TextView dateTime;
dateTime = (TextView) findViewById(R.tasksheetlist.txtdatetime);
private void updateDateValue(String date)
{
String text = dateTime.getText().toString();
text = text+"\n"+date;
dateTime.setText(text);
}
and this is how the function called some where
Date date = new Date();
String dateStr = Helper.ConvertDateStringFromDate(date, "DD MMM");
updateDateValue(dateStr);
This code called but does not reflected on the view.
I think I need to refresh the layout but do not know how?
Please help me in this??
I think this article will help you. http://android-developers.blogspot.com/2007/11/stitch-in-time.html Basically you want to use the Handler class provided in the SDK. You shouldn't need to call invalidate like because setText does that for you. Hope this helps!

How to get EditText value and display it on screen through TextView?

I want to get the user input for the EditText view and display it on the screen through TextView when the Button is clicked. I also, want to know what modifications can be done on the string.xml file to do this.
I didn't get the second question, maybe you can elaborate...but for your first query.
String content = edtEditText.getText().toString(); //gets you the contents of edit text
tvTextView.setText(content); //displays it in a textview..
I'm just beginner to help you for getting edittext value to textview. Try out this code -
EditText edit = (EditText)findViewById(R.id.editext1);
TextView tview = (TextView)findViewById(R.id.textview1);
String result = edit.getText().toString();
tview.setText(result);
This will get the text which is in EditText Hope this helps you.
EditText ein=(EditText)findViewById(R.id.edittext1);
TextView t=new TextView(this);
t.setText("Your Text is="+ein.getText());
setContentView(t);
bb.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
String s1=tt.getText().toString();
tv.setText(s1);
}
}
);
First get the text from edit text view
edittext.getText().toString()
and Store the obtained text in a string, say value.
value = edittext.getText().toString()
Then set value as the text for textview.
textview.setText(value)
yesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
eiteText=(EditText)findViewById(R.id.nameET);
String result=eiteText.getText().toString();
Log.d("TAG",result);
}
});
Easiest way to get text from the user:
EditText Variable1 = findViewById(R.id.enter_name);
String Variable2 = Variable1.getText().toString();
in "String.xml" you can notice any String or value you want to use, here are two examples:
<string name="app_name">My Calculator App
</string>
<color name="color_menu_home">#ffcccccc</color>
Used for the layout.xml: android:text="#string/app_name"
The advantage: you can use them as often you want, you only need to link them in your Layout-xml, and you can change the String-Content easily in the strings.xml, without searching in your source-code for the right position.
Important for changing language, you only need to replace the strings.xml - file
Use the following code when clicked on the button :
String value = edittext.getText().toString().trim(); //get text from editText
textView.setText(value); //setText in a textview
Hope to be useful to you.
Try this->
EditText text = (EditText) findViewById(R.id.text_input);
Editable name = text.getText();
Editable is the return data type of getText() method it will handle both
string and integer values
First get the value from edit text in a String variable
String value = edttxt.getText().toString();
Then set that value to textView
txtview.setText(value);
Where edttxt refers to edit text field in XML file
and txtview refers to textfield in XML file to show the value

Categories

Resources