Disabled EditText Views are Problematic

Mahares

Expert
Licensed User
Longtime User
I have an app with a couple of tabhost pages for simplicity. Each page has a few enabled and a few disabled edittext views. When I press the tab indicator to go to any of the two pages, the focus ALWAYS goes to the disabled box first where I did not want it to be, despite the following code:

B4X:
If TabHost1.CurrentTab=1 Then 'pres tab         
         Enable_P.RequestFocus  'enabled box
         IME.ShowKeyboard(Enable_P)
         Msgbox("Flag","")
End If
I used a msgbox flag and I can see behind it that the focus gets on the enabled one I want. As soon I press the 'OK', it shifts to the disabled edit text which is to the right of the enabled one. I even switched their position on the designer and I still cannot get it to focus on the enabled. It happens in both tabs. Short of replacing the edittext views with labels, is there anything I can do to get the focus to stay away from the disabled ones. We need a TABSTOP property to avoid this mess.
 

Mahares

Expert
Licensed User
Longtime User
@mc73: Although it appears this a similar problem to the other thread, this is a different problem. The focus goes to the disabled box as soon as you open the page. The other thread issue is moving from box to box. By the way, I tried your short code, if this is the one you refrerred to:
B4X:
Sub EditTextDisabled_FocusChanged (HasFocus As Boolean)   
 If HasFocus=True Then EditTextNext.RequestFocus End Sub
It worked well on the first tab moving from box to box, but as soon as I went to another tab, it quit working. I tried Roger's again after some slight modification and the navigation problem from box to box worked well, although it required a good bit of code to accommodate evey box. I have not tried the long code you posted. If you are you referring to the long code:Sub loadTxtBoxes If I has an application in my case here which is a different problem, I will be more than happy to give it a shot.
Thanks for your help.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
If you put the focus request in the focuschange subroutine of the disabled textbox, won't it take you to the enabled box?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Of course I did try it before I posted, but it did not help at all. As soon as I press the tab page indicator to go to that page, the focus jumps to the disabled box, even with this code which is a copy of yours:
B4X:
Sub CSGPpr_FocusChanged (HasFocus As Boolean)
 If HasFocus=True Then CSGP.RequestFocus   'CSGPpr disabled, CSGP enabled
End Sub
What a strange behavior. I have been messing with this and the other issue for the last 2 days straight. Should I take a crack at the long code you posted instead? Is it applicable here? I have not touched it.
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I don't think so, since it's of the same logic :-( can you post a sample project?
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Looks like they partially fixed this in ICS. Yesterday in the other thread with the slightly similar issue I was on my Motorola ET1 Tablet with Gingerbread. After reading the Bug report above I decided to try it out on my ATT HTC One X.

With Gingerbread it is able to get orange highlight focus as well as type in it. On ICS I am unable to type in it, but it does get focus...although with a dark green outline. It appears to behave similar to if InputType is set to 0 and closes the keyboard on focus. Erel may still need to add something to disable the focus capabilities 100%.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I even tried this code (modeled after Roger G. code) and it did not help. As soon as I press the tab page indicator to go to that page, the focus jumps to the disabled box:

B4X:
IME.AddHandleActionEvent(CSGP)  
IME.AddHandleActionEvent(CSGPpr)
B4X:
Sub IME_HandleAction As Boolean
    Dim edtTxt As EditText    
    edtTxt = Sender    
      If edtTxt = CSGPpr Then   'CSGPpr disabled, CSGP enabled. Both reside in same tab page.
         CSGP.RequestFocus
         Return True
      End If
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Could you please try this sample and tell us if it works for you? It worked on my tablet.
 

Attachments

  • 4txtboxes.zip
    8.1 KB · Views: 228
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
That only executes when Next or Done is pressed in the keyboard, this is the tabhost gaining focus and android then setting focus to the first focusable control in the current tab.
I Didn't press something except from clicking on the tabhost.
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Sorry, cross post, that was in reply to them saying they used my other code. That zip works as it should. You can see the disabled get focus then quickly goes to the enabled. I think some of the issues is overuse of the IME library like in the first post code above it sets focus then also opens the keyboard. Focus should already open the keyboard. Perhaps that is why a request to focus another view isn't working.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Sorry, cross post, that was in reply to them saying they used my other code. That zip works as it should. You can see the disabled get focus then quickly goes to the enabled. I think some of the issues is overuse of the IME library like in the first post code above it sets focus then also opens the keyboard. Focus should already open the keyboard. Perhaps that is why a request to focus another view isn't working.
np, I've noticed it afterwards :)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@mc73: Thank you for the code and your tremendous efforts by the way. I tried it and here is why I found out:
1. If I DIM this line:'EditText4.RequestFocus
It goes immediately to the disabled box in the first tab as my project does. However, if I return to tab1, then it is ok.
2. If I UNDIM: 'EditText2.RequestFocus , the code crashes as it isays that it is not initialized.
As you can see, the behavior is all over the place as I experienced with my code. Of course, I have a lot more garbage in my project and actually few more tabs. Also, I cannot place a requestfocus in the activity create the way you have it, because I have a lot of other stuff that is performed first like extraction of previous record data, listviews, calculations, etc.
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
See: http://www.b4x.com/forum/bugs-wishlist/17920-bug-disabled-edit-views-still-get-focus.html

Until there is an improvement/fix you should be able to make a function to toggle enabled like my toggle button does in the test app I made:

B4X:
Sub Button1_Click
Dim obj1 As Reflector

   If EditText2.Enabled Then ' Enabled, so Disable
      EditText2.Enabled = False
      obj1.Target = EditText2
      obj1.RunMethod2("setFocusable", "False", "java.lang.boolean")
      obj1.RunMethod2("setFocusableInTouchMode", "False", "java.lang.boolean")
   Else
      EditText2.Enabled = True
      obj1.Target = EditText2
      obj1.RunMethod2("setFocusable", "True", "java.lang.boolean")
      obj1.RunMethod2("setFocusableInTouchMode", "True", "java.lang.boolean")
   End If
End Sub

You will need the Reflection library. It you follow Erel's link to the other thread there is a direct link there, or you can search for it. Copy the xml and jar file to your library folder where B4A is installed then restart B4A.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Thanks Roger G. The reflector may do it. I am thinking going through each of the panels I have and apply the fllowing code or similar.
B4X:
' To prevent focus on disabled edittext views.
For i = 0 To PanelX.NumberOfViews - 1
            If PanelX.GetView(i) Is EditText   Then
               Dim DisabledView As EditText         
               DisabledView = PanelX.GetView(i)
               If DisabledView.Enabled=False Then 
                  obj1.Target = DisabledView
                  obj1.RunMethod2("setFocusable", "False", "java.lang.boolean")
               End If  
            End If
Next
Do you have a better and more efficicient approach than the above. If not, where would you put the code. I am thinking in Activity_Create
Thank you very much for your help. Hope things are well in Mo.
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
That should work. I'm guessing the disabled boxes are disabled the whole time, so you wouldn't need the code to enable. Doesn't really matter where you put the code as long as it is after the panel you are processing has its layout loaded/views added to it. Could be the end of Create or even in Resume.

Only improvements I see is maybe making:

obj1.Target = PanelX.GetView(i)

possibly skipping the extra temp Edit Text.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I think we still have to keep that temp var: DisabledView. Otherwisw there is no way to call its enabled=false property. You cannot do: PanelX.GetView(i).enable=false.
Thanks for you help. You are going to be an asset to the forum and Erel.
 
Upvote 0
Top