Android Question PreferencesDialog ShortOptions width

james_sgp

Active Member
Licensed User
Longtime User
Hi,
I`m having trouble implementing a PreferencesDialog, I have a ShortOptions item which I`m filling from a database; which all works weel. However when the dialog is displayed, the Shortoptions is squashed to one corner, cutting off the text (which is only 15 characters long). How do I adjust the width of the ShortOptions dropdown, I`ve tried this https://www.b4x.com/android/forum/t...item-at-preferencesdialog.137694/#post-872218

But it doesn`t work, gives the same error message in the post.

Thanks James
 

Attachments

  • 1.png
    1.png
    11.5 KB · Views: 208

Magma

Expert
Licensed User
Longtime User
Hi,
I`m having trouble implementing a PreferencesDialog, I have a ShortOptions item which I`m filling from a database; which all works weel. However when the dialog is displayed, the Shortoptions is squashed to one corner, cutting off the text (which is only 15 characters long). How do I adjust the width of the ShortOptions dropdown, I`ve tried this https://www.b4x.com/android/forum/t...item-at-preferencesdialog.137694/#post-872218

But it doesn`t work, gives the same error message in the post.

Thanks James

Hi there... i have a sub for this:

B4X:
Private Sub SetWidthItem (Pref As PreferencesDialog, Key As String, wwidth As Double)
    For i = 0 To Pref.PrefItems.Size - 1
        Dim pi As B4XPrefItem = Pref.PrefItems.Get(i)
        If pi.key = Key Then
            Dim oldx As Double=Pref.CustomListView1.GetPanel(i).GetView(1).Left
            Dim oldw As Double=Pref.CustomListView1.GetPanel(i).GetView(1).width
            Pref.CustomListView1.GetPanel(i).GetView(1).Left=(oldx + oldw)-wwidth
            Pref.CustomListView1.GetPanel(i).GetView(1).width=wwidth
        End If
    Next
End Sub

