Menus

Thursday, 2 March 2017

Android Long Press Event



Android Long Press Event



There are many situation in which your application needs to long press event.

  (viewitem).setOnLongClickListener(new OnItemLongClickListener() {           
        @Override
        public boolean onLongClick(View v) {
            //your code here
            return false;
        }
    }

Long Press Button Event Handler


Button button = (Button) findViewById(R.id.btn_NextLift);

button.setOnLongClickListener(new OnLongClickListener() { 
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            return true;
        }
    });

Example of android long press event operation






XML file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.root.calendar.MainActivity"
    tools:showIn="@layout/activity_main"
    >


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        tools:context=".LongPress"
        android:layout_height="40dp"
        android:layout_alignEnd="@+id/calendarView">

        <TextView
            android:id="@+id/txtView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="" />

    </RelativeLayout>


    <com.prolificinteractive.materialcalendarview.MaterialCalendarView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/calendarView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        app:mcv_showOtherDates="all"
        app:mcv_selectionColor="#00F"
        />

</RelativeLayout>



Java File


package com.example.root.calendar;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        TextView txtView = (TextView) findViewById(R.id.txtView);
        txtView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
                return true;
            }
        });


    }
}






Related Links

Event Handling in Android

Transfer Data Between Activities Using Intent
Android Programming Tips



No comments:

Post a Comment