Android : How to add same onClickListener in every textView in android dialog?

on Sunday, March 8, 2015


I have a custom android dialog which has several textViews. each textview is showing a different text. The intention is when user clicks on a textview, the dialog should be closed and the textColor of that textview should be returned to the parent.


This is my dialog layout.



<?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"
android:background="#ff000000">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="White"
android:id="@+id/textView_white"
android:layout_gravity="center_horizontal"
android:textSize="30dp"
android:textIsSelectable="true"
android:clickable="true"
android:gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#fffffbfd" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Black"
android:id="@+id/textView_black"
android:layout_gravity="center_horizontal"
android:textSize="30dp"
android:textIsSelectable="true"
android:clickable="true"
android:gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#ffffffff"
android:password="false"
android:background="#ff000000" />
</LinearLayout>


I am launching the dialog this way from the parent activity:



final Context context = MyWidgetConfigureActivity.this;
final Dialog dialog = new Dialog(context);

dialog.setContentView(R.layout.color_chooser);
dialog.setTitle("Choose Text Color");

dialog.show();


I want to add same onClickListener to every textView inside the dialog. How to acheive this? There are 20+ textViews in the dialog, I dont want to add onClickListener to each textViews manually? is there any better way to do the same?


0 comments:

Post a Comment