Please start with this tutorial: Custom View with Designer Support
Implementing custom views in a Java library is similar to custom views created in B4A.
The class must implement the DesignerCustomView interface.
This interface includes two methods:
void _initialize(BA ba, Object activityClass, String EventName)
void DesignerCreateView(PanelWrapper base, LabelWrapper lw, anywheresoftware.b4a.objects.collections.Map props)
Note that the _initialize method can be hidden by adding the @Hide annotation. The second method cannot be hidden.
For example, the following code creates a Switch view. This is a new view added in Android 4:
Implementing custom views in a Java library is similar to custom views created in B4A.
The class must implement the DesignerCustomView interface.
This interface includes two methods:
void _initialize(BA ba, Object activityClass, String EventName)
void DesignerCreateView(PanelWrapper base, LabelWrapper lw, anywheresoftware.b4a.objects.collections.Map props)
Note that the _initialize method can be hidden by adding the @Hide annotation. The second method cannot be hidden.
For example, the following code creates a Switch view. This is a new view added in Android 4:
B4X:
package anywheresoftware.b4a.objects;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.CompoundButton.OnCheckedChangeListener;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.keywords.Common.DesignerCustomView;
import anywheresoftware.b4a.keywords.constants.Colors;
@ShortName("Switch")
@Events(values={"CheckedChange (Value As Boolean)"})
public class SwitchWrapper implements DesignerCustomView {
private BA ba;
private String eventName;
private Switch switchView;
@Hide
public void _initialize(BA ba, Object activityClass, String EventName) {
this.eventName = EventName.toLowerCase(BA.cul);
this.ba = ba;
}
//this method cannot be hidden.
public void DesignerCreateView(PanelWrapper base, LabelWrapper lw, anywheresoftware.b4a.objects.collections.Map props) {
switchView = new Switch(ba.context);
switchView.setText(lw.getText());
switchView.setTypeface(lw.getTypeface());
switchView.setTextSize(lw.getTextSize());
switchView.setTextColor(lw.getTextColor());
switchView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
ba.raiseEvent(this, eventName + "_checkedchange", arg1);
}
});
base.AddView(switchView, 0, 0, base.getWidth(), base.getHeight());
base.setColor(Colors.Transparent);
}
public void setChecked(boolean value) {
switchView.setChecked(value);
}
public boolean getChecked() {
return switchView.isChecked();
}
}