How do I select all text when clicking on a EditText view? - android

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();

Related

How to load a layout inside another one on click and change button function?

I'm trying to do something like this:
When I go to this activity I have what is in black and some objects like EditText boxes.
Once I press the button I want those EditBoxes an other stuff that is up there to stay visible but unable to be edited (that's easy to do from code overriding onClick).
But at the same time I also want to load some layout down inside the same activity (from an xml) and change the button function to act over the objects of the new layout.
Could anyone give me an idea on how to do this two things staying in the same activity?
Update:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
editText1.setEnabled(false);
editText2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
}
});
}
On the first click I want the button to do that. But once it's pressed I want it to do another thing. How could I do that?
(that's easy to do from code overriding onClick).
Actually I would recommend enable or disable which is easier to trace by using
view.setEnabled(bool);
as for the other question I'd recommend adding the layout from the start with setting visibility to GONE and when needed set the visibility to VISIBLE
view.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
Ok, I've realized it was a dumb question, just add a flag an edit it:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!button_pressed) {
editText1.setEnabled(false);
edittext2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
button_pressed=true;
}
else{
create_button.setText("Second click");
create_button.setEnabled(false);
}
}
});
}
}

EditText's selectAll() does not select the text, instead cursor is moved to position 0

My situation is: I have a EditText, and under it I have a button, called "select all". It's purpose is to let the user select all the text by pressing it.
When the button is clicked, I invoke selectAll() on the EditText, but instead of selecting all text, in some cases (generally, when the cursor is already positioned within the text somewhere), the cursor is moved to position 0 (start of text) and text remains unselected. Second click on the button will then select all. But of course it should happen on first click.
From this issue: Android EditText selectAll() doesn't works if one taps on the same field in android 4.x only it seems that this is a bug in android 4.0 and above. (Found no mention in Google issue tracker).
Does anyone know about a way to overcome this problem? It seems that the probelm is also in other selection methods and not only selectAll().
(p.s. This question is sort of duplicate of the issue I mentioned above. But I opened another question, because the author of that issue was satisfied and selected a partial answer (of setting android:selectAllOnFocus="true"), while in my case and scenario it does not help me and does not solve the problem.
Thanks.
Problem caused by IME. If showed cursor drag pointer then selection must be zero width.
You need cancel drag pointer. It can be doned by change text. For example replace:
Editable text = edit.getText();
if (text.length() > 0) {
text.replace(0, 1, text.subSequence(0, 1), 0, 1);
edit.selectAll();
}
We replace first symbol of text with same symbol. It cause cancel drag pointer and allow make selection without bug.
I was having a similar issue. I solved it (using Xamarin in C#, but the equivalent Java will work) with the following code:
private void InitFocus()
{
Java.Lang.Runnable rable = new Java.Lang.Runnable(()=>
{
EditText maleCount = FindViewById<EditText>(Resource.Id.txtMaleCount);
maleCount.RequestFocus();
maleCount.SelectAll();
});
new Handler().Post(rable);
}
I have 2 controls: One is a dropdown that lets you select a chicken house and then a set of buttons for each day of the week. Whenever you change the day of week or the chicken house, I want to set focus in the txtMaleCount EditText and then I want the value in that box selected (since it's a number and they're presumably going to replace it).
Clearly, the non-intuitive part was the need to Post it. Doing it directly (on the UI thread) didn't seem to have any effect.
Try this:
yourEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//((EditText)v).selectAll();
((EditText)v).setSelection(startValue, stopValue);
}
});
Or This:
yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
#Override
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus){
//((EditText)v).selectAll();
((EditText)v).setSelection(startValue, stopValue);
}
}
});
Or this:
theEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = (EditText)view;
editText.setSelection(editText.getText().length()-1); // selects all the text
}
});
Or this:
theEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = (EditText)view;
editText.performLongClick();
}
});
Hope this helps .. :)

Button wont become invisible after being clicked - Android

I have a button that is set to VISIBLE under certain circumstances, then once its clicked its suppose to make the button INVISIBLE again but for some reason its not working. Here is my code,
if(variable == 2){
testButton.setVisibility(View.VISIBLE);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
testButton.setVisibility(View.INVISIBLE);
test2Button.setVisibility(View.VISIBLE);
}
});
}
Have you tried displaying a toast when the button is clicked, just to see if that block of code is even executing? I don't see it, but I'm assuming you've actually declared a View associated with that button via 'findViewById'
EDIT:1
Do this
public void onClick(View view) {
view.setVisibility(View.INVISIBLE);
findViewById(R.id.<your test2Buttons ID>).setVisibility(View.VISIBLE);
}
Note: If you do View.GONE it will leave all area acquired by it and the other control will capture this area
where is with View.INVISIBLE it will maintain its acquired area

Clearing edit text data

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);

How to make EditText erase evertime a button is clicked

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.

Categories

Resources