I have a view in which there is a text box where user enters data, when clicks on submit, I want to store the input and display in another box.
final EditText edittext = (EditText) findViewById(R.id.edittext);
mText = (TextView) findViewById(R.id.timepicker_input);
How can I do it, please help
final EditText edittext = (EditText) findViewById(R.id.edittext);
TextView mText = (TextView) findViewById(R.id.timepicker_input);
mText.setText(edittext.getText().toString());
String info = edittext.getText().toString();
mText.setText(info);
final EditText edittext = (EditText) findViewById(R.id.edittext);
TextView mText = (TextView) findViewById(R.id.timepicker_input);
String storedData = edittext.getText().toString();
mText.setText(storedData);
Here, you can use the string storedData in another activity or to setText for another editText (textbox)
Related
EditText edituname = (EditText) findViewById(R.id.username);
EditText editpassword = (EditText) findViewById(R.id.password);
String s1 = edituname.getText().toString();
String s2 = editpassword.getText().toString();
Toast.makeText(Main2Activity.this, s1, Toast.LENGTH_LONG).show();
The string s1 shows null value since the code is inside onCreate method. I want it in onCreate and not on buttonclick. So please tell me a solution to get the value of EditText in onCreate.
The value of edittext will be null at the time of initialization thats why it is null. If you want the value without any button click you can set TextWatcher on edittext you want
So I am experimenting with Android Studio. I am stuck in the part where I want my "Submit" button to display the user inputted text underneath the page.
Button submit is the button.
"name" and "email" are the EditText variables a.k.a text fields
My TextView variable is outputText
So after the user enters random stuff in both text fields and click submit, I want my outputText to display both texts that were inputted in 2 separate lines
ex:
John Smith
john.smith#gmail.com
The code below is my .java code for the activity. I need to know how to initialize the new intent in a way that lets me do the 4 lines below it. Thanks.
public void buttonOnClick(View v){
Button submit = (Button) v;
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i =
new Intent(...?...);
startActivity(i);
}
});
editName = (EditText) findViewById(R.id.name);
editEmail = (EditText) findViewById(R.id.email);
textout = (TextView) findViewById(R.id.textView);
textout.setText(editName.getText()+" "+editEmail.getText());
}
I want my outputText to display both texts that were inputted in 2
separate lines
Use <br> and Html.fromHtml between text to add new line:
String strText=(String)editName.getText()+"<br>"+(String)editEmail.getText();
textout.setText(Html.fromHtml(strText));
how to initialize the new intent in a way that lets me do the 4 lines
below it...
Intent i =new Intent(CurrentActivityName.this,TargetActivityName.class);
startActivity(i);
For print in next line
textout.setText(editName.getText()+"\n"+editEmail.getText());
For intent
Intent i =new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
Ok I was able to accomplish what I wanted without an Intent initialized. Here's the code:
public void buttonOnClick(){
Button submit = (Button) findViewById(R.id.submit);
editName = (EditText) findViewById(R.id.name);
editEmail = (EditText) findViewById(R.id.email);
textout = (TextView) findViewById(R.id.outputText);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textout.setText(editName.getText()+"\n"+editEmail.getText());
}
});
}
public class MainActivity extends Activity {
EditText number1, number2;
String number_1, number_2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 = (EditText) findViewById(R.id.number1);
number2 = (EditText) findViewById(R.id.number2);
number_1 = number1.getText().toString();
number_2 = number2.getText().toString();
After running the application, why does number_1 have an empty value.
onCreate method is create view.
So EditText number1, nubmer2 is not create yet.
If you want get number1 or number2's value, try in onPause().
Try this..
You have to get the text in like OnClick while starting application both EditText is empty. So that it returns empty.
number_1 = number1.getText().toString();
number_2 = number2.getText().toString();
Make sure you have predefined the value in the edittext and that should work
i need to see your xml.... if your trying to get numbers you have to parse the string to an int anyway... what are you trying to achieve? you simply want the text, from each of the textviews? is there text set in the xml?
Make sure you have predefined the value in the edittext in xml file.
like in your layout file android:text="your string" or else you have to set string after binding
oncreate method like.
number1 = (EditText) findViewById(R.id.number1);
number2 = (EditText) findViewById(R.id.number2);
number1.setText("your string");
then you have to get string from below code.
number_1 = number1.getText().toString();
number_2 = number2.getText().toString();
You need to fill the EditTexts with something before you can get their value. So create a button, and after filling the EditTexts, then get their values by clicking the button.
Perhaps like this:
public class MainActivity extends Activity {
EditText number1, number2;
String number_1, number_2;
Button submit;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 = (EditText) findViewById(R.id.number1);
number2 = (EditText) findViewById(R.id.number2);
submit = (Button)findViewById(R.id.btn1); //Define a button in your layout xml file with the ID field set as "btn1"
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
number_1 = number1.getText().toString();
number_2 = number2.getText().toString();
}
});
in my app i create a set of editText dynamically. how can we set values to a dynamically created editText. i create the editText and add this editText to list
the code for creating the editText is given below
LinearLayout layout = new LinearLayout(context);
layout.setId(expRow2);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER);
for(int i=1;i<=4;i++)
{
EditText editText = new EditText(context);
editText.setHint(exp_EditTextName[i]);
editText.setId(expRow+i);
editText.setEms(10);
editText.setLayoutParams(margins);
layout.addView(editText);
expEditTextList.add(editText);
}
and i need to setValues to editText on other part of my code
final LinearLayout exp = new LinearLayout(context);
exp.setOrientation(LinearLayout.VERTICAL);
exp.setBackgroundColor(Color.GRAY);
exp.setDescendantFocusability(LinearLayout.FOCUS_AFTER_DESCENDANTS);
Button expAdd = new Button(context);
expAdd.setId(123);
expAdd.setBackgroundResource(R.drawable.small_add);
expAdd.setLayoutParams(marginLeftElement);
exp.addView(expAdd);
exp.addView(layout);
EditText edittext = (EditText)expEditTextList.get(0);
edittext.setText("hai");
but i cant set values to the editText.
try like this..
final EditText text1 = (EditText) findViewById(R.id.text1);
final EditText text2 = (EditText) findViewById(R.id.text2);
final EditText text3 = (EditText) findViewById(R.id.text3);
text2.addTextChangedListener(new TextWatcher() {
void afterTextChanged(Editable s) {
text3.setText(text1.getText().toString() + text2.getText().toString());
};
});
for more information try the link
set text on dynamically created edittext in android?
I have a SharedPreferences that saves EditText input from one activity and displays the String value in another activity.
When I enter an input into the EditText fields and press (a button I created) "Save" (which commits the EditText to editor), the file is stored successfully. However, the display activity (which displays the stored String values in SharedPreferences) doesn't display the values. I am guessing it is because it is the onCreate, so the screen is "created" when it runs. How can I get it to update the EditText values when the user commits (presses save) immediately?
CustomStoreEditActivity - Just storing the user inputs (EditText entries):
final Button saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (saveButton.isClickable()) {
SharedPreferences prefs = getSharedPreferences(
"customtime", 0);
// prefs.registerOnSharedPreferenceChangeListener(this);
final SharedPreferences.Editor edit = prefs.edit();
EditText shopName = (EditText) findViewById(R.id.shopName);
EditText open1 = (EditText) findViewById(R.id.open1);
EditText close1 = (EditText) findViewById(R.id.close1);
EditText open2 = (EditText) findViewById(R.id.open2);
EditText close2 = (EditText) findViewById(R.id.close2);
EditText open3 = (EditText) findViewById(R.id.open3);
EditText close3 = (EditText) findViewById(R.id.close3);
EditText open4 = (EditText) findViewById(R.id.open4);
EditText close4 = (EditText) findViewById(R.id.close4);
EditText open5 = (EditText) findViewById(R.id.open5);
EditText close5 = (EditText) findViewById(R.id.close5);
EditText open6 = (EditText) findViewById(R.id.open6);
EditText close6 = (EditText) findViewById(R.id.close6);
EditText open7 = (EditText) findViewById(R.id.open7);
EditText close7 = (EditText) findViewById(R.id.close7);
EditText comments = (EditText) findViewById(R.id.comments);
edit.putString("shopName", shopName.getText().toString());
edit.putString("Monday1", open1.getText().toString());
edit.putString("Monday2", close1.getText().toString());
edit.putString("Tuesday1", open2.getText().toString());
edit.putString("Tuesday2", close2.getText().toString());
edit.putString("Wednesday1", open3.getText().toString());
edit.putString("Wednesday2", close3.getText().toString());
edit.putString("Thursday1", open4.getText().toString());
edit.putString("Thursday2", close4.getText().toString());
edit.putString("Friday1", open5.getText().toString());
edit.putString("Friday2", close5.getText().toString());
edit.putString("Saturday1", open6.getText().toString());
edit.putString("Saturday2", close6.getText().toString());
edit.putString("Sunday1", open7.getText().toString());
edit.putString("Sunday2", close7.getText().toString());
edit.putString("comments", comments.getText().toString());
edit.commit();
Intent myIntent = new Intent(getBaseContext(),
CustomStoreActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(myIntent);
Toast.makeText(getBaseContext(), "Opening Hours Saved!",
Toast.LENGTH_SHORT).show();
}
}
});
}
CustomStoreActivity - where I believe the problem lies:
private void displayPreferences(){
SharedPreferences prefs = getSharedPreferences("customtime", 0);
String shopName = prefs.getString("shopName", "Empty");
String shopTime1 = prefs.getString("Monday1", " ");
String shopTime2 = prefs.getString("Monday2", " ");
String shopComments = prefs.getString("comments", "");
TextView displayPrefs = (TextView) findViewById(R.id.displayPrefs);
displayPrefs.setText(shopName + shopTime1 + shopTime2 + shopComments);
}
Thank you for your ample time.
I think part of what you're looking for is to put your CustomStoreActivity code in onResume instead of onCreate. Whatever code you move into onResume will occur whenever the CustomStoreActivity is brought to the front (including when it is first created).
Another alternative, assuming that CustomStoreActivity is used to launch the Activity that contains the EditTexts, is to use startActivityForResult and pass the data back in the result Intent instead of using the SharedPreferences. Here is a simple example (though note that the setResult call in this example is passed null where you would want to pass in an Intent, as documented here in the Android docs).
It also seems like you're trying to use your second Activity like a dialog box with editable fields. If that's the case, yet another alternative is to actually use a variant of the Dialog class within your CustomStoreActivity class instead of creating another Activity to act like one. See the Android doc for dialogs.
I'm not sure I really understand your question but are you trying to update the TextView of a different Activity from the one you are in? In which case I don't think this can be done and you should use Intents to pass the data to the activity