InfiniDroid

Productivity library for your Android developments

AccueilNos formationsInfiniDroidLicense d'exploitation de nos logicielsVos développements sur mesuresNos ressources (tutoriaux et exemples)Ils nous font confianceNous contacterMentions LégalesA propos de ce site

The InfiniDroid library permits to link your Android Widgets per annotation rather than use the findViewById method (other others methods). To use the library InfiniDroid, please follow the instructions below.

1 - Download th InfiniDroid.jar library:         48 downloads

2 - Launch Android Studio and create a new project based on a blank activity. At the Project Explorer (in the top/left corner), go to "Project" view, as shown in following screenshot.

3 - With a drag and drop operation, place the InfiniDroid.jar library in the lib directory. Then click the right mouse button on the library and select "Add As Library ...": confirm your choice. Then restore the "Android" view.

4 - Edit your layout resource to add the following widgets.

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView android:id="@+id/lblTest"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/hello_world"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button android:id="@+id/btnTest"
        android:text="@string/hello_world"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lblTest"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <fragment
        android:id="@+id/fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="android.webkit.WebViewFragment"
        android:layout_below="@+id/btnTest"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>
Contenu de votre ressouce de layout.

5 - You can now use annotations provided by the InfiniDroid library for bind your Widgets to your Java code.

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
package com.infinisoftware.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebViewFragment;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.infinisoftware.android.binding.AndroidFragment;
import com.infinisoftware.android.binding.AndroidListener;
import com.infinisoftware.android.binding.AndroidWidget;
import com.infinisoftware.android.binding.Binder;


public class MainActivity extends Activity {

    @AndroidWidget private TextView lblTest;
    @AndroidWidget private Button btnTest;
    @AndroidFragment private WebViewFragment fragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Binder.bind( this );
    }

    @AndroidListener
    private View.OnClickListener btnTest_onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            lblTest.setText("Button Clicked");
            Toast.makeText(MainActivity.this, 
                    "Button Clicked " + fragment.toString(), Toast.LENGTH_LONG).show();
        }
    };

}
Code Java de votre activité.

Some explanations may be necessary.

  • The binding will be realized with the line 28 (Binder.bind( this);)
  • The @AndroidWidget annotation link the widget with your Java attribute, at the condition that the names are the same (id attribute of the widget and name of the attribut).
  • To link a fragment, it is necessary to use the @AndroidFragment annotation.
  • The @AndroidListener annotation link an event listener to an event generator. For this to work, the name of your listener must start with the name of the widget and then be followed by the character _ and finally end with the name of the listening interface (with a lower case for the first letter).

Finally, note that it is possible to not use the com.infinisoftware.android.binding.Binder class, but in this case, your activity will necessarily derive from the com.infinisoftware.android.activities.AppCompatActivityWithBinding class.

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
package com.infinisoftware.myapplication;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebViewFragment;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.infinisoftware.android.activities.AppCompatActivityWithBinding;
import com.infinisoftware.android.binding.AndroidFragment;
import com.infinisoftware.android.binding.AndroidListener;
import com.infinisoftware.android.binding.AndroidWidget;
import com.infinisoftware.android.binding.Binder;


public class MainActivity extends AppCompatActivityWithBinding {

    @AndroidWidget private TextView lblTest;
    @AndroidWidget private Button btnTest;
    @AndroidFragment private WebViewFragment fragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Binder.bind( this );        // Now, this line is not needed.
    }

    @AndroidListener
    private View.OnClickListener btnTest_onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            lblTest.setText("Button Clicked");
            Toast.makeText(MainActivity.this, 
                    "Button Clicked " + fragment.toString(), Toast.LENGTH_LONG).show();
        }
    };

}
Code Java de votre activité.

Do not hesitate to share this page, for help us. For example you can like this page on Facebook or Google+ by clicking the buttons below.