Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9163327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:25:14+00:00 2026-06-17T14:25:14+00:00

I want to add an item long click listener, which will set the image

  • 0

I want to add an item long click listener, which will set the image selected as wallpaper. I am getting images from web and displaying them in a grid view. My grid activity is shown below. I already have an on click listener to show a full screen image.

public class ImageGridActivity extends BaseActivity {

String[] imageUrls;

DisplayImageOptions options;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.ac_image_grid);

    Bundle bundle = getIntent().getExtras();
    imageUrls = bundle.getStringArray(Extra.IMAGES);

    options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.stub_image)
            .showImageForEmptyUri(R.drawable.image_for_empty_url)
            .cacheInMemory().cacheOnDisc()
            .bitmapConfig(Bitmap.Config.RGB_565).build();

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter());

    // Set Long-Clickable
    gridView.setLongClickable(true);
    gridView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @SuppressLint("NewApi")
        public boolean onItemLongClick(AdapterView<?> parent, View arg1,
                int position, long arg3) {
            // TODO Auto-generated method stub
            ImageAdapter i = (ImageAdapter) parent.getAdapter();
            Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
                    (int) i.getItemId(position));

            // Get the WallpaperManager
            WallpaperManager myWallpaperManager = WallpaperManager
                    .getInstance(getApplicationContext());

            try {
                // Set the clicked bitmap
                myWallpaperManager.setBitmap(mBitmap);
                Toast.makeText(ImageGridActivity.this, "Wallpaper set",
                        Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                Toast.makeText(ImageGridActivity.this, "Error setting wallpaper",
                        Toast.LENGTH_SHORT).show();
            }

            return false;
        }
    });

    gridView.setOnItemClickListener(new OnItemClickListener() {

        @SuppressLint("NewApi")
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            startImageGalleryActivity(position);
        }
    });

    gridView.setOnScrollListener(new PauseOnScrollListener(true, true));

}

private void startImageGalleryActivity(int position) {
    Intent intent = new Intent(this, ImagePagerActivity.class);
    intent.putExtra(Extra.IMAGES, imageUrls);
    intent.putExtra(Extra.IMAGE_POSITION, position);
    startActivity(intent);
}

public class ImageAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return imageUrls.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ImageView imageView;
        if (convertView == null) {
            imageView = (ImageView) getLayoutInflater().inflate(
                    R.layout.item_grid_image, parent, false);
        } else {
            imageView = (ImageView) convertView;
        }

        imageLoader.displayImage(imageUrls[position], imageView, options);
        return imageView;
    }
}

logcat

     01-22 16:00:35.101: E/AndroidRuntime(29785): FATAL EXCEPTION: main
01-22 16:00:35.101: E/AndroidRuntime(29785): android.content.res.Resources$NotFoundException: Resource ID #0x2
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.content.res.Resources.getValue(Resources.java:1105)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:554)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:630)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at com.nostra13.example.universalimageloader.ImageGridActivity$1.onItemLongClick(ImageGridActivity.java:67)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.widget.AbsListView.performLongPress(AbsListView.java:2622)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2572)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.os.Handler.handleCallback(Handler.java:608)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.os.Looper.loop(Looper.java:156)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at android.app.ActivityThread.main(ActivityThread.java:4987)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at java.lang.reflect.Method.invokeNative(Native Method)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at java.lang.reflect.Method.invoke(Method.java:511)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-22 16:00:35.101: E/AndroidRuntime(29785):    at dalvik.system.NativeStart.main(Native Method)

android manifest

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nostra13.example.universalimageloader"
    android:versionCode="26"
    android:versionName="1.7.0" >

    <uses-sdk
        android:minSdkVersion="5"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.SET_WALLPAPER"/>

    <application
        android:name=".UILApplication"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name" 
        android:allowBackup="True"
        >
        <activity
            android:name=".HomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".ImageGridActivity"
            android:label="@string/ac_name_image_grid" />
        <activity
            android:name=".ImagePagerActivity"
            android:label="@string/ac_name_image_pager" />


        <!-- Widget -->
        <receiver android:name=".widget.UILWidgetProvider">
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_provider" />

            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:25:16+00:00Added an answer on June 17, 2026 at 2:25 pm

    Use OnItemLong listener instead OnItemClick Listener.

    Instead of:

    gridView.setOnItemClickListener(new OnItemClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
    
                startImageGalleryActivity(position);
            }
        });
    

    Use

     gridView.setOnItemLongClickListener(new OnItemLongClickListener() {
    
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
           startImageGalleryActivity(position);
            return false;
        }
    });
    

    Updated

    OnItemClickListener

     gridview.setOnItemClickListener(new OnItemClickListener() {
                  public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
                      Toast.makeText(test2.this, "Click Listener", Toast.LENGTH_SHORT).show(); 
                       // open the pager activity
                      }    });
    

    OnItemLongClickListener

    gridview.setOnItemLongClickListener(new OnItemLongClickListener() {
    
                        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                int position, long arg3) {
                            Toast.makeText(test2.this, "LONG PRESS", Toast.LENGTH_SHORT).show();
                            //set the image as wallpaper 
                            return true;
                        }
                    }); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a menu item to which I add another item. Now I want
Begginer android programer, I want add and remove item from String list when user
I want add new Feed item on entity persist and update. I write this
I want to test Add new item activity in my application. When user fills
I want to permanently add an item to the cache. I am using the
I have problem, when I want add Node to my GUI from other Thread.
I want to open dialog on button click. My dialog is having listview which
I have a set of lots of big long strings that I want to
I want to implement click on item and go to layout where we have
I'm prompting a user to add an item to an SQLiteDb. When they click

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.