Android Question Getting Started with B4XPages

ProjectGroup19

Active Member
Licensed User
Hello Community,
Please, i want to get started with b4xpages but i still not getting things clear. I would be very grateful if you give me a headstart by helping me in converting this activity code to b4xpages.
The code uses HttpJob.

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals
 
End Sub

Sub Globals
   
   
    Private pnlStatus As Panel
    Private pnlLAll As Panel
   
    Private ScrollingLabel1 As ScrollingLabel
    Private btnForgotPassword As Button
    Private CustomListView1 As CustomListView
    Private txtUsername As B4XFloatTextField
    Private txtPassword As B4XFloatTextField
    Private chbRemember As CheckBox
    Private lblStatus As Label
   
    Dim ch As Int=0 'check the state of checkbox
   
    Private pnlHeader As Panel
    Private B4XImageView1 As B4XImageView
    Private Label3 As Label

    Private pnlAllCustom As Panel
   
    Dim ime As IME
End Sub

Sub Activity_Create(FirstTime As Boolean)
    #If Java
import android.annotation.TargetApi;
import android.content.Context;
import android.view.WindowManager.*;
        public void _onCreate( ) {
                this.getWindow( ).setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
                }
   
   #End If
 
    Activity.LoadLayout("login")
    pnlStatus.Visible=False
    pnlLAll.Top=pnlStatus.Top
   
    pnlHeader.Color=Starter.headerColour
    Label3.TextColor=Starter.headerTextColour
    ime.Initialize("ime")
   
    B4XImageView1.CornersRadius=20
    B4XImageView1.RoundedImage=True
    B4XImageView1.Load(File.DirAssets,Starter.sLogo)
    B4XImageView1.ResizeMode="FILL"
    B4XImageView1.Update
   
    CustomListView1.Add(CreateListItem(CustomListView1.AsView.Width,CustomListView1.AsView.Height+50dip), 0)'+120dip), 0)
'  
    ScrollingLabel1.TextColor=Starter.footerColour



End Sub

Sub Activity_Resume
    txtUsername.TextField.RequestFocus
    'txtUsername.EditText.ForceDoneButton=False

    For Each v As View In Activity.GetAllViewsRecursive
        If v.Tag Is ScrollingLabel Then
            Dim sl As ScrollingLabel =v.tag
            sl.Text=sl.Text
        End If
    Next
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

'BACK KEYPRESS
Sub Activity_KeyPress(KeyCode As Int)' As Boolean
    Dim Answ As Object
    Dim Txt As String
   
    If KeyCode = KeyCodes.KEYCODE_BACK Then    ' Checks if the KeyCode is BackKey
        Txt = "Do you really want to close the program?"
        Answ =Msgbox2Async(Txt, "A T T E N T I O N", "Yes", "", "No",null,False ) ' MessageBox
        Wait For (Answ) msgbox_result(result As Int)
        If result= DialogResponse.POSITIVE Then
            Activity.Finish
           
            StartActivity(Logout)
        End If
   
    End If
   

End Sub

'Forgot Password--LOGIN
Sub rbnForgetPass_CheckedChange(Checked As Boolean)
    StartActivity("ForgotPassword")
  
End Sub

'Login--LOGIN
Sub btnLogin_Click
   
    If txtUsername.Text.Trim = "" Then
        MsgboxAsync("Please, enter your username.", "E R R O R")
        Return
    End If

    If txtPassword.Text.trim = "" Then
        MsgboxAsync("Please, enter your password.", "E R R O R")
        Return
    End If


    Dim Job As HttpJob
    Job.Initialize("Login", Me)
   
    Job.PostString(Main.strURL & "ACCOUNT/signin.php", $"Action=Register&USERNAME=${txtUsername.Text.Trim}&PASSWORD=${txtPassword.Text.Trim}"$)

    ProgressDialogShow2("Authenticating Credentials......",False)
    Wait For(Job) JobDone (JobLogin As HttpJob)
   
    If Job.Success Then
        ProgressDialogHide
        Dim ret As String
        'Dim act As String
        ret =Job.GetString
        'Log(ret)
        Dim parser As JSONParser
        parser.Initialize(ret)
   
        Dim Data As List
        Data = parser.NextArray 'returns a list with ma
           
        For Each m As Map In  Data
       
        
       
        Dim  NotFound As String=m.Get("NOT_FOUND")
        Dim Error As String=m.Get("ERROR")
        Dim Success As String=m.Get("SUCCESS")
      