Use it like this in Private Sub PrefDialog_BeforeDialogDisplayed (Template As Object)
B4X:
SetWidthItem(prefdialog,"your input",180dip)
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Yes, that is fhe obe i tried but gives an error that Panel is not available (same as was posted on ur original post (that i linked too).
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
...Also... must trick somehow... to show the width,alignment changes...

using this to show prefdialog... example:
B4X:
    prefdialog.ShowDialog(Options1, "OK", "CANCEL") 'add this line
    Sleep(0) 'add this line
    Wait For (prefdialog.ShowDialog(Options1, "OK", "CANCEL")) Complete (Result As Int)
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Yes, I`m using the FormBuilder to make the original form, here is the code I use to display it:
B4X:
        Private Options1 As Map
        Dim List1 As List
        List1.Initialize
        Options1.Initialize
        Dim rs As ResultSet = main_page.sql_beacon.ExecQuery("SELECT event_name FROM events")
        Do While rs.NextRow
            List1.Add(rs.GetString("event_name"))
        Loop
        rs.Close
        PrefDialog2.SetOptions("Event",List1)
        SetWidthItem(PrefDialog2,"Event",200dip)
        Wait For (PrefDialog2.ShowDialog(Options1, "OK", "CANCEL")) Complete (Result As Int)
        If Result = xui.DialogResponse_Positive Then
'            PrintOptions(Options1, TextArea1)   
        End If

And here is the error I get using Magma's routine:
B4X:
Error occurred on line: 513 (admin)
java.lang.IndexOutOfBoundsException: Index: 4, Size: 0
    at java.util.ArrayList.get(ArrayList.java:437)
    at anywheresoftware.b4a.objects.collections.List.Get(List.java:117)
    at b4a.example3.customlistview._getrawlistitem(customlistview.java:447)
    at b4a.example3.customlistview._getpanel(customlistview.java:440)
    at dsds.bb_admin.admin._setwidthitem(admin.java:1269)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7352)
    at android.widget.TextView.performClick(TextView.java:14230)
    at android.view.View.performClickInternal(View.java:7318)
    at android.view.View.access$3200(View.java:846)
    at android.view.View$PerformClick.run(View.java:27800)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7050)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
** Activity (main) Pause event (activity is not paused). **

This is the line that is giving the error:
B4X:
            Dim oldx As Double=Pref.CustomListView1.GetPanel(i).GetView(1).Left
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
a) as i said SetWidthItem must run into Private Sub PrefDialog_BeforeDialogDisplayed (Template As Object)- do you have ?

b)
Private Options1 As Map
Dim List1 As List
List1.Initialize
Options1.Initialize
Dim rs As ResultSet = main_page.sql_beacon.ExecQuery("SELECT event_name FROM events")
Do While rs.NextRow
List1.Add(rs.GetString("event_name"))
Loop
rs.Close

'run... without put values... this the next step.. to see what happen..
'PrefDialog2.SetOptions("Event",List1) 'hmmm ... set a REM here... something... not good..
SetWidthItem(PrefDialog2,"Event",200dip)

'add here...

prefdialog2.ShowDialog(Options1, "OK", "CANCEL") 'add this line
Sleep(0) 'add this line

Wait For (PrefDialog2.ShowDialog(Options1, "OK", "CANCEL")) Complete (Result As Int)
If Result = xui.DialogResponse_Positive Then
' PrintOptions(Options1, TextArea1)
End If
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
I did try Private Sub PrefDialog_BeforeDialogDisplayed (Template As Object), but found it was not triggered whwn showing the dialog? Im out now, will give it another try when i get back home.

jamea
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
I did try Private Sub PrefDialog_BeforeDialogDisplayed (Template As Object), but found it was not triggered whwn showing the dialog? Im out now, will give it another try when i get back home.

jamea
also add the other said at post #8... and REM adding list to shortoptions... for a while to see if works...
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Ok, so I just put your code into a simple B4XPages app, and it works perfectly. But the same code doesn`t work in my full app...


B4X:
        Private Options1 As Map
        Dim List1 As List
        List1.Initialize
        Options1.Initialize
        Dim rs As ResultSet = main_page.sql_beacon.ExecQuery("SELECT event_name FROM events")
        Do While rs.NextRow
            List1.Add(rs.GetString("event_name"))
        Loop
        rs.Close
        PrefDialog2.SetOptions("Event",List1)
        Sleep(0)
        Wait For (PrefDialog2.ShowDialog(Options1, "OK", "CANCEL")) Complete (Result As Int)
        If Result = xui.DialogResponse_Positive Then
'            PrintOptions(Options1, TextArea1)  
        End If

B4X:
Private Sub SetWidthItem (Pref As PreferencesDialog, Key As String, wwidth As Double)

    Log(Pref.PrefItems.Size)
    For i = 0 To Pref.PrefItems.Size - 1
        Log(i)
        Dim pi As B4XPrefItem = Pref.PrefItems.Get(i)
        If pi.key = Key Then
           
            Log(Pref.CustomListView1.GetPanel(i).GetView(1).Left)
           
            Dim oldx As Double = Pref.CustomListView1.GetPanel(i).GetView(1).Left
            Dim oldw As Double = Pref.CustomListView1.GetPanel(i).GetView(1).width
            Pref.CustomListView1.GetPanel(i).GetView(1).Left = (oldx + oldw)-wwidth
            Pref.CustomListView1.GetPanel(i).GetView(1).width = wwidth
        End If
    Next
End Sub

Private Sub PrefDialog2_BeforeDialogDisplayed (Template As Object)
    SetWidthItem(PrefDialog2,"Event",250dip)
End Sub



James
 
Last edited:
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
So, further checking with my test ap, your code isn`t executing...the ShortOptions just happens to resize correctly!
 

Attachments

  • test1.zip
    10.9 KB · Views: 113
Upvote 0

teddybear

Well-Known Member
Licensed User
Ok, so I just put your code into a simple B4XPages app, and it works perfectly. But the same code doesn`t work in my full app...


B4X:
        Private Options1 As Map
        Dim List1 As List
        List1.Initialize
        Options1.Initialize
        Dim rs As ResultSet = main_page.sql_beacon.ExecQuery("SELECT event_name FROM events")
        Do While rs.NextRow
            List1.Add(rs.GetString("event_name"))
        Loop
        rs.Close
        PrefDialog2.SetOptions("Event",List1)
        Sleep(0)
        Wait For (PrefDialog2.ShowDialog(Options1, "OK", "CANCEL")) Complete (Result As Int)
        If Result = xui.DialogResponse_Positive Then
'            PrintOptions(Options1, TextArea1)
        End If
Your post #11 and test1.zip, I don't think it does work, above code you do nothing for setWidthItem, carefully read post #6 and post #8
Add below code before wait for prefdialog.ShowDialog
B4X:
    prefdialog.ShowDialog(Options1, "OK", "CANCEL") 'add this line
    PrefDialog2_BeforeDialogDisplayed(Null)
    Sleep(0)
    Wait For (prefdialog.ShowDialog(Options1, "OK", "CANCEL")) Complete (Result As Int)
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
Hi there... i have a sub for this:
Your solution is great, but unfortunately it doesn't work for AddShortOptionsItem

I played around a bit and found this solution.
B4X:
' SetWidthItem(prefdialog,"your input",180dip)
' Use it like this in Private Sub PrefDialog_BeforeDialogDisplayed (Template As Object)
Private Sub SetWidthItem (Pref As PreferencesDialog, Key As String, wwidth As Double)
    For i = 0 To Pref.PrefItems.Size - 1
        Dim pi As B4XPrefItem = Pref.PrefItems.Get(i)
        If pi.key = Key Then
            If pi.ItemType = Pref.TYPE_SHORTOPTIONS Then
                Dim Parent As B4XView = Pref.CustomListView1.GetPanel(i).GetView(1)
                Parent.Left = (Parent.Left + Parent.Width) - wwidth
                Parent.Width = wwidth
                Dim view As B4XView = Parent.GetView( 0)
                view.Width = Parent.Width
            Else
                Dim oldx As Double=Pref.CustomListView1.GetPanel(i).GetView(1).Left
                Dim oldw As Double=Pref.CustomListView1.GetPanel(i).GetView(1).Width
                Pref.CustomListView1.GetPanel(i).GetView(1).Left=(oldx+oldw)-wwidth
                Pref.CustomListView1.GetPanel(i).GetView(1).Width= wwidth
            End If
        End If
    Next
End Sub
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Guys, thanks for the awesome help...unfortunately my test app now gets the same error on this line:

B4X:
Dim Parent As B4XView = Pref.CustomListView1.GetPanel(i).GetView(1)

B4X:
--------- beginning of system
Copying updated assets files (2)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Error occurred on line: 55 (B4XMainPage)
java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
    at java.util.ArrayList.get(ArrayList.java:437)
    at anywheresoftware.b4a.objects.collections.List.Get(List.java:117)
    at b4a.example3.customlistview._getrawlistitem(customlistview.java:447)
    at b4a.example3.customlistview._getpanel(customlistview.java:440)
    at b4a.example.b4xmainpage._setwidthitem(b4xmainpage.java:251)
    at b4a.example.b4xmainpage._prefdialog2_beforedialogdisplayed(b4xmainpage.java:181)
    at b4a.example.b4xmainpage$ResumableSub_Button1_Click.resume(b4xmainpage.java:124)
    at b4a.example.b4xmainpage._button1_click(b4xmainpage.java:75)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7352)
    at android.widget.TextView.performClick(TextView.java:14230)
    at android.view.View.performClickInternal(View.java:7318)
    at android.view.View.access$3200(View.java:846)
    at android.view.View$PerformClick.run(View.java:27800)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7050)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
