Search code examples
androiddialogonclicktextviewclickable

OnClick crashes my application


I have a textview in dialog and I want when I click this textview to do something.
I want it when clicking on the textview to do something.

I have added clickable and onClick in my layout
Here are my codes

public class Activitytwo extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //WORKING CODES
        //WORKING CODES
        //WORKING CODES
        //WORKING CODES
    }

    // This one to show the dialog
    //This one works fine!
    public void share(View view){
        sharedialog = new Dialog(Activitytwo.this);
        sharedialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        sharedialog.setContentView(R.layout.postdialog);
        sharedialog.setCanceledOnTouchOutside(true);
        sharedialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        sharedialog.show();

        }

    public void facebookp(View view){
        //This one for the TextView (onClick)
        //This one doesn't work..
    }

}

This is my layout (postdialog.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/postdialogl"
    android:layout_width="match_parent"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_height="150dp"
    android:background="@drawable/postdialog" >

    <TextView
        android:id="@+id/postdtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:textSize="18sp"
        android:textStyle="bold"
        android:text="@string/post"
        android:textColor="#a7b8d6"/>

    <ImageView 
        android:id="@+id/poststrokea"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#202e4c"
        android:layout_below="@+id/postdtitle"
        android:layout_marginTop="5dp"/>
    <ImageView 
        android:id="@+id/poststrokeb"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#803e5482"
        android:layout_below="@+id/poststrokea"/>

    <TextView
        android:id="@+id/facebookpost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
    android:layout_marginTop="60dp"
    android:layout_marginLeft="30dp"
    android:drawablePadding="5dp"
        android:text="@string/facebook"
        android:textColor="#a7b8d6"
        android:textSize="12sp"
        android:drawableTop="@drawable/facebook"
        android:clickable="true"
    android:onClick="facebookp"/>

</RelativeLayout>

Solution

  • One solution is to declare your textview inside your dialog.

        public void share(View view){
          //your code.
            sharedialog = new Dialog(Activitytwo.this);
            sharedialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            sharedialog.setContentView(R.layout.postdialog);
            sharedialog.setCanceledOnTouchOutside(true); 
            sharedialog.getWindow().setBackgroundDrawable(newColorDrawable(android.graphics.Color.TRANSPARENT));
    
            final TextView tviPost = (TextView) sharedialog.findViewById(R.id.sharedialog);
    
         tviPost.setOnClickListener(new OnClickListener() {
                        public void onClick(View arg0) {
                         //Do something
    
                        }
            });
    
        sharedialog.show(); 
        }
    

    and delete the next lines from your layout.

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