Android Tutorial Wrapping a small and easy 3rd party library with java

I am going to try and wrap this (CircularButton) from here
https://github.com/Alexrs95/Circula...alexrs/circularbutton/lib/CircularButton.java
Found it on the android-arsenal.com website. Will in all probability call on wrapper Doctor @DonManfred's help to work through this :)....that is if you don't mind....

I wrote a small tutorial for @Johan Schoeman then... Then i thought this could be interesting to more than just Johan.

So here we go:


1.Original
The content of the zip from Github

2.Find the main source folder
Find the folder from the LIB (if there are lib and demo) where the res-folder is

3. use Rgenerator
copy RGenerator.exe to this folder and run it (you find rgenerator.exe in forum)

4.Cleanup
remove not used parts

5.spacer

6.Start of a new Eclipse-project
This is basically a copy of my HTMLTextView library. This is the base of all wraps i do usually...
Note: Some parts of the file in de\donmanfred\ still needs to be updated to match "the current wrapper"

So i prepare a new project directory structure

Copy the folder Circularbutton to your Eclipse workspace
in eclipse do a import of a available project, select the path of your workspace\Circularbutton
Finish

Now you are in eclipse
After you have imported and opened your project we see some errors

Johann1.png



We will start with the R.java

Johann2.png



Use the Quickfix "Import 'BA' (anywheresoftware.b4a)"


Next the system says the the r.java should get the right package.
Johann3.png


Add the suggested packagedeclaration

Save the r.java. The errors should gone for this file now.

Johann4.png


Looking at the Errors we see an indicator for an higher android-version
than we refer to at start (the base is using android SDK 8)

So we change the Build-Paths and select a newer android.jar
In this case i usually use android 15 (4.0.3+ or so) but for this wrapper we need to set
android-sdk\platforms\android-16\android.jar

After setting this error disappears too...

I already said earlier that my wraps usually are based on HTMLTextView library.
Some artefacts still are in the new wrappe we are writing

To know what we need to change and to what
we look at the java file from the lib (in this case it is just one). Here we find information
of what object it goes...
public class CircularButton extends ImageView {

This lib is an CircularButton which is based on an ImageView.
So basically it is a VIEW too.

in the file de\donmanfred\Circularbutton.java

we start to edit

public class htmltv implements DesignerCustomView {
will get changed to the new class we want

public class CircularButtonWrapper implements DesignerCustomView {

Johann5.png


we now use the quickfix to let eclipse rename the file correct.

Now:
private HtmlTextView mtext;
this is the relict we need to adapt

we start with a new definition using the right objecttype
private CircularButton cb;

and use the quickfix to import

Johann6.png


after replacing all occurencies of mtext with cb
we still have the line
cb = new HtmlTextView(ba.context);

change it to
cb = new CircularButton(ba.context);

to match the right objectdeclaration

As we are extending a view here we use a viewwrapper
here

changing
B4X:
public class CircularButtonWrapper implements DesignerCustomView {
to
B4X:
public class CircularButtonWrapper extends ViewWrapper<CircularButton> implements DesignerCustomView {

Use the quickfix to import anywheresoftware.b4a.objects.ViewWrapper;

from the libs github page we know that we need to allow changes


Button color

button.setButtonColor(Color.BLACK);

Shadow color

button.setShadowColor(Color.BLUE);

in fact we need at least two methods to do this

B4X:
   public void setButtonColor(int color)   {
   
   }
 
   public void setShadowColor(int color)   {
   
   }


now we fill life into it

B4X:
   public void setButtonColor(int color)   {
     cb.setButtonColor(color);
   }
 
