Guest User

Untitled

a guest
Jul 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. public class ImageAdapter extends BaseAdapter {
  2. private Context mContext;
  3.  
  4. // Keep all Images in array
  5. public Integer[] mThumbIds = {
  6. R.drawable.pic_1, R.drawable.pic_1,
  7. R.drawable.pic_1,R.drawable.pic_1,
  8. R.drawable.pic_1, R.drawable.pic_1,
  9.  
  10. };
  11.  
  12. // Constructor
  13. public ImageAdapter(Context c){
  14. mContext = c;
  15. }
  16.  
  17. @Override
  18. public int getCount() {
  19. return mThumbIds.length;
  20. }
  21.  
  22. @Override
  23. public Object getItem(int position) {
  24. return mThumbIds[position];
  25. }
  26.  
  27. @Override
  28. public long getItemId(int position) {
  29. return 0;
  30. }
  31.  
  32. @Override
  33. public View getView(int position, View convertView, ViewGroup parent) {
  34. ImageView imageView;
  35. if (convertView == null) {
  36.  
  37. //Calculation of ImageView Size - density independent.
  38. //maybe you should do this calculation not exactly in this method but put it somewhere else.
  39. Resources r = Resources.getSystem();
  40. float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, r.getDisplayMetrics());
  41.  
  42.  
  43. imageView = new ImageView(mContext);
  44. imageView.setLayoutParams(new GridView.LayoutParams((int)px, (int)px));
  45. imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  46. //imageView.setPadding(8, 8, 8, 8);
  47. } else {
  48. imageView = (ImageView) convertView;
  49. }
  50.  
  51. imageView.setImageResource(mThumbIds[position]);
  52. return imageView;
  53. }
  54.  
  55. }
  56.  
  57. public class MainActivity extends Activity {
  58.  
  59. EditText input;
  60.  
  61. @Override
  62. public void onCreate(Bundle savedInstanceState) {
  63. super.onCreate(savedInstanceState);
  64. setContentView(R.layout.grid_layout);
  65.  
  66. GridView gridView = (GridView) findViewById(R.id.grid_view);
  67.  
  68. // Instance of ImageAdapter Class
  69. gridView.setAdapter(new ImageAdapter(this));
  70.  
  71. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  72. builder.setTitle("How many tables do you have ?");
  73.  
  74. input = new EditText(this);
  75. builder.setView(input);
  76.  
  77. builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
  78. @Override
  79. public void onClick(DialogInterface dialogInterface, int i) {
  80. String txt = input.getText().toString();
  81. Toast.makeText(getApplicationContext(),txt, Toast.LENGTH_LONG).show();
  82. }
  83. });
  84.  
  85. AlertDialog ad = builder.create();
  86. ad.show();
  87. }
  88.  
  89. }
Add Comment
Please, Sign In to add comment