Android Question [Solved]How to refer Image files to each platform?

Theera

Expert
Licensed User
Longtime User
My Code is belows,I have tried to learn changing a CustomView to be B4XCustomView

B4X:
Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
    mBase = Base
    Tag = mBase.Tag : mBase.Tag = Me
    Dim p As B4XView = xui.CreatePanel("p")
    p.Color = xui.Color_Transparent
    
    CornersRadius = DipToCurrent(Props.GetDefault("CornersRadius", 15))
    mDisabled = Not(Props.GetDefault("ButtonEnabled", True))
    mHaptic = Props.GetDefault("HapticFeedback", False)
    pressed = mDisabled
    
    ' กำหนดค่าเริ่มต้นของ switch
    mSwitchState = Props.GetDefault("InitialValue", False)
    If mSwitchState Then
        mValue = "on"
    Else
        mValue = "off"
    End If
    
    ' โหลดรูปภาพทั้งสองตัว
    imgFile1 = Props.GetDefault("SwitchImage1", "switch1.png")
    imgFile2= Props.GetDefault("SwitchImage2", "switch2.png")
    
  '  If File.Exists(File.DirAssets, imgFile1) Then  '<=== For Android is correct
    If File.Exists(xui.DefaultFolder, imgFile1) Then '<==I have tried this,but I can't be success
        'bmpSwitch1 = xui.LoadBitmapResize(File.DirAssets, imgFile1, mBase.Width, mBase.Height, True)  '<=== For Android is correct
        bmpSwitch1 = xui.LoadBitmapResize(xui.DefaultFolder, imgFile1, mBase.Width, mBase.Height, True)  '<==I have tried this,but I can't be success
    Else
        bmpSwitch1 = Null
    End If
    
    If File.Exists(xui.DefaultFolder, imgFile2) Then
        'bmpSwitch2 = xui.LoadBitmapResize(File.DirAssets, imgFile2, mBase.Width, mBase.Height, True)    '<=== For Android is correct
        bmpSwitch2 = xui.LoadBitmapResize(xui.DefaultFolder, imgFile2, mBase.Width, mBase.Height, True) '<==I have tried this,but I can't be success
    Else
        bmpSwitch2 = Null
    End If
    
    xLBL = Lbl
    xLBL.Visible = True
    mBase.AddView(xLBL, 0, 0, 0, 0)
    mBase.AddView(p, 0, 0, 0, 0)
    xLBL.SetTextAlignment("CENTER", "CENTER")
    cvs.Initialize(mBase)
    Base_Resize(mBase.Width, mBase.Height)
End Sub
 
Solution
Here I have made your CustomView as a B4XLIB.
Copy it to your Additional Libraries >>> B4X folder
addlibs.jpg

so it will be available in all IDEs.
After this you can check my test example.
You can see that in the IDE there are only the 2 files THA.PNG and ENG.PNG
In the example there are 2 buttons (your CustomView):
the first one has the Switch fields left empty, and will use the default images that are embedded into the CustomView.
sw1.jpg


The second button instead has the switches set to the extra image that the user will provide (tha.pmg and eng.png in this case)
sw2.jpg

Hope this work for you and help you.
And hope I have done everything correctly.
Today is really too hot to stay at pc.

Theera

Expert
Licensed User
Longtime User
I would like to sorry my terrible. I can do it correct in B4A,but I can't do it in B4J (and B4i )
I can't explain in my problem,so I have attached the project for someone help to solved.

 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
B4X:
File.Exists
has no effect on
B4X:
File.DirAssets
because only you can put files in that folder, so it should be obvious that you know exactly what is in there and what is not.
You can try substitute
B4X:
If File.Exists(xui.DefaultFolder, imgFile1) Then
with
B4X:
If imgFile1 <> "" Then

*** EDIT ***
For this to work you also need to modify these 2 lines in this way
B4X:
#DesignerProperty: Key: SwitchImage1, DisplayName: Switch Image 1 (FileName), FieldType: String, DefaultValue: , Description: ชื่อไฟล์รูป Switch ตัวที่ 1 สำหรับสถานะ OFF (ใส่ใน DirAssets)
#DesignerProperty: Key: SwitchImage2, DisplayName: Switch Image 2 (FileName), FieldType: String, DefaultValue: , Description: ชื่อไฟล์รูป Switch ตัวที่ 2 สำหรับสถานะ ON (ใส่ใน DirAssets)
setting the DefaultValue to empty.
Note that now you set them to switch1.png and switch2.png but you have no such files in your DirAssets.
 
Last edited:
Upvote 0

