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}
Related
So I have 6 edit texts and a button as shown below:
My question is how do I use the input from the EditTexts (which I have stored in content_main.xml) to do mathematical operations like calculating an average which I want to show up in a toast when the calculate button is pressed. I have already written some code in the MainActivity.java file that brings up a toast when the calculate button is pressed (also in content_main.xml), I just need to figure out how to use the inputs from the EditTexts in the toast.
EditText myText // = findViewById...
String text = myText.getText().toString();
What you should do first is to give each of its elements ID to also recognize from the Activity.
Then you should use the click event of the button
//Here it is referring to the id that gave his element in its layout
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
And finally, like the button, get their input values EditText
//Here it is referring to the id that gave his element in its layout
EditText text = (EditText)findViewById(R.id.editText01);
And in order to do math, parse the string value remaining on a double (double for decimals can give the exact calculation if you want something, if you want to be an int approximately)
try{
Double value = Double.parseDouble(text);
}catch(NumberFormatException e){
//Message for error parse
}
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));
In the /res/values folder of my android project i have a string and that is referenced in a text view in my xml file, i want to change the string in my java file.
As you can see below in the code i have made a string variable and then below that i have set what the string variable is set to, which is where the string is located. where i have "here" posed in the code that's where i want to change to string in the values folder. but i don't know what code to use to set it.
I could just change the text in a text view from my java file, which i know how to do, but that is an old way and it sets of a warning so i would rather use a string which is the best way to do so.
With my knowledge of changing text in a text view i have basically guessed my way to this stage but i don't know how to go any further could any one give me some advice on what to do, thanks.
String string;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
counter = 0;
add = (Button) findViewById(R.id.badd);
sub = (Button) findViewById(R.id.bsub);
reset = (Button) findViewById(R.id.breset);
display = (TextView) findViewById(R.id.tvdisplay);
string = (String) getString(R.string.counter);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
((///////////////here////////////////))
counter++;
}
});
You cannot modify the text assigned to <string> elements of a /res/values/strings.xml file at runtime. They're constants so effectively final.
You also cannot change a layout xml file at runtime. If you've created a layout with a TextView that has its android:text attribute set to some initial resource string, that is basically an 'initial' value and cannot be changed to something else at runtime.
You told us a lot of changing text, but you don't said what the text should be. I need to guess, too:
The strings.xml file should be used for texts that might change for different languages. If you just want to change the text of a counter, you shouldn't do it via strings.xml as the numbers are universal :)
Try to go with that:
display.setText(String.valueOf(counter));
You will want to use the setText() method.
display.setText("text");
So, today I decided to try out Android, so please understand that I am a beginner in it.
What I want to achieve right now is to have a EditText, and a set of buttons to be used to enter data into the EditText.
What I've done currently is stick a set of button widgets in the XML layout, and I use this code to make the buttons insert stuff into the EditText:
final EditText inputline = (EditText) findViewById(R.id.textentry);
final Button my_button = (Button) findViewById(R.id.my_btn);
my_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
inputline.append("a");
}
});
This kind of works, but I need help with a few issues:
it always appends the character at the end of the string, not at the current cursor position
similarly, when I call inputline.selectAll() and press my button, it inserts the text at the end of the string again; whereas I want it to delete the text first (as it's selected) and then insert the character
it seems tedious to write all that code for each of the buttons I have. Is there a better way to do this altogether?
Thanks for your help!
I have now pretty much solved by replacing inputline.append("a"); etc. with my custom function, lineInsert(), which you can see below.
public void lineInsert(CharSequence text) {
final EditText inputline = (EditText) findViewById(R.id.textentry);
int start = inputline.getSelectionStart();
int end = inputline.getSelectionEnd();
inputline.getText().replace(Math.min(start,end), Math.max(start,end), text, 0, text.length());
inputline.setSelection(inputline.getSelectionEnd());
}
This has the same behavior as the soft keyboard.
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