Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ImageAdapter extends BaseAdapter {
- private Context mContext;
- // Keep all Images in array
- public Integer[] mThumbIds = {
- R.drawable.pic_1, R.drawable.pic_1,
- R.drawable.pic_1,R.drawable.pic_1,
- R.drawable.pic_1, R.drawable.pic_1,
- };
- // Constructor
- public ImageAdapter(Context c){
- mContext = c;
- }
- @Override
- public int getCount() {
- return mThumbIds.length;
- }
- @Override
- public Object getItem(int position) {
- return mThumbIds[position];
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView imageView;
- if (convertView == null) {
- //Calculation of ImageView Size - density independent.
- //maybe you should do this calculation not exactly in this method but put it somewhere else.
- Resources r = Resources.getSystem();
- float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, r.getDisplayMetrics());
- imageView = new ImageView(mContext);
- imageView.setLayoutParams(new GridView.LayoutParams((int)px, (int)px));
- imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
- //imageView.setPadding(8, 8, 8, 8);
- } else {
- imageView = (ImageView) convertView;
- }
- imageView.setImageResource(mThumbIds[position]);
- return imageView;
- }
- }
- public class MainActivity extends Activity {
- EditText input;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.grid_layout);
- GridView gridView = (GridView) findViewById(R.id.grid_view);
- // Instance of ImageAdapter Class
- gridView.setAdapter(new ImageAdapter(this));
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("How many tables do you have ?");
- input = new EditText(this);
- builder.setView(input);
- builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- String txt = input.getText().toString();
- Toast.makeText(getApplicationContext(),txt, Toast.LENGTH_LONG).show();
- }
- });
- AlertDialog ad = builder.create();
- ad.show();
- }
- }
Add Comment
Please, Sign In to add comment