I have certain edit text fields. I save the data entered in these fields to my database, which opens in another activity.
But I have a problem when I navigate back to the first activity (Using the back button on the hardware of the emulator) to add next record, the edit field data is retained.
I tried onPause() and myEditText.setText("") also. But the dat simple clears off the edit fields but as soon as I click the fields to enter data again the previous data reappears.
I also tried using finish() and everything works except I have to go through all the activities again to enter the data.
Try this one also.
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String uid=editText1.getText().toString();
String pwd=editText2.getText().toString();
editText1.setText("");
editText2.setText("");
}
});
}
Just to make sure that I understood you clearly. You enter text in EditText boxes. Then press a button which takes you to a new activity. But, when you go back to the old activity the text in the EditText boxes doesn't get clear. Try to do editText.setText(""); when you click the button. I know you said that you tried it, but did you try it inside the function which listens for the button click?
public EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText)findViewById(R.id.editText1);
editText.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// As soon as button is clicked, set is as empty
editText.setText("");
}
Try and see if it makes a difference.
you can use this library to clear text by a clear icon
http://droidparts.org/widgets.html#clearableedittext
Use this:
editText.getText().clear();
after onclick of any action do below step
((EditText) findViewById(R.id.yoursXmlId)).setText("");
or else
write this in XML file
EditText
android:hint="Enter Name" />
i did this way.try
You can use:
myEditText.setText(null);
Related
I create a dialog window in an activity, then I put the buttons in this dialog window and listened to them. However, I want to listen to android device found in the back button. and dialog.setOnkeyListen method I used for it. I have put the program by executing dialog window EDITTEXT not enter data into the field. So when I add code to the setOnkeyListen I can not enter data into the EditText field. I hope you know what I mean
If you're asking how to set the OnClickListener for a textview here is an example hope this helps:
TextView textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Code you want to execute on the click goes here
}
});
I am making my first app in android that is a simple calculator.
When I press button '1' then it shows "1" in EditText, but when I press it again it doesn't show "11", it shows only "1".
How can I fix this?
Since you've offered no code I can only take a stab in the dark and guess that you're calling setText() on your EditText which would overwrite whatever was in there in the first place. What you would need to get is something like:
myEdit.setText(myEdit.getText + "1");
try this code on button click.use append method to add text number in editview as:
EditText editText = (EditText) findViewById(R.id.editText1);
editText.append("1");
and you can use:
editText.setText(editText.GetText() + title);
you are probably replacing the contents of the EditText completely, while what you want to do is append to it ... something like,
oneButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + "1");
}
}
so I tried android:selectAllOnFocus and of course I'm using android:hint.
The app loads, requestFocus triggers and the full text is selected.
The problem is that when I click in the EditText the selection is lost.
I've already read:
Select all text inside EditText when it gets focus
For some reason, it worked (on Jelly Bean) only if I posted it with a Handler:
new Handler().post(new Runnable() {
#Override
public void run() {
paidView.selectAll();
}
});
Set the selection in an View.OnClickListener like so:
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setSelection(0, editText.getText().length() - 1);
}
}
Set the selection in an View.OnClickListener like so: (with the 0 and text length opposite to the previously approved response) - this will ensure that when the user starts typing the content will be overridden.
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setSelection(editText.getText().length() - 1, 0);
}
}
I realize this is an old post, but I had this same problem. For me, the reasoning was that I like to move the keyboard out of the way (dismiss it) to get my bearings (or hit buttons to add or remove rows of data) and when i touched back on the EditText I was previously editing, it was annoying to have the keyboard pop back up and the text un-select, forcing me to either work to get the cursor to where I wanted to start deleting, or touch another EditText and then touch back on the original to re-select everything. I just want to have the keyboard pop back up and have the text selected and ready to overwrite whenever I go to that EditText.
There are easy solutions to this:
1) Long tap does this for you on most occasions.
2) If you're already using setSelectAllOnFocus(true), you can just throw a simple clearFocus() and requestFocus() in your onClick listener:
etMyEditText.setSelectAllOnFocus(true);
etMyEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view.clearFocus();
view.requestFocus();
}
});
This way the EditText has everything selected when you tap on it, regardless of soft keyboard status.
Additional bonus:
Add android:windowSoftInputMode="adjustPan" inside your <activity .../> tag in your AndroidManifest.xml file to force the selected EditText to stay in sight when the soft keyboard pops up.
try This
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.selectAll();
//or this.selectAll();
}
To select all the text.
Do the code below after you did findViewById and you are good to go:
edtProductPrice.setSelectAllOnFocus(true);
edtProductPrice.requestFocus();
edtProductPrice.selectAll();
I am developing an application for Android.
My application contains ten buttons, to which I have set an onclicklistener() method.
The ten buttons contains the digits 0-9.
Now, if I click any two or three buttons among the ten buttons, the corresponding digits must be entered into edit text and it must be shown in the edittext box.
I am able to display the single digit if I click on any of the buttons, but if I click on another button, then the previous value disappears and the new value is shown.
But what I want is this: no matter how many buttons I click, that no. of digits will appear in the edittext box.
Please can anyone explain to me the code, or give me a hint so that it can be made in a simpler way.
Using Shared Preferences:
I think, you may used Shared Preferences when you button was click, get value from Edittext and put on shared preferences. After click next button get that shared preferences value. You may used each button click put on value shared preferences.
Go to this problem, which is help you to solve: >> SharedPreference problem in android
Using Intent:
May be used this code on button click event:
Bundle extras = getIntent().getExtras();
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");
if (value1 != null && value2 != null) {
EditText text1 = (EditText) findViewById(R.id.EditText01);
EditText text2 = (EditText) findViewById(R.id.EditText02);
text1.setText(value1);
text2.setText(value2);
}
Other useful resources:
Get Value of a Edit Text field
http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-edittext-controls/
http://www.java2s.com/Code/Android/UI/GetvaluefromEditText.htm
http://geekswithblogs.net/bosuch/archive/2011/01/17/android---passing-data-between-activities.aspx
You are using editText.setText("");
Instead you must use editText.append();
You can take a public static String variable and concat the new value to previous and set it to EditText
Use below code
public class AsActivity extends Activity {
/** Called when the activity is first created. */
Button b1,b2,b3;
EditText et;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
et=(EditText)findViewById(R.id.editText1);
}
public void onclick(View v){
switch(v.getId()){
case R.id.button1:
et.append("1");
break;
case R.id.button2:
et.append("2");
break;
case R.id.button3:
et.append("3");
break;
}
}
}
here i have use property of button you can use switch statement as shown above in ur onclicklistener
In all 0-9 buttons onclick event you can write following code.
editText.setText((editText.getText().toString) +""+ nevText);
When you click on button then above code set newText with previous text in edittext box.
You should use the EditText append() method which appends data to the EditText.
So each time a new button is clicked just use :
myEdtiText.append(str);
Okay i have a edit text box that takes user input. Every time the user clicks the button i would like for the text that the user typed to be gone. As of now everytime the button is clicked the text remains in the edittext. I would like the text to be erased by default without having to create a instance of EditText everytime
Thanks guys if you can help!
Use setText():
mEditText.setText("");
after your setContentView:
final EditText yourEditText= (EditText)findViewById(R.id.yourIDEditText);
yourEditText.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
yourEditText.setText("");
}
});
With this, every time the user clicks all content is cleaned.