i have a custom webview on which user can select text, i want to hide the "TEXT SELECTION HANDLES" when user click on some button at the bottom , i want the text to be selected but want to hide the handles, as you can see below :
// call hideTextSelection() from your onCreate()
function hideTextSelection(){
//SOLUTION 1-> vibration caused by the long click not works here
webView.setHapticFeedbackEnabled(false);
//Solution 2:
/*webView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
webView.setLongClickable(false);*/
}
// whenever you wants to select the data ie. after onClicking the button 'from the bottom of your UI', make it to select as below
yourBottomButton.setOnClickListener(new OnClickListener() {
#Override
public boolean onClick(View v) {
webView.setHapticFeedbackEnabled(true);
}
});
Try this
mWebView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true;
}
});
mWebView.setLongClickable(false);
Related
Long press
When I long-click on an item of the message, the item will display and the layout changes like the picture. I want to make this , but i don't have a keyword to find this solution. I need a keyword or some example to make it.
Many ways you can do. I am going to share one example.
Implement View.OnLongClickListener as follows
private void setupLongPress() {
imageButton.setOnLongClickListener(new View.OnLongClickListener(){
#Override
public boolean onLongClick(View v){
// here your staff
// we added dialog method here as follows
createPreviewDialog();
return false;
}
});
}
Now use LayoutInflater to inflate new layout as a popup windows
private Dialog createPreviewDialog() {
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_preview, null);
LinearLayout closeButton = view.findViewById(R.id.close);
closeButton.setOnClickListener (new View.OnClickListener (){
#Override
public void onClick ( View view ) {
dismiss();
}
});
View okButton = view.findViewById(R.id.ok);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dismiss();
// here your staff
}
});
builder.setView(view);
return builder.create();
}
I have a customView , i want to set onClick which will only be called on the very first click. In which i want to start a thread which will start a counter on other TextView , with simple onClickListener with each click a new threads starts which is a problem . How can i achieve such task ?
Another option is in your onClick() method do set a null listener, i.e.
#Override
public void onClick(View view) {
// disable any other clicks from now on
customView.setOnClickListener(null);
...
}
I think this is only logic problem, So I solve this problem by using a boolean variable for the first click:
boolean isFristClick = true;
#Override
public void onClick(View v) {
if (isFristClick) {
// Start your counter Thread here
isFristClick = false;
} else {
// Do nothing
}
}
What about making a workaround for that?!! like assigning a boolean value to tell if it's the first click:
private boolean first_click = true;
your_view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(first_click){
first_click = false;
// Do something on first click
}else{
// Do another thing on later clicks
}
}
});
i am creating a call/dial button, when i click on that call/dial button, a call will be made based on the input that is displayed in the edittext. I managed to do that part. can you guys advise me whether i can do a longer click on that same call/dial button, so that a toast can come out to ask user to choose something else??
I did some research on "setOnLongClickListener" but i am not sure if i can combine it in the same call/dial button? I have attached on the working dial function which i managed to do, wondering if the "setOnLongClickListener" can be combined together somehere in the code?
private void dialANumber() {
try {
buttonCall = (ImageButton) findViewById(R.id.imageButton2);
buttonCall.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (display != null) {
Intent callNumber = new Intent();
callNumber
.setAction(android.content.Intent.ACTION_CALL);
callNumber.setData(Uri.parse("tel:" + display.getText()));
startActivity(callNumber);
}
}
});
} catch (ActivityNotFoundException anfe) {
Log.e("DialANumber", "Dialing the number failed", anfe);
}
this code is working. i hope a longer click can be made on the same call/dial button so the button can have a normal click to make a call, and longer click to pop out a toast. Thanks in advance.
Note that returning "false" on the long click listener will have the UI responding to the long click as a short click too. Return "true" if you want to kill that off. "True" means "yes, I used this event" and "false" means "whether I used it or not, the environment is free to respond as well." (I know this because I just used AkashG's answer in my own app.)
A GestureDetector with a SimpleOnGestureListener would help you differentiate between the different types of presses. A GestureDectector is a class which can read different types of touch events (for example, single taps and long presses), and sends them to a listener which handles each type differently. Here's the documentation on the Detector and Listener.
http://developer.android.com/reference/android/view/GestureDetector.html
http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html
First, set up your SimpleOnGestureListener, the important methods for you to override will be onSingleTapUp and onLongPress. In your onCreate, create an instance of GestureDetector that references your listener. Then, attach an OnTouchListener to your button that sends the event to your detector. You'll want it to look something like this:
//Set up the Detector
GestureDetector.SimpleOnGestureListener myGestureListener = new GestureDetector.SimpleOnGestureListener()
{
#Override
public boolean onSingleTapUp(MotionEvent e)
{
//Your onClick code
return false;
}
#Override
public void onLongPress(MotionEvent e)
{
//Your LongPress code
super.onLongPress(e);
}
};
//And make a variable for your GestureDetector
GestureDetector myGestureDetector;
...
#Override
onCreate(Bundle b)
{
...
myGestureDetector = new GestureDetector(myActivity.this, myGestureListener);
...
}
...
//And finally, wherever you are setting up your button
button.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
myGestureDetector.onTouchEvent(motionEvent);
return false;
}
There a a bunch of other types of events this class can interpret in case you want to get even more fancy. GestureDetector is a very good class to do a little research on, it can be very useful and isn't too complex. Hope this helps.
Yes you can do this:
XML file:
<Button
android:id="#+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALL"/>
<ImageButton
android:id="#+id/callBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"/>
For button click event:
Button button=(Button) findViewById(R.id.call);
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(), "Long CLick", Toast.LENGTH_SHORT).show();
return false;
}
});
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (display != null) {
Intent callNumber = new Intent();
callNumber
.setAction(android.content.Intent.ACTION_CALL);
callNumber.setData(Uri.parse("tel:" + display.getText()));
startActivity(callNumber);
}
}
});
For imageButton:
ImageButton imageButton=(ImageButton) findViewById(R.id.callBtn);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(check==false){
Toast.makeText(getBaseContext(), "CLick", Toast.LENGTH_SHORT).show();
}
imageButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
check=true;
if(check){
Log.d("bool", check+"");
Toast.makeText(getBaseContext(), "Long CLick", Toast.LENGTH_SHORT).show();
check=false;
}
return false;
}
});
Declare this at the top(golbally):
boolean check=false;
In my android app I need to do some actions after close longClick menu.
I have the next code:
editText.setOnLongClickListener(new View.OnLongClickListener()
{
#Override
public boolean onLongClick (View v)
{
flag = "On";
return false;
}
});
And it works fine, but I need flag="Off" when longClick menu will be closed. Is it possible to catch this event?
Thank you!
you should use this:
#Override
public void onOptionsMenuClosed(Menu menu) {
flag = "off";
super.onOptionsMenuClosed(menu);
}
I have a Button which sets both onLongClickListener & onClickListener :
onLongClickListener :
#Override
public boolean onLongClick(View v) {
do something ...
return true;
}
onClickListener :
#Override
public void onClick(View v) {
do something else ...
}
When I long click the button, onLongClick fires repeatly
(sometimes onClick fires too when I release the button, it's weird ##")
What I want is to make the onLongClick be triggered only once for one long press.
So I modified the code :
onLongClickListener :
#Override
public boolean onLongClick(View v) {
do something ...
myButton.setLongClickable(false);
return true;
}
onClickListener :
#Override
public void onClick(View v) {
myButton.setLongClickable(true);
do something else ...
}
Unfortunately, the onClick callback was locked too after onLongClick fires!
I cant unlock the button anymore :|
Whats wrong with my code? Also, why onClick sometimes works when I release my button after a long click?
I've got the code you need, give me a minute to post it.
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//DO STUFF GRAH!
}
});
btn.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
//OTHER STUFF
return true;
}
});
This worked fine for me. I made an int and onLongClick added one and displayed it in a toast. Always incremented by one, and didn't do the onClick (reset it to 0).