   public void setShadowColor(int color)   {
     cb.setShadowColor(color);   
   }

we need a helper more...

B4X:
   public void setImageBitmap(Bitmap bm)   {
     cb.setImageBitmap(bm);
     cb.invalidate();
   }

now the a little bit tricky parts

in the lib we see

B4X:
  public CircularButton(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  init(context, attrs);
  }
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private void init(Context context, AttributeSet attrs) {
  setScaleType(ScaleType.CENTER_INSIDE);
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  }
  mButtonPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  mButtonPaint.setStyle(Paint.Style.FILL);
  if (attrs != null) {
  TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircularButton);
  buttonColor = a.getColor(R.styleable.CircularButton_buttonColor, buttonColor);
  shadowColor = a.getColor(R.styleable.CircularButton_shadowColor, shadowColor);
  a.recycle();
  }
  setButtonColor(buttonColor);
  }
R.styleable.CircularButton shows an error. The r.java is not 100% ok. In this code the lib will look
into the xml definition of the styleable object Circularbutton.
We dont want to use xml-files. So we try to replace them.
Only two properties are available. We replace them with hardcoded values (leaving the defaults for now)

B4X:
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private void init(Context context, AttributeSet attrs) {
  setScaleType(ScaleType.CENTER_INSIDE);
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  }
  mButtonPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  mButtonPaint.setStyle(Paint.Style.FILL);
  setButtonColor(buttonColor);
  }


back to the "own" file

Adapting
B4X:
@Version(1.00f)
@ShortName("Circularbutton")
to set the correct object and libraryversion when using b4a later.

Now we are in a stage with no errors. As i´m writing this text here i am not sure if it works...

Let´s try it

Johann7.png


and click on compile... when you have a look at this BIG xml-file you see that we need to exclude the original references....

Add

,me.alexrs
^ comma
at the end of the -b4aignore line... me.alexxrs is the classpath of the original-library we are wrapping.

compile again

Now the xml looks much better. no not needed references.

At this point i will do a test b4a-project (10.Example.zip)


Last but not least: A spoiler :D

Circularbutton001.png
 

Attachments

  • 1.Original.zip
    179.4 KB · Views: 605
  • 2.Find the main source folder.zip
    3.4 KB · Views: 486
  • 3. use Rgenerator.zip
    3.1 KB · Views: 528
  • 4.Cleanup.zip
    2.9 KB · Views: 478
  • 5.Start of a new Eclipse-project.zip
    13.4 KB · Views: 468
  • 6.Working in Eclipse.zip
    91.7 KB · Views: 489
  • 7.New Wrapper.zip
    20.1 KB · Views: 507
  • 9.Resulting Library.zip
    5.2 KB · Views: 462
  • 10.Example CircularButtonEx.zip
    12.1 KB · Views: 478

Johan Schoeman

Expert
Licensed User
Thank my friends
I download attach example and compile with slc but again it get error
Problem is android.annotation.TargetApi
How do i reference a higher android.jar in eclipse while i use slc and dont use eclipse
what's problem my dear friends?
What does your B4A configuration look like? In B4A: Tools ---> Configure Paths

Bel.png

 

Johan Schoeman

Expert
Licensed User
add the android.jar to the libs folder of your project when compiling (just a guess; i use eclipse to code and slc to compile from within eclipse)
Should not be necessary @DonManfred. I use Notepad++ for editing and compile with SLC and have never had to add android.jar to the libs folder for a project to compile
 

Bel

Member
Licensed User
Thanks guys,with copy android.jar into libs folder in my project,my problem solved
Again thanks
 

Cassie

Member
Licensed User
Hi Guys,

Anyone could explain to me how can I use any third party .jar files. I bought a Mini Thermal printer and i want to integrate it to B4a apps in printing but it seems the jar file has no xml file. How could I do this?

Thanks in advance.
 

MarcoRome

Expert
Licensed User
Hi Guys,

Anyone could explain to me how can I use any third party .jar files. I bought a Mini Thermal printer and i want to integrate it to B4a apps in printing but it seems the jar file has no xml file. How could I do this?

Thanks in advance.

JavaObject look THIS
 
Top