** Activity (main) Pause event (activity is not paused). **
** Service (starter) Destroy (ignored)**
 

Attachments

  • test1.zip
    11 KB · Views: 110
Upvote 0

teddybear

Well-Known Member
Licensed User
Guys, thanks for the awesome help...unfortunately my test app now gets the same error on this line:

B4X:
Dim Parent As B4XView = Pref.CustomListView1.GetPanel(i).GetView(1)

B4X:
--------- beginning of system
Copying updated assets files (2)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Error occurred on line: 55 (B4XMainPage)
java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
    at java.util.ArrayList.get(ArrayList.java:437)
    at anywheresoftware.b4a.objects.collections.List.Get(List.java:117)
    at b4a.example3.customlistview._getrawlistitem(customlistview.java:447)
    at b4a.example3.customlistview._getpanel(customlistview.java:440)
    at b4a.example.b4xmainpage._setwidthitem(b4xmainpage.java:251)
    at b4a.example.b4xmainpage._prefdialog2_beforedialogdisplayed(b4xmainpage.java:181)
    at b4a.example.b4xmainpage$ResumableSub_Button1_Click.resume(b4xmainpage.java:124)
    at b4a.example.b4xmainpage._button1_click(b4xmainpage.java:75)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7352)
    at android.widget.TextView.performClick(TextView.java:14230)
    at android.view.View.performClickInternal(View.java:7318)
    at android.view.View.access$3200(View.java:846)
    at android.view.View$PerformClick.run(View.java:27800)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7050)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
