Detect dismissal of the keyboard (by back button) in EditText - java

I have a set of EditText fields generated by a for loop. Each time that field is edited, I need to run some calculations and update other text fields (think excel spreadsheet sums).
My logic is working, and I even have it set up to replace the last field's down focus change to the first field (to avoid the "Done" button showing on the keyboard).
However, if the user presses the Back button to dismiss the keyboard, the focus is not changed and the calculations are not done. The entered number is different but now the totals are wrong. I can't find where to detect when the keyboard is dismissed so I can work around this.
What I have now:
final EditText etWeight = new EditText(this);
etWeight.setText("initWt");
etWeight.setSelectAllOnFocus(true);
etWeight.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
//code to update after weight is changed
}
));
llWeightRow.addView(etWeight);
I have also tried putting this code in setOnEditorActionListener, setOnClickListener, and setOnKeyListener.
Aside from some of these not working as expected, none of them appear to trigger if the back button is pressed to dismiss the keyboard. I have searched online but only came up with suggestions on how to manually hide the keyboard with my own button; I can't seem to find anything concerning the back button on the tablet itself.
How can I detect that the keyboard has been dismissed so I can force a focus change (or just re-run the calculation code)?
Update:
Not marking this as answered because this doesn't answer this specific question, but I currently am producing my desired results (other fields always updated) by switching to a onTextChanged method. Now the values are updated whenever any text inside the EditText is changed.
etWeight.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence cs, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence cs, int i, int i1, int i2) {
//calc and update code
}
#Override
public void afterTextChanged(Editable editable) {
}
});

Have you tried to override onBackPressed? You can make your manipulations with focus there.
#Override
public void onBackPressed() {
// Make needed preparations here
super.onBackPressed();
}

try this
InputMethodManager imm = (InputMethodManager) ActivityName.this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
Log.d(TAG,"Software Keyboard was shown");
} else {
Log.d(TAG,"Software Keyboard was not shown");
}

Related

Enable button when edit text and checkbox are are fill and vice versa

Basically, I want to enable the button when both edit text and checkbox are fill
if any one of not fill then disable the button in android with java
you can add the condition inside setOnClickListener function i.e
button.setOnClickListener {
if(editText.text.toString().isEmpty() || !checkBox.isChecked) return
// do your job here...
}
you can try do it like following:
check for editText if its empty and if the checkbox is checked,
so if editText isn't empty and checkbox is checked that will mean button shall be enabled,else disabled
and apply same condition for on checkbox listener
condition will look as follows
yourButton.setEnabled(yourCheckBox.isChecked() && !editText.text.toString().isEmpty())
example on text watcher implementation:
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
yourButton.setEnabled(yourCheckBox.isChecked() && !editText.text.toString().isEmpty())
}
});
the reason you will need this on checkbox and on textwatcher at same time, is cause textwatcher can tell when editText is changed in realtime, on other hand the checkbox as far as I know it ain't possible thats why you will need the other condition set on checkbox click listener as well.

How to trigger an event when user clicks away from EditText?

I am building an app, and I want the app to trigger an event when the user has entered text into the editText and clicks away from it or closes the keyboard? How can I do this?
I am not very skilled in Java, so I would be grateful if you provided some description or code.
You can use onFocusChange listener on your edit text to overcome this problem
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b==true){
//entered in the edit text
}
else {
//left edit text
}
}
});
Simply set an onFocusChangedListener on your EditText and configure what happens when the EditText loses focus. Here's an illustration:
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean bool) {
if(bool){
// here, the EditText is in focus
}
else {
//here, the EditText is no longer in focus.
// do what you want to do
}
}
});
I hope this helps.

Perform action on EditText enter pressed in android while retaining the enter icon on keyboard?

I want to perform an action when enter is pressed in android editText. I have tried various ways of using EditorInfo.IME_ACTION_DONE, which works, but the problem is, it shows a tick icon, which I don't want. I want it to look like the default enter button on the keyboard.
How do I handle enter pressed event while retaining the "Enter" shape? Is there something like EditorInfo.IME_ACTION_ENTER to get my job done?
My approach (which shows tick icon):
editorEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if( (actionId == EditorInfo.IME_ACTION_DONE){
Toast.makeText(EditorActivity.this, "working", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
I want it to look like "Enter" shape because I am making a text editor. I am trying to shift to another EditText on the "Enter" button is pressed and don't want it to look like a "Tick" button. That's why I don't want the tick symbol.
I think just changing the inputType of your EditText in your layout should do the trick to have an "Enter" like button in your soft keyboard in Android.
<EditText
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textMultiLine" />
Please note that I have used textMultiLine as the inputType of the EditText.
You can take some action on pressing the "Enter" button in your EditText like the following.
final EditText edittext = (EditText) findViewById(R.id.edit);
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (before == 0 && count == 1 && s.charAt(start) == '\n') {
String text = edittext.getText().replace(start, start + 1, "").toString(); // Removes the enter
Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
Hope that completes the answer.

Android: How to run a callback right after setText() is rendered

I want to display some text in an EditText and do some work right away after the text is displayed. I have the following code in my onCreate() method:
this.editor.setText(text, TextView.BufferType.EDITABLE);
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
// Work that needs to be done right after the text is displayed
}
}, 1000);
This works OK, but I want to minimize the delay between setText() rendering and the work being done-- a 1s delay is unacceptable. However, if I change the delay to 0ms or 1ms, then the work is done before the text gets rendered.
I could keep typing in numbers to search for the perfect delay time that would execute my code just after the text was rendered, but that seems very tedious/imprecise. Is there a better way to tell Android to run a callback right after that happens? Thanks.
edit: Here are two things I've tried that didn't work. For bonus points, it would be very helpful if you could explain to me why these didn't work.
Using Handler.post
new Handler(Looper.getMainLooper()).post(r) also runs r before the text finishes rendering. I thought setText adds rendering code to the queue, so shouldn't post(r) being called after that add r after the rendering code?
Using View.post
this.editor.post(r) didn't work either, r is still called before the text is rendered.
Use this it would hlp
mSongNameTextView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
You can assign a TextWatcher to your EditText.
A TextWatcher is basically a listener that listens for changes to the text (before, during and after) in the EditText.
It can be implemented as follows:
EditText et;
et.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Work that needs to be done right after the text is displayed
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
}
So when you set the text explicitly, this listener should be called and after the text is changed, the // Work that needs to be done right after the text is displayed code will be run.
You can use ViewTreeObserver as below:
yourView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// do your work here. This call back will be called after view is rendered.
yourView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// or below API 16: yourView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
I initially wanted to delay the work because it was CPU-intensive. I realized the solution was to spin up a new thread for the work, rather than post it to the UI thread.

Android - Bolding Part of Text in EditText

There's a similar answer here, but it doesn't state what I'm about to ask.
I have a 'Bold Text' option in my Android view and if a user selects that option, a boolean gets set as true in the code behind.
Using a TextWatcher, how can I change the text typed by the user to bold after a specific point in the EditText. If the user turns it off, the text typed after should be in normal styling. Everything depends on the boolean value.
Here's what I have so far:
Boolean isBolded = false;
// Code that turns the bold option true and false...
contentBox = (EditText) findViewById(R.id.contentBox);
contentBox.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(isBolded)
{
//Start bolding the text typed after that point
}
else
{
//Stop styling the text typed after that point
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
The difficulty would be to determine the points in the EditText where the user decides to turn bolding off and on while typing. What do you guys have in mind?
Set Html.fromHtml("<b>This part will be bold</b> This won't!") as the Text.

Categories

Resources