Android Question Button shape from XML?

Zdravko Filipovic

Member
Licensed User
Longtime User
Is it possible to add shape to button from xml file?

I mean, create button in designer (or code) and add its shape from xml file:

B4X:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#7995A8"
android:startColor="#E8E8E8"
android:endColor="#000000"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
<stroke
android:width="3dp"
android:color="#878787"
/>
</shape>
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yes, with XmlLayoutBuilder library.


Create this XML file and put it under Object\res\drawables. Make sure that the file is read-only.

Now you can load the drawable object and assign it to whichever view you like:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim xl As XmlLayoutBuilder
   Activity.Background = xl.GetDrawable("shape") 'file name is shape.xml
End Sub

Note that with the rapid debugger you need to click on Tools - Clean Project after you change the file. Otherwise the debugger may reuse the previous installed APK.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…