I need edittext to be changed according to the spinner value
its working for keylistener(null) for non editable but i am not able to get it back and since focus shouldn't loose from edittext I am not using this setEnabled method
I am using this code
sp_card.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View arg1,
final int pos, long arg3) {
if (filteredCardsList.get(pos).awardPointsRatio != null
&& filteredCardsList.get(pos).awardPointsRatio == 0.0) {
et_award_points.setText("");
et_award_points.setHint("No ratio is available");
et_award_points.setKeyListener(et_award_points
.getKeyListener());
} else {
et_award_points.setKeyListener(null);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Here et_award_points becomes always non-editable field its not changing to its state back
Try this code.It works
private KeyListener listener;
listener = et_award_points.getKeyListener();
et_award_points.setKeyListener(listener);
use editTextInstance.setEnabled(true) to made it editable, editTextInstance.setEnabled(false) to disable
Change your button to ToggleButton and implement this code , this should be the best solution I reckon
button.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editText.setEnabled(isChecked);
}
});
Related
I usually use boolean for this but in this situation I can not. I have a code like below on my Spinner and everything works fine :
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
carNames = brand.getChildCarNames(brand.getListDataHeader().get(position));
makeAndShowDialogBox();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
The problem is when I use bolean what happens is that the choice that comes as Default is not showing the DialogBox if user chooses that Default value as-well. Such as "Tomato" comes as Default and user wants to select "Tomato" as well; nothing happens than.
What I want to do is also prevent this dialog box coming asap when Activity is opened but also I want to prevent "on selection of default value nothing happens" issue.
So, Is there any way to check exactly if the User pressed to select or not?
You can use a Boolean variable to determine if the spinner is just loaded, so you can prevent the dialog to show when the Activity is opened (or onResume). You can declare a variable outside the listener, and change its value inside the listener. Its value is changed inside the listener, so you have to do final Boolean array instead of regular Boolean.
You can try with the following code:
final Boolean[] spinnerLoaded = {false};
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if (!spinnerLoaded[0]) {
spinnerLoaded[0] = true;
}
else {
carNames = brand.getChildCarNames(brand.getListDataHeader().get(position));
makeAndShowDialogBox();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
I have a vertical scrolling layout with multiple EditText and Spinner controls.
Problem is when i choose any spinner option after selection the screen scrolls back to the last EditText is was editing before this spinner. The Focus remains with the last EditText i was editing. Is there a way i can keep focus with the current selected spinner.
I am looking for a solution that can be implemented on all spinners from layout XML or some generic method to apply to all spinners.
Here's my solution.
mSpinner.setFocusableInTouchMode(true);
mSpinner.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (mSpinner.getWindowToken() != null) {
mSpinner.performClick();
}
}
}
});
Use this to avoid EditText Focus :
scrollView = (ScrollView) findViewById(R.id.scrollView_id);
scrollView.setOnTouchListener(new OnTouchListener() {
// to solve foocus problem on scrolling
public boolean onTouch(View v, MotionEvent event) {
if (myEditText.hasFocus()) {
myEditText.clearFocus();
}
if (myEditText1.hasFocus()) {
myEditText1.clearFocus();
}
return false;
}
});
I solved the problem with the help of Ryderz answer. here is the code :
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
scrollView = (ScrollView) findViewById(R.id.sv_main);
scrollView.setOnTouchListener(new OnTouchListener() {
// to solve focus problem on scrolling
public boolean onTouch(View v, MotionEvent event) {
IBinder windowToken = null;
if (myEditText1.hasFocus()) {
myEditText1.clearFocus();
windowToken = myEditText1.getWindowToken();
}
if (myEditText2.hasFocus()) {
myEditText2.clearFocus();
windowToken = myEditText2.getWindowToken();
}
if (myEditText3.hasFocus()) {
myEditText3.clearFocus();
windowToken = myEditText3.getWindowToken();
}
if (windowToken != null) {
imm.hideSoftInputFromWindow(windowToken, 0);
}
scrollView.requestFocusFromTouch();
return false;
}
});
Then I set the android:focusable="true" for my textViews so that on scroll when focus is removed from editText then the Textviews can be picked for focus. In that way the user doesn't see any focused control on screen.
I had the same problem like you and I've resolved with this solution.
I've redefined OnItemSelectedListener and when the spinner changes the value, I take the focus and after that, I release again.
This is my class CustomOnItemSelectedListener:
public class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener{
private Spinner mSpinner;
private int iCurrentSelection;
public CustomOnItemSelectedListener(Spinner s){
mSpinner = s;
iCurrentSelection = mSpinner.getSelectedItemPosition();
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (iCurrentSelection != arg2) {
mSpinner.setFocusableInTouchMode(true);
mSpinner.requestFocus();
mSpinner.setFocusableInTouchMode(false);
mSpinner.clearFocus();
}
iCurrentSelection = arg2;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
//No hace falta hacer nada
}
}
In my code I do this
Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener(spinner));
If you want to keep focus, don't do "clearFocus()".
I have a form for member registration in my android app, I have applied the animation effect on fields which are required (some EditText views in this case) if those fields are not filled the effect should occur then like this :
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub
if (mEditText.getText.equals("")) {
mEditText.setAnimation(MyAnimation.animate());}
and MyAnimation.animate() is like :
public class MyAnimation {
public static Animation animate(){
TranslateAnimation mAnimate = new TranslateAnimation(0, 5, 0, 0);
mAnimate.setInterpolator(new CycleInterpolator(50));
mAnimate.setDuration(600);
return mAnimate;
}
}
but the problem is it occurs when the mEditText gains the focus, and my need is this should occur on focus leave if mEditText is empty.
Implement setOnFocusChangeListener and check there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control.
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
{
//do job here owhen Edittext lose focus
if (mEditText.getText().equals("")) {
mEditText.setAnimation(MyAnimation.animate());}
}
}
});
I have a number of EditText elements in my page along with two buttons. I want the user to touch on any one EditText field and click any button to insert a certain value into that very EditText field they touched. Giving input using the keypad is not allowed. Please help me to do this.
You can use View.OnFocusChangeListener to detect if any view (edittext) gained or lost focus.
for (EditText view : editList){
view.setOnFocusChangeListener(focusListener);
}
....
private OnFocusChangeListener focusListener = new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
focusedView = v;
} else {
focusedView = null;
}
}
}
This goes in your activity or fragment or wherever you have the EditTexts. The .... is just saying that you can put it anywhere else in the class. And obviously you would need to create an array with them in it, thus the editList.
This works for me. It returns a boolean.
myEditText.hasFocus()
One thing that you can do is declare a global variable evalue which will tell you which is the last selected EditText by using onTouchListener and then based on the value of evalue, you can set the text value to the edittext by button click. hope you understood.
the code for it can be as follow:
EditText e1,e2;
Button b1,b2;
String evalue;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
e1.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1)
{
evalue="1";
return false;
}
});
e2.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1)
{
evalue="2";
return false;
}
});
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0) {
if(evalue=="1")
{
e1.setText("yes");
}
if(evalue=="2")
{
e2.setText("yes");
}
}
});
b2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0) {
if(evalue=="1")
{
e1.setText("No");
}
if(evalue=="2")
{
e2.setText("No");
}
}
});
}
Its a logical coding.. not upto the mark.. if you find a better one. then use it. thank you.
This worked for me.
e1 = (EditText) findViewById(R.id.editText1);
e1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View arg0, boolean hasfocus) {
if (hasfocus) {
Log.e("TAG", "e1 focused")
} else {
Log.e("TAG", "e1 not focused")
}
}
});
With Java 8 lambdas:
EditText editTextTo = findViewById(R.id.editTextTo);
editTextTo.setOnFocusChangeListener((view, b) -> {
if (view.isFocused()) {
// Do whatever you want when the EditText is focused
// Example:
editTextFrom.setText("Focused!");
}
});
Note that the view inside the lambda function is actually an instance of an EditText.
Have your Fragment implement OnTouchListener & OnFocusChangeListener:
public class AttributesFragment extends Fragment
implements OnTouchListener, OnFocusChangeListener {
Then implement using:
#Override
public boolean onTouch(View view, MotionEvent event) {
if (view instanceof EditText) {
view.setOnFocusChangeListener(this); // User touched edittext
}
return false;
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
}
Don't forget to set your listener after creating your EditText
editText.setOnTouchListener(this);
This way if an EditText box is focused by default but user has not changed the field it will not fire the onFocusChange event.
Simple Coding:
if(EditText1.isFocused()){
//EditText1 is focused
}else if(EditText2.isFocused()){
//EditText2 is focused
}
This worked for me using Java 8 (lambdas):
EditText myEditText = findViewById(R.id.editText);
myEditText.setOnFocusChangeListener((view, hasFocus) -> {
if(hasFocus){
Log.e("TAG", "myEditext has focus")
}else{
Log.e("TAG", "myEditext has no focus")
}
});
In your activity or fragment implement OnFocusChangeListener.
edittextNeme= (EditText) findViewById(R.id.edittextNeme);
edittextNeme.setOnFocusChangeListener(this);
You will have to check which view changed focus by getting the view id with view.getId() and handle accordingly.
#Override
public void onFocusChange(View view, boolean hasfocus) {
switch(view.getId()){
case R.id.edittextNeme:
//Do something
break;
...etc
}
}
val currentFocusView = activity.currentFocus
if (currentFocusView is EditText){
currentFocusView.appendText("Has Focus!")
}
I am trying to capture when an edittext field which is disabled gets pressed.
(I only want to change it, I dont want text inserted into it).
I assigned an onClickListener to it but it is not called.. Any suggestions ?
Is there any other event I can listen to ?
final EditText field = column.get(i);
field.setEnabled(false);
field.setClickable(true);
field.setFocusable(false);
field.setFocusableInTouchMode(false);
field.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((EditText)v).setText("a");
}
});
Thanks !
You cant pass a click-event when a View is disabled.
Therefore, try changing:
field.setEnabled(false);
To:
field.setEnabled(true);
final EditText field = column.get(i);
field.setCursorVisible(false);
field.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
field.setText("something");
field.clearFocus();
}
}
});