how to write common onclicklistener for same multiple include in data binding

2.4k views Asked by At

I have layout file where I have multiple includes with same layout file like below.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="row1Data"
            type="com.desaidr.testapp.RowDetails" />

        <variable
            name="row2Data"
            type="com.desaidr.testapp.RowDetails" />

        <variable
            name="row3Data"
            type="com.desaidr.testapp.RowDetails" />

        <variable
            name="row4Data"
            type="com.desaidr.testapp.RowDetails" />

        <variable
            name="row5Data"
            type="com.desaidr.testapp.RowDetails" />
    </data>

    <LinearLayout
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/row_1"
            layout="@layout/include_row_details"
            app:rowDetails="@{row1Data}"/>

        <include
            android:id="@+id/row_2"
            layout="@layout/include_row_details"
            app:rowDetails="@{row2Data}"/>

        <include
            android:id="@+id/row_3"
            layout="@layout/include_row_details"
            app:rowDetails="@{row3Data}"/>

        <include
            android:id="@+id/row_4"
            layout="@layout/include_row_details"
            app:rowDetails="@{row4Data}"/>

        <include
            android:id="@+id/row_5"
            layout="@layout/include_row_details"
            app:rowDetails="@{row5Data}"/>

    </LinearLayout>
</layout>

below is the include layout file like

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="rowDetails"
            type="com.desaidr.testapp.RowDetails" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt_rot_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@={rowDetails.title}"/>
    </LinearLayout>
</layout>

I would like to have common onCLickListener for all those textView inside my include file.

Below is my Activity code

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ActivityMainBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        mBinding.row1.txtRowTitle.setOnClickListener(this);
        mBinding.row2.txtRowTitle.setOnClickListener(this);
        mBinding.row3.txtRowTitle.setOnClickListener(this);
        mBinding.row4.txtRowTitle.setOnClickListener(this);
        mBinding.row5.txtRowTitle.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.txt_row_title:
                //TODO I am getting all my click here but how would differentiate which include is responsible for this click listener
                break;
        }
    }
}

I am getting click listener call backs in my onClick method but how would I differentiate which include is responsible for that click. As I ma getting same ID every time for click.

I could have separate click listener for all those text view. But is there a way to achieve in one click listener.

4

There are 4 answers

3
Meenal On BEST ANSWER

You can check using binding's id like this :

 @Override
public void onClick(View view) {
    if (view == binding.row1.txtRotTitle) {
        Toast.makeText(MainActivity.this, "text clicked", Toast.LENGTH_SHORT).show();
    } else if (view == binding.row2.txtRotTitle) {
        Toast.makeText(MainActivity.this, "text clicked 2", Toast.LENGTH_SHORT).show();
    }
}
6
santosh kumar On

Try this,

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.row_1:
            break;
        case R.id.row_2:
            break;
    }
    }
4
Mayur Raval On

You should try this.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private ActivityMainBinding mBinding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

    mBinding.row1.txtRowTitle.setOnClickListener(this);
    mBinding.row2.txtRowTitle.setOnClickListener(this);
    mBinding.row3.txtRowTitle.setOnClickListener(this);
    mBinding.row4.txtRowTitle.setOnClickListener(this);
    mBinding.row5.txtRowTitle.setOnClickListener(this);
}


@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.txt_row_title:
            //TODO I am getting all my click here but how would differentiate which include is responsible for this click listener
            switch (((ViewGroup)v.getParent()).getId()) {
                 case R.id.row_1:
                    break;
                 case R.id.row_2:
                    break;
                 case R.id.row_3:
                    break;
                 case R.id.row_4:
                    break;
                 case R.id.row_5:
                    break;
            }
            break;
    }
}
}

Hope this help

0
Raúl Prieto Bailón On

maybe too late but now I encountered with the same question and I made out a mix from the answers given in here. That's how I did it with many buttons (it would work as well for any other components).

public class DashBoardActivity extends AppCompatActivity implements View.OnClickListener {

ActivityDashboardBinding binding;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_dashboard);

    setOnClickListeners();
}

@Override
public void onClick(View v) {
    Intent intent = null;

    switch (v.getId()) {
        case R.id.btDependencies:
            intent = new Intent(this, DependencyListActivity.class);
            break;
        case R.id.btSections:
            //intent = new Intent(this, Li.class);
            Toast.makeText(this,"Sections",Toast.LENGTH_SHORT).show();
            break;
        case R.id.btProducts:
            //intent = new Intent(this, .class);
            Toast.makeText(this,"Products",Toast.LENGTH_SHORT).show();
            break;
        case R.id.btProfile:
            //intent = new Intent(this, .class);
            Toast.makeText(this,"Profile",Toast.LENGTH_SHORT).show();
            break;
        case R.id.btHelp:
            //intent = new Intent(this, .class);
            Toast.makeText(this,"Help",Toast.LENGTH_SHORT).show();
            break;
        case R.id.btSettings:
            //intent = new Intent(this, .class);
            Toast.makeText(this,"Settings",Toast.LENGTH_SHORT).show();
            break;
        default:
            break;

    }
    if (intent != null)
        startActivity(intent);
}

/**
 * Method used to set the OnClickListener on every DashBoard buttons
 */
private void setOnClickListeners() {
    binding.btDependencies.setOnClickListener(this);
    binding.btSections.setOnClickListener(this);
    binding.btProducts.setOnClickListener(this);
    binding.btProfile.setOnClickListener(this);
    binding.btHelp.setOnClickListener(this);
    binding.btSettings.setOnClickListener(this);
}

Hope it helps to anyone =)