Android Question Can I write code instead of using the designer?

The designer is awesome, I really like it but I want to write code to generate my UI so I can understand things better. I looked at the B4X XUI guide but it doesn't seem to talk about elements like buttons, labels, ect.
Can I create a page, place elements programmatically and not use the designer? Is there a good guide out there? Sorry If I didn't find it or if it is blatantly obvious.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
The beauty of B4X is that almost everything is possible. The designer can save a lot of time and headaches.
However I naturally move to code generated views. Partly because I can see what I am doing, but also because I know what I am doing.

The best discovery is xui, B4XView and B4XPages. Look at xui.CreatePanel("").
To create a label.
B4X:
Dim lbl as Label
lbl.Initialize("")
Dim lablx as B4XView = lbl
lblx.Text = "Hello there"
Root.AddView(lblx, 200, 300, 200, 50)  'where 200, 300, 200, 50 are left, top, width, height

It is also possible to create any of the native views. But some custom views require the designer initialization process,
For example, if I need a BBcodeview or a CustomListView, I just create a panel using xui.CreatePanel and load it with he designer layout which is a single
view with anchors that fills the panel, leaving a little margin for border control.

I suggest that the best approach is to experiment. It is easy to do with B4XPages in B4J.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Not using the Designer is a mistake.
Especially with CustomViews.

See this Tutorial
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
This argument will divide B4X Developers forever. šŸ˜
My 2 cents are that it's better to use the Designer as much as possible.
For sure for static layout part.
Adding view by code should be used for dynamic layout parts or in particular cases.
One of the best example to create views by code is to create the 90 numbers table for bingo, thinking to make it with 90 Buttons.
Doing it with designer would be tedious.
Anyway it's important to reach the objective.
Everyone will take the road the he/she like/prefer more.
 
Last edited:
Upvote 0

epiCode

Active Member
Licensed User
The designer is awesome, I really like it but I want to write code to generate my UI so I can understand things better. I looked at the B4X XUI guide but it doesn't seem to talk about elements like buttons, labels, ect.
Can I create a page, place elements programmatically and not use the designer? Is there a good guide out there? Sorry If I didn't find it or if it is blatantly obvious.
It purely depends on what are you creating finally:

Use code when:
Just a few controls
A lot of controls but they inherit properties from each other based on some logic
Controls need creation or removal based on actions/interaction
Need performance (like loading a lot of slightly complex cards in a customlistview)

Best to Use designer when:
Complex Layouts
Involves Custom Views (not all custom views are code friendly)
Layouts are reused within or outside the project (more manageable)
Maintaining Consistency and Look and Feel across pages (easier to see, select, change or copy paste)

Is there a good guide out there?
@William Lancee and @angel_ have already given some examples.

Moreover it works the same way:
Declare a control
Set its properties
Add it to a panel or activity/Root
 
Upvote 0
Not using the Designer is a mistake.
Especially with CustomViews.

See this Tutorial

It purely depends on what are you creating finally:

Use code when:
Just a few controls
A lot of controls but they inherit properties from each other based on some logic
Controls need creation or removal based on actions/interaction
Need performance (like loading a lot of slightly complex cards in a customlistview)

Best to Use designer when:
Complex Layouts
Involves Custom Views (not all custom views are code friendly)
Layouts are reused within or outside the project (more manageable)
Maintaining Consistency and Look and Feel across pages (easier to see, select, change or copy paste)


@William Lancee and @angel_ have already given some examples.

Moreover it works the same way:
Declare a control
Set its properties
Add it to a panel or activity/Root

I just want to learn the code relationship between the building of the UI and interactions. I fully plan on using the designer once I understand how the underlying code works, like I said the designer is pretty nice. I just don't like using things I don't fully understand. I appreciate the tips though!
 
Upvote 0
With the XUI Views library you can also do it like this::

B4X:
Dim lbl As B4XView = XUIViewsUtils.CreateLabel
lbl.Text = "Hello there"
Root.AddView(lbl, 200dip, 300dip, 200dip, 50dip)
I like this, very concise. Not that the more verbose one is bad though!

For the root, are UI roots per B4A Page files or can you technically make all your pages in one file declaring multiple roots? (Not that I would want to)
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I like this, very concise. Not that the more verbose one is bad though!

