ontouch and onclick on a textview not working
android
Solution
- Instead of infalter use window class
- return false in on Touch listener
set contenet view to your main activity
package com.collabera.labs.sai;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class HomePage extends Activity {
AlertDialog.Builder builder;
AlertDialog a;
TextView text, txtNewUser;
ImageView image;
Button ok;
String pswrd;
public Window mWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog_private_space);
ok = (Button) findViewById(R.id.buttonOK);
final Context mContext = this;
Dialog dialog = new Dialog(HomePage.this);
dialog.setContentView(R.layout.custom_dialog_private_space);
dialog.setTitle("Password");
dialog.setCancelable(true);
dialog.show();
mWindow = dialog.getWindow();
Log.w(" dialog is " + mWindow.toString(), "-------");
text = (TextView) mWindow.findViewById(R.id.tvDesc);
txtNewUser = (TextView) mWindow.findViewById(R.id.tvNewUser);
image = (ImageView) mWindow.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
txtNewUser.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP)
Toast.makeText(mContext, "Touched", Toast.LENGTH_SHORT)
.show();
Log.v("AS", "Touched");
return false;
}
});
txtNewUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
Log.v("AS", "Clicked");
}
});
}
}
Problem
have a look at the code below... i am setting the onclick listener on the textview and the ontouch listener but none of them is working... what seems to be the problem>....? ``` AlertDialog.Builder builder; AlertDialog a; TextView text,txtNewUser ; ImageView image; View layout; Button ok; String pswrd; LayoutInflater inflater; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); final Context mContext = this; Dialog dialog = new Dialog(mContext); inflater = (LayoutInflater) mContext .getSystemService(LAYOUT_INFLATER_SERVICE); ok = (Button) findViewById(R.id.buttonOK); dialog.setContentView(R.layout.custom_dialog_private_space); dialog.setTitle("Password"); dialog.setCancelable(true); layout = inflater.inflate(R.layout.custom_dialog_private_space, (ViewGroup) findViewById(R.id.layout)); text = (TextView) dialog.findViewById(R.id.tvDesc); txtNewUser = (TextView) dialog.findViewById(R.id.tvNewUser); image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.lock_symbol_android); builder = new AlertDialog.Builder(mContext); builder.setView(layout); a = builder.create(); a.show(); txtNewUser.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if(event.getAction() == MotionEvent.ACTION_UP) Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show(); Log.v("AS", "Clicked"); return true; } }); txtNewUser.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show(); Log.v("AS", "Clicked"); } }); } ``` here is the code of textview from the xml of the dialog layout ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/layout" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tvNewUser" android:text="New User?" android:layout_gravity="top|left" android:clickable="true" /> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvDesc" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Enter password" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="100" android:inputType="textPassword" > </EditText> <Button android:id="@+id/buttonOK" android:layout_width="100dp" android:layout_gravity="center" android:layout_height="fill_parent" android:text="OK" /> </LinearLayout> ```