Theera

Expert
Licensed User
Longtime User
B4X:
File.Exists
has no effect on
B4X:
File.DirAssets
because only you can put files in that folder, so it should be obvious that you know exactly what is in there and what is not.
You can try substitute
B4X:
If File.Exists(xui.DefaultFolder, imgFile1) Then
with
B4X:
If imgFile1 <> "" Then
My problem is in B4J ,after run app,it isn't showing any images while in B4A ,it's correct.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Tested even with B4J, it works here.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I am checking your code better and probably there is a lot to change to make is easier to understand and to work.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I decided to make just a minimum correction not to invalidate all what you have done.
Try this example.
To make it possible to attach the file here I removed all the images that was included in your example.
Remember to add the same images again to this example to work.
Let me know.
 

Attachments

  • TestB4XUISwitch.zip
    24.8 KB · Views: 19
Upvote 0

Theera

Expert
Licensed User
Longtime User
I decided to make just a minimum correction not to invalidate all what you have done.
Try this example.
To make it possible to attach the file here I removed all the images that was included in your example.
Remember to add the same images again to this example to work.
Let me know.
My problem still have been,because I need to created it as B4XUISwitch library for insteading of original UISwitch library which used only B4A.
When I need to change images that aren't SwitchImage1.png and SwitchImage2.png. I always rename them.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Here I have made your CustomView as a B4XLIB.
Copy it to your Additional Libraries >>> B4X folder
addlibs.jpg

so it will be available in all IDEs.
After this you can check my test example.
You can see that in the IDE there are only the 2 files THA.PNG and ENG.PNG
In the example there are 2 buttons (your CustomView):
the first one has the Switch fields left empty, and will use the default images that are embedded into the CustomView.
sw1.jpg


The second button instead has the switches set to the extra image that the user will provide (tha.pmg and eng.png in this case)
sw2.jpg

Hope this work for you and help you.
And hope I have done everything correctly.
Today is really too hot to stay at pc.
 

Attachments

  • NewTest.zip
    292.9 KB · Views: 21
  • B4XUISwitch.b4xlib
    15.3 KB · Views: 27
Upvote 0
Solution

Theera

Expert
Licensed User
Longtime User
Here I have made your CustomView as a B4XLIB.
Copy it to your Additional Libraries >>> B4X folder
View attachment 166117
so it will be available in all IDEs.
After this you can check my test example.
You can see that in the IDE there are only the 2 files THA.PNG and ENG.PNG
In the example there are 2 buttons (your CustomView):
the first one has the Switch fields left empty, and will use the default images that are embedded into the CustomView.
View attachment 166118

The second button instead has the switches set to the extra image that the user will provide (tha.pmg and eng.png in this case)
View attachment 166119
Hope this work for you and help you.
And hope I have done everything correctly.
Today is really too hot to stay at pc.
Thank you the kind of you,Sagenut. I get understand your strategy that giving canceled default values of images.
Your solved helps me grow in coding.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
ผมได้ปรับโค้ดของ Sagenut ให้เป็นแบบนี้แล้ว แต่ผมยังคงใช้แนวคิดของ Sagenut อยู่

[รหัส]
' ดาวน์โหลดรูปภาพประกอบร่างกาย
imgFile1 = Props.GetDefault("SwitchImage1","SwitchImage1")
imgFile2= Props.GetDefault("SwitchImage2","SwitchImage2")


หาก imgFile1 <>"Img_ON.png" แล้ว
'bmpSwitch1 = xui.LoadBitmapResize(ไฟล์.DirAssets, imgFile1, ความกว้าง mBase, ความสูง mBase, จริง)
bmpSwitch1 = xui.LoadBitmapResize(ไฟล์.DirAssets, Props.Get("SwitchImage1"), mBase.Width, mBase.Height, True)
สิ้นสุดถ้า



หาก imgFile2 <>"Img_OFF.png" แล้ว
'bmpSwitch2 = xui.LoadBitmapResize(ไฟล์.DirAssets, imgFile2, ความกว้าง mBase, ความสูง mBase, จริง)
bmpSwitch2 = xui.LoadBitmapResize(ไฟล์.DirAssets, Props.Get("SwitchImage2"), mBase.Width, mBase.Height, True)
สิ้นสุดถ้า
[/รหัส]
 
Last edited:
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
imgFile1 = Props.GetDefault("SwitchImage1","SwitchImage1") imgFile2= Props.GetDefault("SwitchImage2","SwitchImage1")
Check your code.
You wrote SwitchImage1 for both images.
 
Upvote 0
Top