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 8508841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:16:22+00:00 2026-06-11T03:16:22+00:00

I’ve adapted the code from AOSP Launcher2 WallpaperChooser & WallpaperChooserDialogFragment to be a standalone

  • 0

I’ve adapted the code from AOSP “Launcher2” WallpaperChooser & WallpaperChooserDialogFragment to be a standalone Gallery App, and I wanted it to work on API levels 7 up to API 16.

To make it work for API levels < 11 I added the Android Support Library (rev.10) “Compability Package”, V4 package.

I based my changes in the compability package (android.support.v4.app.DialogFragment.java) on the suggestions from here:
DialogFragments with devices api level < 11.

Otherwise the app would crash with an error (“DialogFragment can not be attached to a container view”).

However, the app refuses to work as intended when deployed to an actual Jelly Bean 4.1.1 device (Nexus S), but it works perfectly fine on the emulator.

Tested & Works on:

  • Android 2.1 Emulator (API 7)
  • Android 2.2 Emulator (API 8)
  • Android 2.3.3 Emulator (API 10)
  • Android 4.0.3 Emulator (API 15)
  • Android 4.1 Emulator (API 16)

  • Android 2.3.7 Device (API 10)

Tested & Doesn’t Work on:

  • Android 4.1.1 Device (API 16)

When running on the 4.1.1 device, it never loads/displays the gallery images when tapping on them in the horizontal view.

It only displays each picture after selecting and rotating the device, and then again, select a new picture, rotate again, and it displays correctly.

I haven’t managed to figure out the problem why it wouldn’t work on a real device so any help is appreciated.
If I remove the compability package and only targets API level +11, the app works on the real 4.1.1 device.

This is the code for the app:

GalleryDemo.java:

package gallery.android.demo;

import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import android.os.Bundle;

public class GalleryDemo extends FragmentActivity {

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.wallpaper_chooser_base);

    Fragment fragmentView = getSupportFragmentManager().findFragmentById(R.id.wallpaper_chooser_fragment);
    if (fragmentView == null) {
        DialogFragment fragment = GalleryDemoDialogFragment.newInstance();
        fragment.show(getSupportFragmentManager(), "dialog");
    }
}
}

GalleryDemoDialogFragment.java:

package gallery.android.demo;

import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

import java.util.ArrayList;

@SuppressWarnings("deprecation")
@TargetApi(16)
public class GalleryDemoDialogFragment extends DialogFragment implements AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {

private static final String TAG = "GalleryDemoDialogFragment";
private static final String EMBEDDED_KEY = "gallery.android.demo.GalleryDemoDialogFragment.EMBEDDED_KEY";

private boolean mEmbedded;
private Bitmap mBitmap = null;

private ArrayList<Integer> mThumbs;
private ArrayList<Integer> mImages;
private WallpaperLoader mLoader;
private WallpaperDrawable mWallpaperDrawable = new WallpaperDrawable();

public static GalleryDemoDialogFragment newInstance() {
    GalleryDemoDialogFragment fragment = new GalleryDemoDialogFragment();
    fragment.setCancelable(true);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null && savedInstanceState.containsKey(EMBEDDED_KEY)) {
        mEmbedded = savedInstanceState.getBoolean(EMBEDDED_KEY);
    } else {
        mEmbedded = isInLayout();
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putBoolean(EMBEDDED_KEY, mEmbedded);
}

private void cancelLoader() {
    if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
        mLoader.cancel(true);
        mLoader = null;
    }
}

@Override
public void onDetach() {
    super.onDetach();
    cancelLoader();
}

@Override
public void onDestroy() {
    super.onDestroy();
    cancelLoader();
}

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    FragmentActivity activity = getActivity();
    if (activity != null) {
        activity.finish();
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    findWallpapers();
    return null;
}

@TargetApi(16)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    findWallpapers();
    if (mEmbedded) {
        View view = inflater.inflate(R.layout.wallpaper_chooser, container, false);
        switch (Build.VERSION.SDK_INT) {
        case Build.VERSION_CODES.ICE_CREAM_SANDWICH :
            view.setBackgroundDrawable(mWallpaperDrawable);
            break;
        case Build.VERSION_CODES.JELLY_BEAN :
            view.setBackground(mWallpaperDrawable);
            break;
        default:
            view.setBackgroundDrawable(mWallpaperDrawable);
            break;
        }

        final Gallery gallery = (Gallery) view.findViewById(R.id.gallery);
        gallery.setCallbackDuringFling(false);
        gallery.setOnItemSelectedListener(this);
        gallery.setAdapter(new ImageAdapter(getActivity()));

        View setButton = view.findViewById(R.id.set);
        setButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = gallery.getSelectedItemPosition();
                String[] wallpaperArray = getResources().getStringArray(R.array.wallpapers);
                String name = wallpaperArray[position]+".jpg";

                Log.i(TAG, "Position="+position);
                Log.i(TAG, "Filename="+name);
                Toast.makeText(getActivity().getApplicationContext(), "(Save2SD)\n"+
                                                                      "Position="+position+"\n"+
                                                                      "Filename="+name+
                                                                      "", Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
    return null;
}


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

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
        mLoader.cancel();
    }
    Log.d(TAG, "onItemSelected()");
    mLoader = (WallpaperLoader) new WallpaperLoader().execute(position);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}

