I am working on my project where I have a listView and each item is a LinaerLayout that has a TextView with Linkify hyperlink.
So, when I press the an item in the List view, it opens a dialog, which is fine.
When I press the linked text in the listView, it opens a dialog, which is fine.
PROBLEM: When I LONG-PRESS the linked text in the Listview, it opens a dialog AND an activity of the given link at the same time! In this case, I only want it to open the dialog only.
In other words, I want to ignore Linkify's hyperlink on Long press.
Does anyone know how I can do this?
I don't know where to apply LongPress attributs... Thanks in advance.
FYI, I tried the following but doesn't work.
public class URLSpanNoUnderline extends URLSpan implements OnLongClickListener {
public URLSpanNoUnderline(String url) {
super(url);
}
#Override
public void updateDrawState(TextPaint textPaint) {
super.updateDrawState(textPaint);
textPaint.setUnderlineText(false);
}
#Override
public void onClick(View v) {}
#Override
public boolean onLongClick(View v) {
Log.d("log", "lonnnnnnnnnnnnnnnng click");
return false;
}
}
you need a longClick mark,set it when in textview longclicklistener,and in touchlistener,when action equals MotionEvent.ACTION_UP and longClick is true,return true。
textview.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
isLongClick= true;
return false;
}
});
textview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP && isLongClick){
isLongClick= false;
return true;
}
if(event.getAction() == MotionEvent.ACTION_DOWN){
isLongClick= false;
}
return v.onTouchEvent(event);
}
});
this problem happend in some phone.
Related
It does not work.
setLongClickable(true);
setOnLongClickListener(new OnLongClickListener()..
Set OnLongClickListener and override onLongClick. By returning true, the long click event will be treated as consumed.
mWebView.setOnLongClickListener(new View.OnLongClickListener(){
#Override
public boolean onLongClick(View v) {
return true;
}
});
I have Two EditTexts. I want On First EditTexts click clear the second and on second edittext click clear the first one. So I have tried OnClickListener and OnTouchListener. But it is not working properly.
et_email.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!(et_email.getText().toString().equalsIgnoreCase("") && et_mobile.getText().toString().equalsIgnoreCase(""))) {
if (MotionEvent.ACTION_UP == event.getAction()) {
et_mobile.setText("");
et_mobile.setFocusableInTouchMode(true);
et_mobile.setFocusable(true);
et_mobile.requestFocus();
}
return true; // return is important...
} else
return false;
}
});
et_mobile.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!(et_email.getText().toString().equalsIgnoreCase("") && et_mobile.getText().toString().equalsIgnoreCase(""))) {
if (MotionEvent.ACTION_UP == event.getAction()) {
et_email.setText("");
et_mobile.setFocusableInTouchMode(true);
et_mobile.setFocusable(true);
et_mobile.requestFocus();
}
return true; // return is important...
}
else
return false;
}
});
But the problem is Focus is not setting on first touch and while clicking and unable to clear EditText.
First add this to your Edittext layouts:
android:focusableInTouchMode="false"
Without this line, the EditText will react to touch-mode, and only listen to your onClick() the second time you click your EditText bar.
This will disable touch-mode for EditText, and fire onClick() in first time
focusableInTouchMode
Note: boolean that controls whether a view can take focus while in touch mode. If this is true for a view, that view can gain focus when clicked on, and can keep focus if another view is clicked on that doesn't have this attribute set to true.
than do as follows:
EditText1
EditText et_email = (EditText) findViewById(R.id.yourEditText1);
et_email.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do this
et_mobile.setText("");
//or this
et_mobile.getText().clear();
}
});
EditText2
EditText et_mobile = (EditText) findViewById(R.id.yourEditText2);
et_mobile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do this
et_email.setText("");
//or this
et_email.getText().clear();
}
});
This should help u out.
you are clearing the first one one touching the first one, change your variable names in first listener like this
if (!(et_email.getText().toString().equalsIgnoreCase("") && et_mobile.getText().toString().equalsIgnoreCase(""))) {
if (MotionEvent.ACTION_UP == event.getAction()) {
et_mobile.setText("");
et_email.setFocusableInTouchMode(true);
et_email.setFocusable(true);
et_email.requestFocus();
}
return true; // return is important...
} else
I hope this code helps you (it works for me)
edit1 = (EditText) findViewById(R.id.edit1);
edit2 = (EditText) findViewById(R.id.edit2);
edit1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
edit2.setText("");
}
});
edit2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
edit1.setText("");
}
});
I am trying to add both OnClickListener and OnTouchListener to my image view. Following is how the image view is created
dialogImage = (ImageView)findViewById(R.id.dialogImage);
Following is how the listeners are set
dialogImage.setOnClickListener(dialogBoxClicked);
dialogImage.setOnTouchListener(imageViewSwiped);
Following is the listener method implementation
public OnClickListener dialogBoxClicked = new OnClickListener()
{
#Override
public void onClick(View v)
{
//To do has been removed because the code is too big
}
};
OnTouchListener imageViewSwiped = new OnSwipeTouchListener()
{
public void onSwipeRight()
{
currentlyActiveQuestion++;
currentWord = words.get(currentlyActiveQuestion);
setUI();
}
public void onSwipeLeft()
{
currentlyActiveQuestion--;
currentWord = words.get(currentlyActiveQuestion);
setUI();
}
};
Here the OnTouchListener is implemented by a class called OnSwipeTouchListener to monitor left and right swipes. This class can be found here - https://stackoverflow.com/a/12938787/1379286
But the problem now is, when I set the OnTouchListener to the image view, the OnClickListener is not responding / do not do what it should do. ImageView is only responding to OnTouchListener. If I remove OnTouchListener then the OnClickListener works. I tested this in a virtual device WVGA5.1 and Galaxy Nexus in eclipse and not in a real phone because I do not have one.
How can I solve this?
EDIT
Any code example will be greatly appreciated
You may call View.performClick() when action_up. This results in the click event being called when an actual click happens. Hope it helps.
your_txtView.setOnClickListener(new TextView.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
your_txtView.setOnTouchListener(new TextView.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
} else if (MotionEvent.ACTION_UP == event.getAction()) {
v.performClick();
}
return true;
}
});
The OnTouchListener hooks the click-event. Handle the click event in it instead. Check out the answer on this question
From my experience, If you can't have both onTouchListener and onClickListener for the same view. If you want your onClickListener to work, set clickable="true" in the XML.
Here's the code:
textView1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
String content = textView1.getText().toString();
if (!content.equals("")){
showNameDialog();
}
return true;
}
});
Pretty simple. If the string content has text inside of it, it executes the showNameDialog() method.
Here's the method:
private void showNameDialog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
dialogBuilder.setTitle(name.toString().toUpperCase());
dialogBuilder.setMessage("Name's frequency: " + arrayListToString);
dialogBuilder.setPositiveButton("ok", null);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
}
It all works pretty well, except for the fact that when I click on textView1, it opens two, three or four AlertDialogs. Why? How can I make it open just one?
Touches aren't clicks, so I'd assume that onTouch can be called multiple times while the View is touched (on touch down, then touch up, etc). Instead try using an OnClickListener:
textView1.setClickable (true);
textView1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String content = textView1.getText().toString();
if (!content.equals("")){
showNameDialog();
}
}
});
Try use this code
textView1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
String content = textView1.getText().toString();
if(event.getAction() == MotionEvent.ACTION_DOWN){
if (!content.equals("")){
showNameDialog();
}
}
return true;
}
});
I have a Gridview displaying buttons that get their content from an array, I would like to change the background color of these buttons when clicking/pressed on it. I tried onClick and onTouch, it does go in to the method, but color is not background is not set. What is wrong with my code? Please help.
private class ImageAdapter extends BaseAdapter {
private Context mContext;
btnView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("onClick","go");
btnView.setBackgroundColor(Color.rgb(12,11,12));
btnView.setBackgroundColor(Color.parseColor("#3614B3"));
Intent data = new Intent();
data.setData(Uri.parse("PictureStyle" + position));
setResult(RESULT_OK, data);
}
});
btnView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.d("OnTouch","go2");
btnView.setBackgroundColor(Color.parseColor("#3614B3"));
return false;
}
});
btnView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d("Touch listener",String.valueOf(position));
btnView.setBackgroundColor(0xff00ff00);
Log.d("Touch listener","set");
if ( event.equals(MotionEvent.ACTION_DOWN) ) {
btnView.setBackgroundColor(0xffff0000);
}
return false;
}
});
try it with v.setBackgroundColor(Color.rgb(12,11,12)); in your onClick
You better use GridView.setOnItemClickListener instead of register a clickListener on each Button