I am traing to clear textedit field when it is focused. I know about hint option, but I wanna clear textedit everytime when user actives it, also if is filled by user (now user have to clear field manualy everytime he wants change value).
I load fragments in my app so there are a lot of edittext fields which I wanna clear on focus, so is there universal method to do this, or I have to do it to all fields severally?
Should I create another java file to this method or put inside onCreate?
You need to add an onClickListener and set the textfield to an empty string when clicked. You need to add the listener for each textfield but you can reuse the same listener because the functionality is the same. You can set the listener on onCreate, that's fine.
You should you OnFocusChangeListener as is shown in code below:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
((EditText)v).setText("");
}
}
});
Simply set an View.OnFocusChangeListener with the method setOnFocusChangeListener(View.OnFocusChangeListener l).
In the callback onFocusChange(View v, boolean hasFocus) you can do the following:
if (hasFocus && v instanceof EditText) {
((EditText) v).setText("");
}
Related
I want to make it impossible for the user to click on a button until he does not 'get out of' or clicks away from an EditText.
Like, the button will be there, but when the user goes to enter text in an EditText, it will be grayed out, and will be clickable again only when the user leaves the editText.
I hope my question is clear. How can I do this?
Your button behaviour depends on the focus of the editext so you can use
mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mButton.setEnabled(false)
} else {
mButton.setEnabled(true)
}
}
});
the if is extended, consider that you can solve in just one line
mButton.setEnabled(!hasFocus)
Set OnFocusChangeListener on your EditText:
editView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
button.setEnabled(!hasFocus);
}
});
Use setOnFocusChangeListener and override the onFocusChange. Then, use the button.setEnabled
I want to make something happen on text selection, which is OnLongClickListener, but inside of that I need to get selected text, which is handled by default OnLongClickListener (at least I think it is).
Actual result, by adding just my listener, is that my method is called, I'm trying to get indices of selection bounds, but these are 0. I can also see in debugger, that no text is selected in that momment.
Code:
textView.setTextIsSelectable(true);
textView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
int start = textView.getSelectionStart();
int end = textView.getSelectionEnd();
// the rest of code
}
}
}
Question: How can I preserve default listener, which will be called first and make selection and then call my function.
I think you misunderstand how listeners work. They don't replace standard behavior, so there is no default listener to invoke to make sure something happens.
In this case, most likely your OnLongClick listener is simply being called before the TextView has actually updated its selection. In which case, you can try having your listener delay its processing until after the selection is set. Look at using Handler.postDelayed() or AsyncTask for that purpose. For example:
textView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
new Handler().post(() -> {/*your code here*/});
}
}
}
But, that being said, OnLongClickListener is not the correct listener to use for text selection changes. You need an ActionMode Callback instead:
How to set up a listener on the selected text in TextView
I'm having a question where I couldn't find the answer online or know how to find it..
I have EditText xml attribute and I made an event listener
to this attribute by changing the color of an underline beneath it. Is there a way when the focus is removed from this EditText (i.e user click on any other element rather than this one) to remove the highlighted color for the line I colored?
On the onclick event listener? It seems weird, but I want the opposite of the onclick like onclickremove or something.
You can use the the setOnFocusChangeListener to your EditText. If lost focus,clear the color filter:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
else{
editText.getBackground().clearColorFilter();
}
}
});
If you want to change your view color, just add the below line in onFocusChange:
view.setBackgroundColor(Color.parseColor("#ffffff"));
Hope this helps.
You need to use setOnFocusChangedListener for this. The hasFocus determines whether the focus is removed or given to a view. It being false indicates that user has left the field.
EditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus == false){
// change the color
}
}
});
I have an ImageView overlay inside of a RelativeLayout and want to prevent any clicks from going through the ImageView to the Buttons etc that are behind it (thus in effect disabling the entire RelativeLayout).
Is the a simpler way of doing this then iterating the RelativeLayout views and setting them to disabled as I currently am doing using this code:
RelativeLayout rlTest = (RelativeLayout ) findViewById(R.id.rlTest);
for (int i = 0; i < rlTest.getChildCount(); i++) {
View view = rlTest.getChildAt(i);
view.setEnabled(true);
}
you can set the image to be
android:clickable="true"
Simply call rlTest.setClickable(false). This will prevent the click to be propagate to the children
There is a much cleaner way
You can use:
android:onClick="preventClicks"
in XML and in your Activity
public void preventClicks(View view) {}
This works with fragments.
Example inside this Activity has multiple fragments overlapping one another, just by adding the XML attribute in the background of your fragment it will still call the Activity.preventClicks and will prevent touches on fragments behind it
The following solution works in the general case:
_container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// NOTE: This prevents the touches from propagating through the view and incorrectly invoking the button behind it
return true;
}
});
It basically blocks any touches from propagating through the view by marking the touch event as handled.
This works on both UI controls and layout containers (ie: LinearLayout, FrameLayout etc.).
The solution to set "clickable" as false did not work for me for layout containers either in code or in the view XML.
I assume that you are using onClickListeners.
How about using onTouchListener instead of onClickListeners. By doing this you will have a control over how deep down in your hierarchy the touch even can be visible. For example, if you have toch listeners on a relative-layout(RL) and a image-view(IV)(contained in RL), and you assign touchListeners to both. Now if you return true from IV's touch event, the lower down member RL will not receive the touch event. However if you return false from from IV's touch event, the lower down member RL will receive the touch event.
Hope this helps!
Just add these two listeners:
// Set an OnTouchListener to always return true for onTouch events so that a touch
// sequence cannot pass through the item to the item below.
view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
return true;
}
});
// Set an OnHoverListener to always return true for onHover events so that focus cannot
// pass through the item to the item below.
view.setOnHoverListener(new OnHoverListener() {
#Override
public boolean onHover(View v, MotionEvent event) {
v.onHoverEvent(event);
return true;
}
});
You could use databindings and consume the clicks like this:
android:onClick="#{() -> true}"
In C#, I use an empty delegate:
objectName.Click += delegate {};
I haven't encountered any problems with it but it does prevent clicks from filtering through to underlying controls.
you can also se the root click listener to null:
// Do not process clicks on other areas of this fragment
binding.root.setOnClickListener(null)
This works 100%.
It doesnt affect other listeners that are already set on the fragment's views.
In my program i use many EditText dynamically created , and i need to call method each time when any of them get changed (lost focus). is it possible to do such thing ?
otherwise how to make on focus lost\change lister for all View ?
you will need to set View.setOnFocusChangeListener for EditText to listen for focus change .
For Example:
View.OnFocusChangeListener editTextFocusChnage=
new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
swich(v.getId()){
case Edittext1_id:
// do your work here..
break;
case Edittext2_id:
// do your work here..
break;
case Edittext3_id:
// do your work here..
break;
.....
}
}
};
where Edittext1_id,Edittext2_id,.... is dynamic EditText id's
EDIT :-
add FocusChangeListener to EditText's as:
editText1.setOnFocusChangeListener(editTextFocusChnage);
editText2.setOnFocusChangeListener(editTextFocusChnage);
editText3.setOnFocusChangeListener(editTextFocusChnage);
.....