0

Is it possible to open a class inside a dialog box in Android?

For example :

Dialog settingsDialog = new Dialog(this);
        settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        settingsDialog.setContentView(getLayoutInflater().
                                   inflate(R.layout.image_pager, null));
        settingsDialog.show();

is opening an image. How can I do the same for loading classes instead of xml layout?

Additional info:
This is my ImagePager class

public class ImagePopUp extends BaseActivity implements OnClickListener {

    private ViewPager pager;
    Button boutton_retour;
    public static String img;


    private DisplayImageOptions options;
    String url,image,title;
    ArrayList<Post> PostDetails = new ArrayList<Post>();

    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_diaporama_pager);

        showViewHolderExtra();

    }


    private class ImagePagerAdapter extends PagerAdapter {

        private String[] images;
        private LayoutInflater inflater;

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        ImagePagerAdapter(String[] images) {
            this.images = images;
            inflater = getLayoutInflater();
        }

        @Override
        public void finishUpdate(View container) {
        }

        @Override
        public int getCount() {
            return images.length;
        }


        @Override
        public Object instantiateItem(View view, int position) {

            Dialog settingsDialog = new Dialog(ImagePopUp.this);
            settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.image_layout, null));
            settingsDialog.show();

            final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
            final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.im_pager);
            final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loadinprogress);

            imageLoader.displayImage(images[position], imageView, options, new ImageLoadingListener() {

                public void onLoadingStarted() {
                    spinner.setVisibility(View.VISIBLE);

                }


                public void onLoadingFailed(FailReason failReason) {
                    String message = null;
                    switch (failReason) {
                        case IO_ERROR:
                            message = "Input/Output error";
                            break;
                        case OUT_OF_MEMORY:
                            message = "Out Of Memory error";
                            break;
                        case UNKNOWN:
                            message = "Unknown error";
                            break;
                    }

                    Toast.makeText(ImagePopUp.this, message, Toast.LENGTH_SHORT).show();

                    spinner.setVisibility(View.GONE);
                    imageView.setImageResource(android.R.drawable.ic_delete);
                }

                public void onLoadingComplete(Bitmap loadedImage) {
                    spinner.setVisibility(View.GONE);
                    Animation anim = AnimationUtils.loadAnimation(ImagePopUp.this, R.anim.fade_in);
                    imageView.setAnimation(anim);
                    anim.start();
                }

                public void onLoadingCancelled() {
                    // Do nothing
                }
            });

            ((ViewPager) view).addView(imageLayout, 0);
            return imageLayout;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View container) {
        }

    }

    public void showViewHolderExtra(){

        String[] seperated = img.split(";");

        Bundle bundle = getIntent().getExtras();
        int pagerPosition = bundle.getInt("POSITION", 0);

        System.out.println("image: "+ pagerPosition);

        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new ImagePagerAdapter(seperated));
        pager.setCurrentItem(pagerPosition);

        options = new DisplayImageOptions.Builder()
            .showImageForEmptyUri(R.drawable.ic_launcher)
            .cacheOnDisc()
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .build();
    }




    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch(v.getId()){

        case R.id.boutton_retour:
            finish();
        break;

        }
    }
}

I want this to appear in the dialog box on click.

Adinia
  • 3,722
  • 5
  • 40
  • 58
Dimitri
  • 1,924
  • 9
  • 43
  • 65

3 Answers3

0

You want the whole activity to load in a dialog box. There's a couple of ways. If the activity is just a ViewPager, you could just set the dialog's content to just that ViewPager:

    Dialog settingsDialog = new Dialog(this);
    settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    ViewPager mViewPager = new ViewPager(this);
    ImagePagerAdapter adapter = new ImagePagerAdapter(images);
    mViewPager.setAdapter(adapter);
    settingsDialog.setContentView(mViewPager);
    settingsDialog.show();

You could even load the whole activity with intents like normal, but then just have it look like a dialog, with a dialog theme or otherwise.

If you're made aware that .setContentView can take a resource id or a view, then it should become self-apparent what methods to employ. It would be just like onCreate.

Adinia
  • 3,722
  • 5
  • 40
  • 58
mango
  • 5,577
  • 4
  • 29
  • 41
0

The easiest way is just to set in your AndroidManifest:

 <activity
            android:name=".ImagePopUp"
            android:theme="@android:style/Theme.Dialog" >
        </activity>

If you need the dialog to have a custom size, you can use something like this at the beginning of your ImagePopUp class:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screen_width = dm.widthPixels;
int screen_height = dm.heightPixels;

getWindow().setLayout((screen_width *0.75), (int) (screen_height - 50));
getWindow().setGravity(Gravity.CENTER);

Then, open the class as usual, using an intent on click:

Intent intent = new Intent(YourActualClass.this, ImagePopUp.class);
startActivity(intent);
Adinia
  • 3,722
  • 5
  • 40
  • 58
0

You can do this by using a custom view in your layout xml.

 settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.image_pager, null));

layout/imagepaper.xml

<com.your.package.CustomLinearLayout
   xmlns: ... 
   android:layout_width="" ... etc
   />

Then this is your 'class' for the dialog's layout

CustomLinearLayout.java

public class CustomLinearLayout extends LinearLayout {

     // Overridden constructors

     // onFinishInflate() etc

}

Another way to do this is with a 'DialogFragment'

shown here: http://blog.blundell-apps.com/tut-generic-fragment-dialog/

Your class extends DialogFragment:

 public class OneOptionDialogFragment<T> extends DialogFragment {

       // do stuff

 }

And to show the dialog (taken from above url):

 OneOptionDialogFragment<UserDetails> dialogFragment = OneOptionDialogFragment.newInstance(title, message, buttonText);
    dialogFragment.show(getSupportFragmentManager(), "DeleteFragTag");
Blundell
  • 75,855
  • 30
  • 208
  • 233