Android Question How do I put a listview below a edittext

Ricky D

Well-Known Member
Licensed User
Longtime User
I want to create a library to do this. I've got the edittext working but cannot get the listview to show
 

stevel05

Expert
Licensed User
Longtime User
How have you set it up so far? In code or a layout?
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
In a library I created the edittext in code.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
How are you adding the listview?
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
B4X:
Private Sub CreateGUI
    mBase.Color = Colors.Transparent
 
    et.Initialize("et")
    mBase.AddView(et, 0, 0, mBase.Width, mBase.Height)
    et.SingleLine = True
    et.TextSize = 14 * TextSizeScaleFactor
 
    'pnlBelow.Initialize("below")
    'mBase.AddView(pnlBelow,mBase.Left,200,mBase.Width,400)
    'pnlBelow.Color = Colors.Yellow
 
    lv.Initialize("lv")
    mBase.AddView(lv,et.Left,et.Top+et.Height+10,et.Width,400)
 
    'setKBSuggestions(mKBSuggestions)
 
    'setForceDoneButton(mForceDoneButton)
 
    'setKBFullScreen(mKBFullScreen)
 
    'SetEditTextNoUI(etForeground)
End Sub

This gets called from the DesignerCreateView sub
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The problem you will have is that the base will need to be resized to fit both the edit text and the listview. You would be better to get the activity from the props map (Props.Get("activity")) and add the listview directly to that. You will need to get the absolute position of the edittext within the activity to position the listview.

The code here should help. https://www.b4x.com/android/forum/threads/get-relative-view-position.39389/
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Thanks. Do I add a new activity to do this?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
No, the props map holds the activity that the layout is loaded into.
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
B4X:
#DesignerProperty: Key: AC, DisplayName:Activity, FieldType: Activity, DefaultValue: AC, Description: The Activity

creates this error

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 31 (AutoComplete2)
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewParent android.view.View.getParent()' on a null object reference
at android.view.ViewGroup.addViewInner(ViewGroup.java:4186)
at android.view.ViewGroup.addView(ViewGroup.java:4040)
at android.view.ViewGroup.addView(ViewGroup.java:4016)
at anywheresoftware.b4a.objects.PanelWrapper.AddView(PanelWrapper.java:65)
at b4a.example.autocomplete2._designercreateview(autocomplete2.java:192)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at b4a.example.main.afterFirstLayout(main.java:102)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
** Activity (main) Resume **
this is my designer code
B4X:
Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
    mBase = Base
    mBase.AddView(Props.Get("AC"),mBase.Left,mBase.Top,mBase.Width,mBase.Height)
    CreateGUI
End Sub

B4X:
Private Sub CreateGUI
    mBase.Color = Colors.Transparent
   
    et.Initialize("et")
    mBase.AddView(et, 0, 0, mBase.Width, mBase.Height)
    et.SingleLine = True
    et.TextSize = 14 * TextSizeScaleFactor
    'pnlBelow.Initialize("below")
    'mBase.AddView(pnlBelow,mBase.Left,200,mBase.Width,400)
    'pnlBelow.Color = Colors.Yellow
   
    lv.Initialize("lv")
    mBase.AddView(lv,et.Left,et.Top+et.Height+10,et.Width,400)
   
    lv.AddSingleLine("Hello")
   
    'setKBSuggestions(mKBSuggestions)
   
    'setForceDoneButton(mForceDoneButton)
   
    'setKBFullScreen(mKBFullScreen)
   
    'SetEditTextNoUI(etForeground)
End Sub
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
If I could put both the edittext and listview on the activity I would
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The activity object is already in the props map, you don't need to do anything to create it. Dim Act As Activity in the Class_Globals and Act = Props.Get("activity") in the Designer create view.

The attached file get's that far with the edittext in a layout. It will automatically be in the Base Panel when it's loaded
 

Attachments

  • AC.zip
    9.6 KB · Views: 140
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Many thanks I will continue to use your code to put the features I need
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
OK, if you load the customview into a panel rather than directly onto the activity you will need to use the subroutines I posted above to get it's position.
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
ok thanks. Now I need to figure out how to get events to work and to be able to make the listview invisible and also positioned above or below depending on where the view is placed.

I can make the listview visible/invisible from code but I really need for it to happen when I type in the edittext or backspace it. I also need to get the clicked item and put it into the edittext then close the listview.

I've tried it inside the library and also from the main activity but no events fire
 
Last edited:
Upvote 0
Top