Of late I was just trying to create a mems geneator app, yes I had been following the new bostons and in that buckey created them using 2 fragments and here i just want to do in a single main activity, but I just can't figure out how to retrieve the text from the edit text field and set the text of Text view to it. I know it's pretty a newbie question but still I don't know how to code it so please help...
I had just imported some widgets,views,etc and done some modified the on create function and my on create function is:
public class MainActivity extends AppCompatActivity {
public static EditText topText;
public static EditText bottomtext;
public static TextView top;
public static TextView bottom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topText = (EditText) findViewById(R.id.topedit);
bottomtext = (EditText) findViewById(R.id.bottomedit);
top = (TextView) findViewById(R.id.top);
bottom = (TextView) findViewById(R.id.bottom);
Button myButton = (Button) findViewById(R.id.button);
myButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View V) {
top.setText((CharSequence) topText);
bottom.setText((CharSequence) bottomtext);
}
}
);
}
Simply do this:
if(topText.getText()!=null){
top.setText(topText.getText().toString());
}
if(bottomtext.getText()!=null){
bottom.setText(bottomtext.getText().toString());
}
Try this to get text of the EditText field:
CharSequence text = topText.getText();
And set the text above for the textView:
top.setText(text);
Use this short example to understand the concept
store=ret.getText().toString();
show.setText(store);
Explanation
ret: is the name of the edit text field;
store: is used to hold anything gotten from ret (text field)
show.setText(store) displays the result in the textview
This question has already been answered:
"Use getText() on your EditText object".
Get Value of a Edit Text field
Next time do a quick search ;)
Related
I have in my program a scroll view with a linearlayout inside of it. I add dynamically TextView's to the linearlayout and I have no way to know how much TextView's I'll end up with. When a certain TextView is clicked I need to get it's text. Any idea what is the best way to do it?
Thanks in advance.
I've tried to add a listener to the text view but I am not sure how to get the text. I saw in some posts that you can do a listener to the LinearLayour/ScrollView though I am not sure what is the best option.
This happenes every time a message is added:
TextView messageText = new TextView(RecordedMessagesScreen.this);
messageText.setText(content);
messageText.setClickable(true);
messageText.setOnClickListener(RecordedMessagesScreen.this.textViewListener);
RecordedMessagesScreen.this.messagesLayout.addView(messageText);
this is the listener:
this.textViewListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("message", ***NEED TO GET THE TEXT***)
}
};
At the class level declare a String variable:
private String text = "";
and a View.OnClickListener variable:
private View.OnClickListener listener;
Initialize the listener in onCreate() of your activity like this:
listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView) v;
text = tv.getText().toString();
}
};
and every time you create a new TextView set the listener:
textView.setOnClickListener(listener);
This way the variable text each time you click a TextView will get the clicked TextView's text.
You can customize the code inside onClick() yo suit your needs.
I have a list of buttons and what I want to do is display some information on press. Here´s the code:
public class ActividadDos extends AppCompatActivity {
private Map<CharSequence, String> info = new HashMap();
private Button btnYogur;
private Button btnChocolate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actividad_dos);
btnYogur = (Button) findViewById(R.id.btnYogur);
btnChocolate = (Button) findViewById(R.id.btnChocolate);
info.put(btnYogur.getText(), "Lácteos");
info.put(btnChocolate.getText(), "Grasas, aceites y dulces");
}
public void displayMensaje(View v) {
Button button = (Button) v;
String nombre = (String) button.getText();
String mensaje = "Clasificación: " + info.get(nombre);
Toast.makeText(ActividadDos.this, mensaje, Toast.LENGTH_SHORT).show();
}
}
It doesn´t show any errors but when I press the buttons it doesn´t display the message.
Similar code is working on another activity.
You forgot to call displayMensaje() method.
Add this to .xml for corresponding button(As per requirement)
android:onClick="displayMensaje"
It isn't displaying anything because you aren't calling displayMensaje() method anywhere in your code at all. To solve this, please add a call to this method and your problem will be solved.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I posted this question before, however i didnt explain myself good. I will have the other question removed. Here is my current situation:
I have a regular xml page with a textView which when clicked opens a popup dialog. This dialog contains 2 editText. Currently my code (OnClick – Done button) gets the value of both edit texts and puts them into the single TextView. However when i open the pop-up again, instead of the two strings being listed in its own editText (Where each string was originally inputted) the combined string which was stored in the text view appears in one edit text. The issue is that although i’m getting the strings from 2 different editText’s and storing them into one textView. I cannot get each string back individually. I understand that i may have to store the string from each editText into variables and then i can use the variables to show the strings combined in the textView (and the editText – when i open the popup dialog again) How would i go about this? Thank for your help
The code:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView showPopUpButton;
EditText getInput;
EditText getInput2;
String myvalue = "";
String myvalue2 = "";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showPopUpButton = (TextView) findViewById(R.id.buttonShowPopUp);
showPopUpButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showPopUp3(); }
});
}
private void showPopUp3() {
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("Enter PU Builder");
LayoutInflater inflater = getLayoutInflater();
View checkboxLayout = inflater.inflate(R.layout.popuplayout, null);
helpBuilder.setView(checkboxLayout);
getInput = (EditText) checkboxLayout.findViewById(R.id.editText1);
getInput2 = (EditText) checkboxLayout.findViewById(R.id.editText2);
getInput.setText(myvalue);
getInput2.setText(myvalue2);
helpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
myvalue = getInput.getText().toString();
showPopUpButton.setText(myvalue + ", " + myvalue2);
}
});
AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
}
}
First of all you need string to save your EditText value in it declare it like this
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView showPopUpButton; //NEW
EditText getInput; //NEW
EditText getInput2; //NEW
// declare string to save the dialog edittext
String myValue = "" ;
then you need to show the last value of dialog in the EditText so try this :
private void showPopUp3() {
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("Enter PU Builder");
LayoutInflater inflater = getLayoutInflater();
View checkboxLayout = inflater.inflate(R.layout.popuplayout, null);
getInput = (EditText) checkboxLayout.findViewById(R.id.editText1); //MISTAKE
getInput2 = (EditText) checkboxLayout.findViewById(R.id.editText2); //MISTAKE
getInput.setText(showPopUpButton.getText()); //New to keep the text in the editText when done is pressed
getInput2.setText(getInput2.getText()); //New test
// here set the my value to edit text , note firs time will be empty
getInput.setText(myValue)
and last thing when you click in done button in dialog you need to save the EditText value like that :
#Override
public void onClick(DialogInterface dialog, int which){
//showPopUpButton.setText(getInput.getText() + ", " + getInput2.getText());//NEW
//showPopUpButton.setText(value) ;
// save the edit text value into myvalue string
myvalue = getInput.getText().toString();
}
});
feed me back
This is fairly straightforward.
Create the variables to placehold your strings.
String inputText;
Where applicable, get and set.
inputText = editText.getText().toString();
textView.setText(inputText);
Did I understand this correctly? Is this what you are trying to accomplish?
I have ScroolView and it's inside Linear layout. In this linear layout i added programmically around 20 TextView. When i press any of those TextView i change it color to RED (at first it was white). When i press again the same TextView i check his color and if it's is RED i again make it white.
What i want to do:
I press for example 1 TextView and make it RED. Then when i press 2 TextView i make this one RED and i want to make 1 TextView WHITE. This functionality should be at all TextView.
So any idea's how to do this ?
You mean to say at a time you need only one textview to be red. You can do this using 2 variables. One is a boolean colored. This indicates that at least one TextView is colored. Another is a TextView variable. Create a TextView variable lastColoredTextView. Let it be null initially. Then whenever the textview is clicked, assign the lastColoredTextView to the clicked TextView. Then whenever you are clicking, just check if colored then change the color of lastColoredTextView to white.
Change class name and it will work fine.
public class Test_stflowActivity extends Activity {
TextView current_red_txt_box = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = null;
LinearLayout lp = new LinearLayout(getApplicationContext());
lp.setOrientation(LinearLayout.VERTICAL);
View.OnClickListener txt_click = new View.OnClickListener() {
#Override
public void onClick(View v) {
current_red_txt_box.setTextColor(Color.WHITE);
TextView tv = (TextView) v;
tv.setTextColor(Color.RED);
current_red_txt_box = tv;
}
};
for (int i = 0; i < 20; i++) {
tv = new TextView(getApplicationContext());
tv.setId(i);
tv.setTextSize(40);
tv.setText("you text");
tv.setTextColor(Color.WHITE);
tv.setOnClickListener(txt_click);
lp.addView(tv);
current_red_txt_box = tv;
}
setContentView(lp);
}
}
In my app I have an edit user details page and I want to display the current name, email address etc in the corresponding editText fields and then the user can just erase that and enter a new one if they want.
Is there a way to do this? Thanks for any help
There is the hint feature? You can use the setHint() to set it, or set it in XML (though you probably don't want that, because the XML doesn't 'know' the name/adress of your user :) )
You can use EditText.setText(...) to set the current text of an EditText field.
Example:
yourEditText.setText(currentUserName);
From the xml:
android:text="yourtext"
You can use text property in your xml file for particular Edittext fields.
For example :
<EditText
android:id="#+id/ET_User"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yourusername"/>
like this all Edittext fields contains text whatever u want,if user wants to change particular Edittext field he remove older text and enter his new text.
In Another way just you get the particular Edittext field id in activity class and set text to that one.
Another way = programmatically
Example:
EditText username=(EditText)findViewById(R.id.ET_User);
username.setText("jack");
You can do it in this way
private EditText nameEdit;
private EditText emailEdit;
private String nameDefaultValue = "Your Name";
private String emailDefaultValue = "abc#xyz.com";
and inside onCreate method
nameEdit = (EditText) findViewById(R.id.name);
nameEdit.setText(nameDefaultValue);
nameEdit.setOnTouchListener( new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (nameEdit.getText().toString().equals(nameDefaultValue)){
nameEdit.setText("");
}
return false;
}
});
nameEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus && TextUtils.isEmpty(nameEdit.getText().toString())){
nameEdit.setText(nameDefaultValue);
} else if (hasFocus && nameEdit.getText().toString().equals(nameDefaultValue)){
nameEdit.setText("");
}
}
});
emailEdit = (EditText)findViewById(R.id.email);
emailEdit.setText(emailDefaultValue);
emailEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus && TextUtils.isEmpty(emailEdit.getText().toString())){
emailEdit.setText(emailDefaultValue);
} else if (hasFocus && emailEdit.getText().toString().equals(emailDefaultValue)){
emailEdit.setText("");
}
}
});
First you need to load the user details somehow
Then you need to find your EditText if you don't have it-
EditText et = (EditText)findViewById(R.id.youredittext);
after you've found your EditText, call
et.setText(theUserName);
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText et = (EditText) findViewById(R.id.et);
EditText et_city = (EditText) findViewById(R.id.et_city);
// Set the default text of second EditText widget
et_city.setText("USA");
}
}
Use android android:hint for set default value or android:text
We wish there is a default value attribute in each view of android views or group view in future versions of SDK. but to overcome that, simply before submission, check if the view is empty equal true, then assign a default value
example:
/* add 0 as default numeric value to a price field when skipped by a user,
in order to avoid parsing error of empty or improper format value. */
if (Objects.requireNonNull(edPrice.getText()).toString().trim().isEmpty())
edPrice.setText("0");