OnLongClick Listener


OnLongClickListener :  Interface definition for a callback to be invoked when a view has been clicked and held.

onClick(View v) : Called when a view has been clicked and held.

To enable LongClickListener write following code:

MainActivity.java :

public class MainActivity extends Activity {

       ImageButton ib;
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              ib = (ImageButton) findViewById(R.id.imageButton1);
             
              ib.setOnLongClickListener(new OnLongClickListener() {
                    
                     @Override
                     public boolean onLongClick(View v) {

                           Toast.makeText(MainActivity.this, "Yes you have Long Clicked !", Toast.LENGTH_SHORT).show();
                           return false;
                     }
              });
             
       }

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }

}

activity_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="69dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>


 




***********************************************************************************************************

Reach us At: - 0120-4029000 / 24 / 25 / 27 / 29 Mbl: 9953584548
Write us at: - smruti@apextgi.com and pratap@apextgi.com

No comments:

Post a Comment