Skip to content

Instantly share code, notes, and snippets.

@umetsu
Created November 11, 2015 09:37
Show Gist options
  • Save umetsu/400255ac886b4bef9b8b to your computer and use it in GitHub Desktop.
Save umetsu/400255ac886b4bef9b8b to your computer and use it in GitHub Desktop.
Android DataBinding - Listener bind sample
<?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="activity"
type="net.prunusmume.databindingsample.ListenerBindActivity" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="タップしてね!"
app:onClickListener="@{activity.buttonClickListener}" />
</RelativeLayout>
</layout>
package net.prunusmume.databindingsample;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import net.prunusmume.databindingsample.databinding.ActivityListenerBindBinding;
public class ListenerBindActivity extends AppCompatActivity {
private final View.OnClickListener mButtonClickListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
Toast.makeText(ListenerBindActivity.this, "タップされたよ", Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActivityListenerBindBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_listener_bind);
binding.setActivity(this);
}
public View.OnClickListener getButtonClickListener() {
return mButtonClickListener;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment