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
Related
I have one textview and one button ,i coud change textview text with below code :
final Textview c_tv_matn;
Button c_btn_dokme;
c_btn_dokme = (button) findviewbyid(R.id.btn1);
c_tv_matn = (Textview) findviewbyid(R.id.txt1);
c_btn_dokme.setonclickListener(new OnclickListener() {
#Override
public void onClick(View v) {
c_tv_matn.SetText("this is second text");
});
But i wanna change text from String.xml and make Next Button Like this ;
"matn_1","matn_2"matn_3"matn_4...
STRING.XML
<string name="matn_0">Hello world!</string>
<string name="matn_1">You are hero john</string>
<string name="matn_2">you can change this world</string>
<string name="matn_3">You are so clever</string>
cAN YOU HELP ME TO GET RES FROM STRING AND CHANG TEXTVIEW TEXT WITH NUMBERS?
cAN YOU HELP ME TO GET RES FROM STRING AND CHANG TEXTVIEW TEXT WITH NUMBERS?
You can use either getString(int) or getText(int) to retrieve a string. getText(int) retains any rich text styling applied to the string.
But in this case you'll use getString(int) that returns the string value associated with a particular resource ID. It will be stripped of any styled text information.
Sometimes you'll need a context like on this case that you want to do it inside a Button then you can get the context from your View, or if you have a global context in your Activity/Fragment you can use it also.
Example
If I did not misunderstood, what you want is to put the text from Strings.xml to TextView, so you can do it like this :
final Textview c_tv_matn;
Button c_btn_dokme;
int textNumber = 1;
c_btn_dokme = (button) findviewbyid(R.id.btn1);
c_tv_matn = (Textview) findviewbyid(R.id.txt1);
c_btn_dokme.setonclickListener(new OnclickListener() {
#Override
public void onClick(View v) {
switch(textNumber){
case 1:
c_tv_matn.setText(v.getContext().getString(R.string.matn_1))
textNumber++;
break;
case 2:
c_tv_matn.setText(v.getContext().getString(R.string.matn_2))
textNumber++;
break;
case 3:
c_tv_matn.setText(v.getContext().getString(R.string.matn_3))
textNumber++;
break;
default:
textNumber = 1;
c_tv_matn.setText(v.getContext().getString(R.string.matn_1)
break;
});
To get the String from string.xml you need a Context.
An Activity is a Context so, if you are in an Activity you can just call getString(R.string.<the name in string.xml>) to retrieve the String you need.
For example getString(R.string.matn_0).
Then it can be applied to your needs:
c_tv_matn.SetText(getString(R.string.matn_0));
If you are not in an Activity then you need to get hold of a Context, probably passing it in as a parameter to the constructor of the class (and store it as an attribute) or as a parameter to the method that will do the setting of the text.
Create an array in an Android XML file
you need an array in string.xml. For reference please have look on below link
https://www.homeandlearn.co.uk/android/grid_view_array.html
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
}
Hell i am trying to compare textview value with button how can i do it?
i tried this way but not working.
button xml - android:tag = "1"
Java
Button btn = (Button) findViewById(id);
string valu = btn.getTag();
string txt = textView.getText();
if(txt.equals(valu))
{
// do what you want
}
try this:
textView.getText().toString();
getText() returns a CharSequence not a String.
Because of that your equals must be false.
Simply use this.
if (b.getTag().toString().equalsIgnoreCase(tv.getText().toString())) {
Toast.makeText(getApplicationContext(), "HI", 9000).show();
}
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}
I have a program that has a text input and a button. When I type something into the input and press the button I want that String to be added to a String Arraylist and have that Arraylist displayed in a TextView. Right now I have a method:
public void addString(View view)
{
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
choices.add(message);
}
"edit-choices" is the name of the text input and "choices" is the name of the array list. First of all am I doing this correctly? Second, how to I get the text view to display the contents of "choices". Right now my TextView id is just textView1
Please keep in mind that it is not the best way to show list items in a TextView. You can do this using a ListView. Anyhow, see pseudo code below (didn't test that in Eclipse, however, it should show how it is basically going to work):
public class YourActivity extends Activity {
Vector<String> choices = new Vector<String>();
public void onCreate(Bundle ....) {
(Button) myButton = (Button) findViewById(R.id.button);
myButton.setOnClickListener(new OnClickListener() {
#Override
public boolean button.onClick() {
addString();
TextView textView = (TextView) findViewById(R.id.text_view);
String listRepresentation = "";
for (String choice : choices)
if ("".equals(listRepresentation))
listRepresentation = choice; else
listRepresentation = ", " +choice;
textView.setText(listRepresentation );
}
});
}
public void addString(View view)
{
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
choices.add(message);
}
}
So simply assign an OnClickListener to your button that does what you need.
The question is how you want the Text to be displayed...
Either like a list view or just as a normal text.
If you want to show the text as a normal text in the text view you can simply do something like this.
for(String msg : choices)
{
textView1.setText(textView1.getText()+msg);
}
If you want the choices to be displayed in list view you need to set an adapter to the list view using the choices that you have.
First of all am I doing this correctly?
If it works for you, sure. I would maybe cache the EditText so you don't have to "find" it every time you want to access it's content.
Your only "problem" here is, that a TextView has no method that accepts a List<String>. So, you'll need to make a single string out of your list of strings.
You can simply iterate over the list and con-cat them together:
StringBuilder b = new StringBuilder();
for (String s : choices){
b.append(s+"\n");
}
textview.setText(b.toString());
This will simply build one string from all the items in your list, adding line-breaks after every item.
You'll need to set your TextView's android:inputType-attribute to textMultiLine, so it will actually show you multiple lines.