Next
      
  
            If NotFound = "Not Found" Then
                MsgboxAsync("Invalid username or password. Please, check your credentials and Try again!", "LOGIN")
                txtUsername.Text=""
                txtPassword.Text=""
                txtUsername.TextField.RequestFocus
                ToastMessageShow("Login failed", True)
                Return
           
         
          
               
            Else If Success="Success" Then
         

               
            ime.HideKeyboard  
'              
           
            Starter.Ulogin=1     'Checking login status
           
        
           
          
        
           
            StartActivity(DashBoard)
           
               
            txtUsername.Text=""
            txtPassword.Text=""
             
            Activity.Finish

      Else If Error = "Error" Then
                MsgboxAsync("Login Failed...Please, check you internect connection and try again !", "LOGIN")
                ToastMessageShow("Check your Internet connection!", True)
                   
        End If
       
   
   
    Else
        ToastMessageShow("Not Connected. Please, check your internet connection", True)
    End If
    Job.Release
ProgressDialogHide
End Sub

'Create Account--LOGIN
Sub btnCreateAccount_Click
    StartActivity("Register")

End Sub

Sub CreateListItem( Width As Int, Height As Int) As Panel
' 
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, Width, Height)
    p.LoadLayout("customLogin")
   
    chbRemember.Checked=True
    ch=1
   
    Dim PnlHeight As Int = pnlAllCustom.Height+ 7dip
    p.SetLayout(0,0,Width,PnlHeight)
    Return p

End Sub

'Forgot Password
Sub btnForgotPassword_Click
    StartActivity(ForgotPassword)
End Sub

Sub txtUsername_EnterPressed
   
End Sub


Sub chbRemember_CheckedChange(Checked As Boolean)
   
If Checked=True  Then
        ch=1
                Else
        ch=0
End If
   
End Sub
 
Last edited:

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Hi there,

Here is what I would do:

(read and try the example here: https://www.b4x.com/android/forum/t...framework-for-managing-multiple-pages.118901/)
  • Create a new project as B4xPages
  • copy all of the code here into B4xMainPage
  • It looks like you will need additional B4xPages for any other activity e.g. "Forgot password", but it should work as an initial test, normally use b4xpages.showpage() . Example of this in link above.
  • Globals becomes Class_globals
  • Copy Activity_create code to B4xPage_created
  • Change all reference from Activity to Root e.g. Change Activity.LoadLayout to Root.loadLayout
  • Activity_resume becomes B4xPage_appear
  • Activity_pause becomes B4xpage_disappear
  • Activity.finish becomes b4xpages.closepage(me)
  • The code in key press should change to this https://www.b4x.com/android/forum/t...or-managing-multiple-pages.118901/post-743793 to deal with close request.
Hope that helps.
 
Upvote 0

ProjectGroup19

Active Member
Licensed User
Hi there,

Here is what I would do:

(read and try the example here: https://www.b4x.com/android/forum/t...framework-for-managing-multiple-pages.118901/)
  • Create a new project as B4xPages
  • copy all of the code here into B4xMainPage
  • It looks like you will need additional B4xPages for any other activity e.g. "Forgot password", but it should work as an initial test, normally use b4xpages.showpage() . Example of this in link above.
  • Globals becomes Class_globals
  • Copy Activity_create code to B4xPage_created
  • Change all reference from Activity to Root e.g. Change Activity.LoadLayout to Root.loadLayout
  • Activity_resume becomes B4xPage_appear
  • Activity_pause becomes B4xpage_disappear
  • Activity.finish becomes b4xpages.closepage(me)
  • The code in key press should change to this https://www.b4x.com/android/forum/t...or-managing-multiple-pages.118901/post-743793 to deal with close request.
Hope that helps.
Thank you for the head start, @Andrew (Digitwell)
 
Upvote 0
Top