How can I implement longclickable in my application

I have a button that will lead you to a sample with a brief description, but what I would like to do is a long click and then allow the user to go to the site for more information.

here is my code for my button (regular)

<Button android:id="@+id/samplea" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginTop="20dp" android:background="@drawable/samplea_button" android:longClickable="true"/> 

and my java is

 Button next = (Button) findViewById(R.id.samplea); next.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { final ImageView imageView = (ImageView) findViewById(R.id.iM2); imageView.setImageResource(R.drawable.samplea_draw); 

How do I add a longclickable ad to this to send me to the site? Can anybody help?

I added it, but now it seems to me that I got to this site (after a long click), but not to the image (after the usual onclick) heres my code:

  next1.setOnLongClickListener(new OnLongClickListener() { public boolean onLongClick(View v) { // Launch your web intent Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com/a/13298207/1267661")); startActivity(intent); return true; } public void onClick(View view) { final ImageView imageView = (ImageView) findViewById(R.id.iM2); imageView.setImageResource(R.drawable.samplea_draw); 

get the yellow line under "public void onClick (View view) {"

+4
source share
3 answers

Update
Deploy OnLongClickListener just like your OnClickListener, but it should be separate. Try the following:

 Button next = (Button) findViewById(R.id.samplea); next.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { // You can turn this into a class variable final ImageView imageView = (ImageView) findViewById(R.id.iM2); imageView.setImageResource(R.drawable.samplea_draw); } )}; next.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { // Launch your web intent Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com/a/13298207/1267661")); startActivity(intent); return true; } }); 
+3
source

You add a long click listener -

 next.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { //your action on long click return true; } }); 

Look here - Android: long click on the button β†’ perform actions

You will always get better answers with more effort if you first ask your question on Google!

+1
source

Follow below link for details

Long click button

See the sample code below.

 next.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"Button long click", Toast.LENGTH_SHORT).show(); return true; } }); 
0
source

Source: https://habr.com/ru/post/1444944/


All Articles