For the root, are UI roots per B4A Page files or can you technically make all your pages in one file declaring multiple roots? (Not that I would want to)
B4XPages layouts are the same as the Activity ones:
every page will load his own layout.
It's still possible to load the same layout in every page.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
For me the issue is simple. B4X and all other computer languages are tools I can use. What I do when and how I use them is not just
dependent what I want to create, but also on my mood, what's handy, and what I am most comfortable with. My most recent creation was:

https://www.b4x.com/android/forum/t...ormix-helper-app-a-standalone-standby.145624/

This is itself a tool. If you look at the source code, you'll see that labels are used as buttons and that it has buttons that are not even views.
It also has smart string constants instead of resource files. At the time I was coding, there were many possible alternatives. I choose methods that worked right away because I had used them recently, or simply because I was curious if it would work, because I needed variety, or by random chance.

I did use the designer to add a barebones BBCodeView to the project to provide an instruction manual.
That being said, I am not part of a group of programmers in an organization. If I was, my approach would have to be tempered by the needs of the organization and the complexity of teams.

Finally, the thread by @alwaysbusy gave me a little insight of why I find the separation of images from my procedures less comfortable than others might.

https://www.b4x.com/android/forum/threads/aphantasia-the-inability-to-visualize.145491/
 
Last edited:
Upvote 0
For me the issue is simple. B4X and all other computer languages are tools I can use. What I do when and how I use them is not just
dependent what I want to create, but also on my mood, what's handy, and what I am most comfortable with. My most recent creation was:

https://www.b4x.com/android/forum/t...ormix-helper-app-a-standalone-standby.145624/

This is itself a tool. If you look at the source code, you'll see that labels are used as buttons and that it has buttons that are not even views.
It also has smart string constants instead of resource files. At the time I was coding, there were many possible alternatives. I choose methods that worked right away because I had used them recently, or simply because I was curious if it would work, because I needed variety, or by random chance.

I did use the designer to add a barebones BBCodeView to the project to provide an instruction manual.
That being said, I am not part of a group of programmers in an organization. If I was, my approach would have to be tempered by the needs of the organization and the complexity of teams.

Finally, the thread by @alwaysbusy gave me a little insight of why I find the separation of images from my procedures less comfortable than others might.

https://www.b4x.com/android/forum/threads/aphantasia-the-inability-to-visualize.145491/
Very cool tool! I am using this for personal app creation, my job uses Java and COBOL. Despite being a Java dev I kinda hate making Android apps, Basic has always made developing fun and B4A is no exception. I also like how it can interface with custom Java code (although I haven't messed with that functionality).
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I am afraid that this thread is going to become a battle between Designer fans and code fans.
And for me there is no reason !
Designing user interfaces is a difficult task, especially in B4A with the multiple screen sizes and scales.
Then, each developer has its own habits, preferences etc.
You can do in code almost everything you can do in the Designer, only for many CustomViews you need the Designer.
Then it is up to you to do what you prefer !
Designer or Code ?
Use what you are comfortable with, no obligation to use anything else.

Coming back to the original post:
I looked at the B4X XUI guide but it doesn't seem to talk about elements like buttons, labels, ect.
XUI has nothing to to do with adding user interfaces by code or via the Designer.
And Buttons, Labels etc are B4xView in XUI, and no more Buttons, Labels !
The B4X XUI guide shows how to use cross-platform user interfaces, defined in the Designer or by code is not the question.
You can use both, without any restriction, it depends only on your preferences !!!
Which one is the BEST ?
The one you prefer !
 
Last edited:
Upvote 0
I am afraid that this thread is going to become a battle between Designer fans and code fans.
And for me there is no reason !
Designing user interfaces is a difficult task, especially in B4A with the multiple screen sizes and scales.
Then, each developer has its own habits, preferences etc.
You can do in code almost everything you can to in the Designer, only for many CustomViews you need the Designer.
Then it is up to you to do what you prefer !
Designer or Code ?
Use what you are comfortable with, no obligation to use anything else.

Coming back to the original post:

XUI has nothing to to do with adding user interfaces by code or via the Designer.
And Buttons, Labels etc are B4xView in XUI, and no more Buttons, Labels !
The B4X XUI guide shows how to use cross-platform user interfaces, defined in the Designer or by code is not the question.
You can use both, without any restriction, it depends only on your preferences !!!
Which one is the BEST ?
The one you prefer !
To be fair I never asked what's better, I just asked if you could because I hadn't found many examples of coding the GUI.
 
Upvote 0
Top