How to use IDroid UI binding API

Viewed 53

How can i use IDroid framework to take android UI elements Binding , onClick listener binding in the project.

  implementation 'com.hadoopz:IDroid:1.0.62'
1 Answers

Make sure you have proper dependency in build.gradle file

 implementation 'com.hadoopz:IDroid:1.0.62'

Create your UI layout xml file. res/layout/login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
    android:id="@+id/btn1"
    android:text="Test1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<Button
    android:id="@+id/btn2"
    android:text="Test2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>

Create your activity in the project and declare the activity in AndroidManifest.xml ,for example ,activity class name :MyActivity.java

Import those annotation into the class:

import com.hadoopz.MyDroidLib.inject.annotation.ContentView;
import com.hadoopz.MyDroidLib.inject.annotation.Event;
import com.hadoopz.MyDroidLib.inject.annotation.ViewInject;
import com.hadoopz.MyDroidLib.inject.y;

Put the layout annotation to class header as below:

@ContentView(R.layout.login)
public class MyActivity extends Activity {.....}

Initialize the IDroid UI framework in android onCreate method as below :

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    requestWindowFeature(1);
    y.view().inject((Activity) this);
}

Inject the UI elements to activity attributes s below:

@ViewInject(R.id.btn1)
private TextView btn1;
@ViewInject(R.id.btn2)
private TextView btn2;

Binding onClick listener to a private method in the activity as below: please make sure the method is private.

  @Event({R.id.btn1, R.id.btn2})
private void viewListeners(View view) {
    switch (view.getId()) {
        case R.id.btn1:
            Log.d(getClass().getSimpleName(), "button2 is clicked" );
            break;
        case R.id.btn2 :
            Log.d(getClass().getSimpleName(), "button2 is clicked" );
            break;
        default:
            break;
    }
}

This is how we do it in Kotlin:

@Event(value = intArrayOf(R.id.btn1, R.id.btn2))
private fun allListener(view: View?) {

    when (view?.getId()) {
        R.id.btn1 -> {
            Log.d(getClass().getSimpleName(), "button1 is clicked" );
        }
        R.id.btn2 -> {
            Log.d(getClass().getSimpleName(), "button2 is clicked" );
        }
    }

After doing that you can delete code like this:

Button btn1 = (Button)findViewById(R.id.btn1);
Button btn2 = (Button)findViewById(R.id.btn2);
btn1.setOnClickListener(.....);
btn2.setOnClickListener(.....);
Related