** Activity (main) Pause event (activity is not paused). **
** Service (starter) Destroy (ignored)**
Before PrefDialog2_BeforeDialogDisplayed(Null)
You have to do
prefdialog.ShowDialog(Options1, "OK", "CANCEL") 'add this line
See my post #13
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Thanks, too many posts, and I missed that one. For anyone else, here is a simple app with working code.

Thanks to all.
 

Attachments

  • test1.zip
    11 KB · Views: 125
Upvote 0

Magma

Expert
Licensed User
Longtime User
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
Here is a complete solution that works.
B4X:
Private Sub Button1_Click
    Dim PrefDialog2 As PreferencesDialog
    PrefDialog2.Initialize(Root, "BB Administrator", 350dip, 250dip)
    PrefDialog2.LoadFromJson( File.ReadString( File.DirAssets, "organizer.json"))

    Private Options1 As Map
    Dim List1 As List
    List1.Initialize
    Options1.Initialize
    List1.Add("option 1")
    List1.Add("long option 2")
    List1.Add("very long option 3")
    PrefDialog2.SetOptions("Event",List1)
    
    Dim dia As Object = PrefDialog2.ShowDialog(Options1, "OK", "CANCEL")
    SetWidthItem(PrefDialog2,"Event",250dip)
    
    Wait For( dia) Complete( Result As Int)
    If Result = xui.DialogResponse_Positive Then
'            PrintOptions(Options1, TextArea1)    
    End If
End Sub

Private Sub SetWidthItem (Pref As PreferencesDialog, Key As String, wwidth As Double)
    For i = 0 To Pref.PrefItems.Size - 1
        Dim pi As B4XPrefItem = Pref.PrefItems.Get(i)
        If pi.key = Key Then
            If pi.ItemType = Pref.TYPE_SHORTOPTIONS Then
                Dim Parent As B4XView = Pref.CustomListView1.GetPanel(i).GetView(1)
                Parent.Left = (Parent.Left + Parent.Width) - wwidth
                Parent.Width = wwidth
                Dim view As B4XView = Parent.GetView( 0)
                view.Width = Parent.Width
            Else
                Dim oldx As Double=Pref.CustomListView1.GetPanel(i).GetView(1).Left
                Dim oldw As Double=Pref.CustomListView1.GetPanel(i).GetView(1).Width
                Pref.CustomListView1.GetPanel(i).GetView(1).Left=(oldx+oldw)-wwidth
                Pref.CustomListView1.GetPanel(i).GetView(1).Width= wwidth
            End If
        End If
    Next
End Sub
 
Upvote 0
Top