private void findWallpapers() {
    mThumbs = new ArrayList<Integer>(24);
    mImages = new ArrayList<Integer>(24);

    final Resources resources = getResources();
    final String packageName = resources.getResourcePackageName(R.array.wallpapers);

    addWallpapers(resources, packageName, R.array.wallpapers);
    addWallpapers(resources, packageName, R.array.extra_wallpapers);
}

private void addWallpapers(Resources resources, String packageName, int list) {
    final String[] extras = resources.getStringArray(list);
    for (String extra : extras) {
        int res = resources.getIdentifier(extra, "drawable", packageName);
        if (res != 0) {
            final int thumbRes = resources.getIdentifier(extra + "_small", "drawable", packageName);
            if (thumbRes != 0) {
                mThumbs.add(thumbRes);
                mImages.add(res);
            }
        }
    }
}


private class ImageAdapter extends BaseAdapter implements ListAdapter, SpinnerAdapter {
    private LayoutInflater mLayoutInflater;

    ImageAdapter(FragmentActivity activity) {
        mLayoutInflater = activity.getLayoutInflater();
    }

    public int getCount() {
        return mThumbs.size();
    }

    public Object getItem(int position) {
        return position;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        View view;

        if (convertView == null) {
            view = mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
        } else {
            view = convertView;
        }

        ImageView iv = (ImageView) view.findViewById(R.id.wallpaper_image);

        int thumbRes = mThumbs.get(position);
        iv.setImageResource(thumbRes);

        Drawable thumbDrawable = iv.getDrawable();
        if (thumbDrawable != null) {
            thumbDrawable.setDither(true);
        } else {
            Log.e(TAG, "Error decoding thumbnail resId=" + thumbRes + " for wallpaper #" + position);
        }
        return view;
    }
}


private class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> {
    BitmapFactory.Options mOptions;

    WallpaperLoader() {
        mOptions = new BitmapFactory.Options();
        mOptions.inDither = false;
        mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
    }

    @Override
    protected Bitmap doInBackground(Integer... params) {
        if (isCancelled()) return null;
        try {
            return BitmapFactory.decodeResource(getResources(), mImages.get(params[0]), mOptions);
        } catch (OutOfMemoryError e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(Bitmap b) {
        if (b == null) return;
        if (!isCancelled() && !mOptions.mCancel) {
            if (mBitmap != null) {
                mBitmap.recycle();
            }

            View v = getView();
            if (v != null) {
                Log.d(TAG, "WallpaperLoader.onPostExecute() getView != null");
                mBitmap = b;
                mWallpaperDrawable.setBitmap(b); // Doesn't appear to work on (physical) device with Jelly Bean (4.1.1)?
                v.postInvalidate();
            } else {
                Log.d(TAG, "WallpaperLoader.onPostExecute(Bitmap b) getView == null");
                mBitmap = null;
                mWallpaperDrawable.setBitmap(null);
            }
            mLoader = null;
        } else {
           b.recycle();
        }
    }

    void cancel() {
        mOptions.requestCancelDecode();
        super.cancel(true);
    }
}

private static class WallpaperDrawable extends Drawable {

    Bitmap mBitmap;
    int mIntrinsicWidth;
    int mIntrinsicHeight;

    void setBitmap(Bitmap bitmap) {
        Log.d(TAG, "WallpaperDrawable.setBitmap(Bitmap)");
        mBitmap = bitmap;
        if (mBitmap == null) {
            Log.d(TAG, "WallpaperDrawable.setBitmap(Bitmap) mBitmap == null");
            return;
        }
        mIntrinsicWidth = mBitmap.getWidth();
        mIntrinsicHeight = mBitmap.getHeight();
    }

    @Override
    public void draw(Canvas canvas) {
        if (mBitmap == null) {
            Log.d(TAG, "WallpaperDrawable.draw(Canvas) mBitmap == null");
            return;
        }
        Log.d(TAG, "WallpaperDrawable.draw(Canvas)");
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        int x = (width - mIntrinsicWidth) / 2;
        int y = (height - mIntrinsicHeight) / 2;
        canvas.drawBitmap(mBitmap, x, y, null);
    }

    @Override
    public int getOpacity() {
        return android.graphics.PixelFormat.OPAQUE;
    }

    @Override
    public void setAlpha(int alpha) {
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
    }
}
}
  • 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-11T03:16:23+00:00Added an answer on June 11, 2026 at 3:16 am

    The real device running API 16 is the only device with hardware accelerated rendering, which is why it behaves differently. You can try setting android:hardwareAccelerated=”false” in your activity’s manifest entry to experiment with this.

    You can leave acceleration on if you call invalidateSelf() when you change the content of a Drawable:

    private static class WallpaperDrawable extends Drawable {
        void setBitmap(Bitmap bitmap) {
            . . .            
            invalidateSelf();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.