Enabling EditText OnClick when it is already disabled - android

I've noticed that I cannot use the EditText.setOnClickListener() when EditText.setEnabled(false) has been called before.
Is there any way to enable the EditText after clicking on it?

By calling EditText.setEnabled(false) you are disabling the EditText So what will you do by implementing EditText.setOnClickListener(), You can't do like this
public void setEnabled (boolean enabled) --
Set the enabled state of this view. The interpretation of the enabled state varies by subclass.
See the official Docs --
http://developer.android.com/reference/android/view/View.html#setEnabled%28boolean%29
If you want to use EditText.setOnClickListener()
then write EditText.setEnabled(true) before EditText.setOnClickListener()

Related

How to change state of swipe button from code?

I am using a swipe button from com.ebanx:swipe-button library in my application and I wish to change the state of the swipe button to enable (based on the information recieved via another Bluetooth device) when I open the button's activity. ie: Without any user input I have to change swipe button's state to enable !
You can use toggleState()
SwipeButton mSwipeButton; = findViewById(R.id.my_swipe_button);
mSwipeButton.toggleState();
if you use an older version where toggleState is not available, use collapseButton(); or expandButton(); to collapse or expand the swipe button
There are two issues with the library you're using, first is coding bug, second is wrong documentation, but that's not the case.
to make the button active:
SwipeButton swipe_btn = findViewById(R.id.swipe_btn);
swipe_btn.setEnabled(true);
now by default, the button state is closed and you can change that in the xml file i.e the layout where you created the button, you will see something like below:
<com.ebanx.swipebtn.SwipeButton
app:initial_state="disable" //change to enable will make button open by default
app:has_activate_state="true"
/>
Finally to monitor the state of the button, you will have to listen to the state changes like below:
swipe_btn.setOnStateChangeListener(new OnStateChangeListener() {
#Override
public void onStateChange(boolean active) {
Toast.makeText(MainActivity.this, "IS "+active, Toast.LENGTH_LONG).show();
}
});
if active, the button is open, else it's close.
Note: When I say open, I mean the button is toggledOn, and when I say close, it means the other way round(toggleOff).
The bug here is that when you use swipe_btn.toggleState(); The button will be deactivated, meaning it will not even respond to click event which is not right, so the way around is to use the onStateChangeListener as I use it above so that when the button is open you can do something and when it's close you can still do anything.
Note: library version: 'com.ebanx:swipe-button:0.8.3'

Talk Back annoucing spinner content description even when editText has focus

I have a Layout,
that has a form .. about five items down in the form is a spinner, and everytime my activity loads Talkback seems to read this spinners content description even though the focus and flashing cursor are on the first edittext view.
I can't seem to stop it.. any ideas?
Accessibility Focus and Input Focus are tracked separately in Android. I would need more details about your specific scenario to provide a 100% answer. However, my guess is that your EditText field is requesting input focus when the view loads, so the cursor is being moved there, and potentially the keyboard is popped up (depending on Android version). However, due to tab ordering, or some other mechanism, the spinner is the first item on the page to get Accessibility Focus.
There are two solutions to this, depending on the behavior that you want.
Solution 1 (recommended): Don't have your EditText box request input focus. Allow the user to force input focus into the box on interaction. For capable users, this is just a simple tap into the EditText box.
Solution 2 (less accessibile, but perhaps more usable): Shift accessibility focus to the EditText in your onResume method (or onCreate, or wherever you deem most appropriate). A method like this would be best:
public void resetFocus() {
editTextControl.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
editTextControl.requestFocus();
}
I would recommend calling the above method in onResume, so that every time this particular view loads input and accessibility focus will be in a predictable place. Otherwise, you've created additional accessibility issues.
I solved it simply like this:
/*
This custom Spinner has the purpose of making sure that there are no accessibility events fired after
a selection is made
*/
public class SilentSpinner<T> extends SimpleSpinner<T> {
//region constructors
...
//endregion
//region public methods
#Override
public void sendAccessibilityEvent(int eventType) {
if (eventType != AccessibilityEvent.TYPE_VIEW_SELECTED) {
super.sendAccessibilityEvent(eventType);
}
}
//endregion
}

How to visually show that a preference options are disabled?

