Hello world. Today i wanna show you how to implementation onClick on the android programming. Basically onClick function can use for all widget in android. Something like Textview, Textfield, Button, etc. So on this post i will show you onClick just in Textview. Here we go.
This my basic layout, i call it fragment_one.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/activity_horizontal_margin_twice">
<TextView
android:id="@+id/tvOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:text="@string/fragmentOne"/>
<View
android:id="@+id/vOne"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorAccent"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:layout_below="@+id/tvOne"/>
<TextView
android:id="@+id/tvOnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_below="@+id/vOne"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:text="@string/onClick"/>
<View
android:id="@+id/vTwo"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorAccent"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:layout_below="@+id/tvOnClick"/>
</RelativeLayout>
and on this layout i've two textview that i will to implement click. And this is my function to implement my textview onClick.
private void onClick(View view){
tvOne = (TextView) view.findViewById(R.id.tvOne);
tvOnClick = (TextView) view.findViewById(R.id.tvOnClick);
tvOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, "Hi i am function onclick", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
tvOnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, "Hi i am function onclick", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
On this function i just declare my text view and call setOnClickListener . And then i put snack bar to show "Hi i am function onClick". It would be like this one.
You can write comment bellow or comment on the my Github page. And also you can download it on my
Github page.