user2251344
user2251344

Reputation: 9

Android onClick Action

Fellas, I've been scratching my head trying to implement a fairly simple onClick action on a TextView to no success. Here is my code:

    public class AccountsActivity extends Activity {

    final Context context = this;

     private TextView tvNextOkin;


    /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.my_account);

        tvNextOkin = (TextView) findViewById(R.id.tv_acc_next_of_kin);
         tvNextOkin.setText("Not set. Tap here to add");
         tvNextOkin.setTextColor(Color.RED);

        }



        public void performClick(View view){
         Log.i("Action::", "clicked!!");

      // add  listener
      tvNextOkin.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View arg0) {

                  // custom dialog
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.custom_dialog);
                  dialog.setTitle("This is a custom dialog");

                     dialog.show();
         }


         });
     }

 }

and here is the xml layout:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" >

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="2dp" >

     <TextView
      android:id="@+id/tv_acc_next_of_kin"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="5dp"
      android:text="tap here to add"
      android:textColor="#000"
      android:textSize="14dp"
      android:onClick="performClick"
      android:typeface="sans" />

      </LinearLayout>

      </ScrollView>

My aim is to able to call the performClick() method when the textview is clicked. ANy suggestions?

Upvotes: 0

Views: 1097

Answers (5)

Luc
Luc

Reputation: 2805

I saw your problem.

When you set android:onClick="performClick", It mean, when user click to TextView the method performClick will be invoked.

In this method will do: SET onClickListener for tvNextOkin

It doesn't show any dialog. :D

The solution:

<TextView
      android:id="@+id/tv_acc_next_of_kin"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="5dp"
      android:text="tap here to add"
      android:textColor="#000"
      android:textSize="14dp"
      android:typeface="sans" />

tvNextOkin.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View arg0) {

                  // custom dialog
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.custom_dialog);
                  dialog.setTitle("This is a custom dialog");

                     dialog.show();
}

Do not put it in performClick method.

Hope this will help you.

Regards,

Upvotes: 0

Aravin
Aravin

Reputation: 4108

Add this in your xml for textview

android:onClick="onClick"
android:clickable="true"

and perfrom onclick operation

public void onClick(View v) {
        ...
      }  

Upvotes: 0

droidx
droidx

Reputation: 2172

Add android:clickable="true" for TextView in xml

and as @ Altaf mentioned in his answer, remove the listener. Just have,

public void performClick(View view){
     Log.i("Action::", "clicked!!");

              // custom dialog
     final Dialog dialog = new Dialog(context);
     dialog.setContentView(R.layout.custom_dialog);
     dialog.setTitle("This is a custom dialog");

     dialog.show();
 }

Or alternate way is to remove the android:onClick="onClick" and implement the onClickListener for the TextView in your activity.

Upvotes: 1

RickSu
RickSu

Reputation: 1

you should register your onClick listener in onCreate(). You can reference Android SDK document.

Upvotes: 0

Altaf
Altaf

Reputation: 5170

Remove listener

tvNextOkin.setOnClickListener(new OnClickListener() {}

You just need performClick method

Upvotes: 0

Related Questions