I have created a 'Preferences' file in which I have a CheckboxPreference . When I uncheck the CheckboxPreference its dependants are getting disabled. However, it doesn't leave any visual clue that these dependants are disabled. How to provide that?
Try to show some alert dialogue or Toast message in onSharedPreferenceChangedListener() in your preference activity.
Try to get a reference to your Preference in your PreferenceActivity using findPreference method then set preference.setShouldDisableView() to true and preference.setEnabled() to false and see if it helps.

Why android InputMethodManager.showSoftInput() returns false?

Recently while developing an app, I faced an issue. I have searched a lot on google but couldn't find any solution. In the end I came across this Android issue tracker
To explain my issue, I have made a sample App.
Basic Working of my Sample App
I have a screen, which has an EditText, a Button and a RelativeLayout.
Width and Height of RelativeLayout is 0px. It is just a view to move focus away from EditText.
When App is launched focus is on RelativeLayout, not on EditText(so that there is not blinking cursor in it.)
When a user clicks on Button I just move focus to RelativeLayout using requestFocus() call on RelativeLayout.
When user taps on EditText, keyboard comes up. I can enter text in that.
What I want to achieve
If I change orientation of phone when keyboard is visible then after orienation changes, keyboard should stay.
If keyboard is visible and some other activity comes on top of it for e.g. alarm, facebook chat heads, opening something from notification area, locking unlocking device, etc.. then on returning back to sample app keyboard should be visible.
How I am achieving this
In onSaveInstanceState(), I check if focus is on EditText then put a boolean variable in Bundle.
In onStop(), I am setting a one boolean flag wasEditing = true.
In onRestoreInstanceState(), I checked if Bundle has flag value set in onSaveInstanceState(). If yes then I am make wasEditing = true.
In onResume(), I check this wasEditing and if it is true, I request focus for EditText.
After that I call imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT,resultRec)
Where I am getting problem
Sometimes after executing this call, Keyboard is not visible in few cases, like during orientation change.
When I put logs I have found this function is returning false
But if I make this showSoftInput() call with some delay of 100ms using mEditText.postDelayed() in onResume() everything works fine.
Question
In what cases this function returns false and why delay is working?
Note
Although I have solved my problem using delay, but I still want to know why it is behaving like that.
This is a problem I ran into today as well. Of my 8 android devices only 1 has the problem and it's running Android 4.0.4.
The problem was fixed by adding
mEditText.requestFocus();
mEditText.requestFocusFromTouch();
before calling
mEditText.showSoftInput(...)
You'll see the resultcode from showSoftInput is now true. I noticed that after the mEditText.requestFocus() the isFocused() was still false. Probably a bug in Android 4.0 and perhaps 4.1.
Please call showKeyboard after your mEditText is attached to window.
Please make sure the time you make the call.
PROBLEM:
I faced with this keyboard not showing up problem. I found the following solution inspired by this answer but not their solution! In short the reason for this mess is that the request focus and the IMM provided service can only run on a view that is created and active. When you do all these on the creation phase onCreate(Bundle savedInstance).. or onCreateView(LayoutInflater inflater... and the view is still in initializing state, you won't get an active view to act on! I have seen many solutions using delays and checks to wait for that view to get active then do the show keyboard but here is my solution based on the android frame work design:
SOLUTION:
in your activity or fragment override the following make sure your view has the access (define it in the top of the activity/fragment):
#Override
public void onStart() {
yourView.requestFocus();
showSoftKeyboard(yourView);
super.onStart();
}
public void showSoftKeyboard(View view) {
if(view.requestFocus()){
InputMethodManager imm = (InputMethodManager)
mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}

Android Switch. On or Off without user selection

Is there away to change the state of the switch, either on or off without the user pressing it?
http://developer.android.com/reference/android/preference/SwitchPreference.html
Take a look on the inherited methods from class android.preference.TwoStatePreference, there you'll find setChecked method:
public void setChecked (boolean checked)
It sets the checked state and saves it to the SharedPreferences. So SwitchPreference.setChecked(true) and SwitchPreference.setChecked(false) should work fine for you.
If you want to do it programatically...
setChecked(true);
setChecked(false);
This is inherited from the superclass TwoStatePreference.
http://developer.android.com/reference/android/preference/TwoStatePreference.html#setChecked(boolean)

Categories

Resources