I am trying to create a custom dialog box that displays an image in full-view and has a Select as well as Cancel Button. This custom dialog box should be displayed when an image is clicked so as to allow the user to view the selected image in full-view. The image view should be one that is selected from a GridView. The Select Button will display a Toast message and the Cancel button will allow the user to exit the dialog. The idea is to allow the user to see the image in full-view before he or she makes a final choice on the picture they would like.
However, I am having several problems trying to do this. My code is shown below:
package com.newapp;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//error here: R cannot be resolved to a variable
GridView gridview = (GridView) findViewById(R.id.photogrid);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.ImageDialog);
dialog.setTitle("Full-image view");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
Button select = (Button) findViewById(R.id.selectimage);
buttonChangePerferences.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
Button cancel = (Button) findViewById(R.id.cancelselection);
cancel.setOnClickListener(new OnClickListener()
//error here: Syntax error on tokens;AnnotationName expected instead
{
public void onClick(View v) {
dialog.cancel();
}
});
})
}
package com.newapp;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter{
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
};
}
Can somebody help me fix these problems? Any help would much appreciated. Thanks
I think you need to change this line
Button select = (Button) findViewById(R.id.selectimage);
into
Button select = (Button) dialog.findViewById(R.id.selectimage);
and the same for cancel.
After select button you used “buttonChangePerferences” instead of “select”…
You have a problem with R.layout because you are using R.layout.ImageDialog where the name of the layout is in capital letters while it should be only in lower case…
you need to add dialog.show(); after you declare your buttons
plus, because the complier has got confused you may need simply to rewrite some lines and at some point when your application still has a little red mark in the package explorer on the right in Exclipse, you need to go to PROJECT menu then CLEAN and select your application and click OK.