Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 18th, 2012  |  syntax: None  |  size: 17.68 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to get the image resource of images added dynamically onto Android?
  2. public class QuizActivity extends Activity implements OnClickListener {
  3. /** Called when the activity is first created. */
  4. RelativeLayout r2;
  5. // Global variable(s)
  6. int[][] quizData; // Storing the quiz specifications in an integer array
  7. int[][] questionImages = {
  8.         { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
  9.                 R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h,
  10.                 R.drawable.i, R.drawable.j },
  11.         { R.drawable.a_checked, R.drawable.b_checked, R.drawable.c_checked,
  12.                 R.drawable.d_checked, R.drawable.e_checked,
  13.                 R.drawable.f_checked, R.drawable.g_checked,
  14.                 R.drawable.h_checked, R.drawable.i_checked,
  15.                 R.drawable.j_checked },
  16.         { R.drawable.zero, R.drawable.one, R.drawable.two,
  17.                 R.drawable.three, R.drawable.four, R.drawable.five,
  18.                 R.drawable.six, R.drawable.seven, R.drawable.eight,
  19.                 R.drawable.nine, R.drawable.decimal },
  20.         { R.drawable.zero_checked, R.drawable.one_checked,
  21.                 R.drawable.two_checked, R.drawable.three_checked,
  22.                 R.drawable.four_checked, R.drawable.five_checked,
  23.                 R.drawable.six_checked, R.drawable.seven_checked,
  24.                 R.drawable.eight_checked, R.drawable.nine_checked,
  25.                 R.drawable.decimal_checked } };
  26.  
  27. // End
  28. @Override
  29. public void onCreate(Bundle savedInstanceState) {
  30.     super.onCreate(savedInstanceState);
  31.     setContentView(R.layout.main);
  32.  
  33.     // Beginning of variable declarations
  34.     ScrollView s1 = new ScrollView(this);
  35.     RelativeLayout r1 = new RelativeLayout(this);
  36.     r2 = r1;
  37.     File quizSpecs = new File("mnt/sdcard/teacher_1.csv"); // Read the file
  38.     BufferedReader csvReader = null;
  39.     String line = ""; // Storing each line in a string
  40.     StringTokenizer currentLine = null;
  41.     int noOfQuestions = 0; // Number of questions in the quiz
  42.     int time = 0; // Duration of the quiz
  43.     int i = 0, j = 0, k = 0; // Loop variables
  44.     int previd = 0;
  45.     // End of variable declarations
  46.  
  47.     try {
  48.         csvReader = new BufferedReader(new FileReader(quizSpecs));
  49.     } catch (Exception e) {
  50.         // TODO Auto-generated catch block
  51.         e.printStackTrace();
  52.     }
  53.     try {
  54.         line = csvReader.readLine();
  55.     } catch (Exception e) {
  56.         // TODO Auto-generated catch block
  57.         e.printStackTrace();
  58.     }
  59.     currentLine = new StringTokenizer(line, ",");
  60.     noOfQuestions = Integer.parseInt(currentLine.nextToken());
  61.     time = Integer.parseInt(currentLine.nextToken());
  62.     // System.out.println(noOfQuestions + " " + time);
  63.     while (currentLine.hasMoreTokens()) {
  64.         currentLine.nextToken();
  65.     }
  66.     quizData = new int[noOfQuestions][6];
  67.     for (i = 0; i < noOfQuestions; i++) {
  68.         try {
  69.             line = csvReader.readLine();
  70.         } catch (Exception e) {
  71.             // TODO Auto-generated catch block
  72.             e.printStackTrace();
  73.         }
  74.         currentLine = new StringTokenizer(line, ",");
  75.         for (j = 0; j < 6; j++) {
  76.             quizData[i][j] = Integer.parseInt(currentLine.nextToken());
  77.             // System.out.println(quizData[i][j]);
  78.         }
  79.     }
  80.     try {
  81.         csvReader.close();
  82.     } catch (Exception e) {
  83.         // TODO Auto-generated catch block
  84.         e.printStackTrace();
  85.     }
  86.     for (i = 0; i < noOfQuestions; i++) {
  87.         TextView questionNo = new TextView(this);
  88.         questionNo.setText(String.valueOf(i + 1));
  89.         questionNo.setId(1000 * (i + 1));
  90.         questionNo.setTextSize(18);
  91.         RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(
  92.                 RelativeLayout.LayoutParams.WRAP_CONTENT,
  93.                 RelativeLayout.LayoutParams.WRAP_CONTENT);
  94.         p1.addRule(RelativeLayout.BELOW, previd);
  95.         previd = (1000 * (i + 1));
  96.         questionNo.setLayoutParams(p1);
  97.         r1.addView(questionNo, p1);
  98.         switch (quizData[i][1]) {
  99.         case 1:
  100.         case 2:
  101.             for (j = 0; j < quizData[i][2]; j++) {
  102.                 ImageView option = new ImageView(this);
  103.                 option.setImageResource(questionImages[0][j]);
  104.                 option.setId((1000 * (i + 1)) + j + 1);
  105.                 option.setOnClickListener(this);
  106.                 /*
  107.                  * if (j >= quizData[i][2]) {
  108.                  * option.setVisibility(View.INVISIBLE);
  109.                  * option.setEnabled(false); }
  110.                  */
  111.                 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
  112.                         RelativeLayout.LayoutParams.WRAP_CONTENT,
  113.                         RelativeLayout.LayoutParams.WRAP_CONTENT);
  114.                 params.addRule(RelativeLayout.BELOW, ((1000 * i) + 1));
  115.                 params.addRule(RelativeLayout.RIGHT_OF,
  116.                         ((1000 * (i + 1)) + j));
  117.                 previd = ((1000 * (i + 1)) + j);
  118.                 option.setLayoutParams(params);
  119.                 r1.addView(option, params);
  120.             }
  121.             break;
  122.         case 3:
  123.             for (j = 0; j < (quizData[i][3] == 0 ? quizData[i][2]
  124.                     + quizData[i][3] : quizData[i][2] + quizData[i][3] + 1); j++) {
  125.                 for (k = 10; k > -1; k--) {
  126.                     ImageView num = new ImageView(this);
  127.                     num.setImageResource(questionImages[2][10 - k]);
  128.                     num.setId((1000 * (i + 1)) + (100 * j) + k + 1);
  129.                     num.setOnClickListener(this);
  130.                     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
  131.                             RelativeLayout.LayoutParams.WRAP_CONTENT,
  132.                             RelativeLayout.LayoutParams.WRAP_CONTENT);
  133.                     if (j == 0) {
  134.                         params.addRule(RelativeLayout.RIGHT_OF,
  135.                                 (1000 * (i + 1)));
  136.                     } else {
  137.                         params.addRule(RelativeLayout.RIGHT_OF,
  138.                                 (1000 * (i + 1)) + (100 * (j - 1)) + k + 1);
  139.                     }
  140.                     if (k == 10) {
  141.                         params.addRule(RelativeLayout.BELOW, (1000 * i) + 1);
  142.                     } else {
  143.                         params.addRule(RelativeLayout.BELOW,
  144.                                 ((1000 * (i + 1)) + (100 * j) + k + 2));
  145.                     }
  146.                     num.setLayoutParams(params);
  147.                     r1.addView(num, params);
  148.                 }
  149.             }
  150.             previd = (1000 * (i + 1)) + 1;
  151.             break;
  152.         case 4:
  153.         case 5:
  154.             for (j = quizData[i][2] - 1; j > -1; j--) {
  155.                 for (k = 0; k < quizData[i][3]; k++) {
  156.                     ImageView match = new ImageView(this);
  157.                     match.setImageResource(questionImages[0][k]);
  158.                     match.setId((1000 * (i + 1)) + (100 * j) + k + 1);
  159.                     match.setOnClickListener(this);
  160.                     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
  161.                             RelativeLayout.LayoutParams.WRAP_CONTENT,
  162.                             RelativeLayout.LayoutParams.WRAP_CONTENT);
  163.                     if (k == 0) {
  164.                         params.addRule(RelativeLayout.RIGHT_OF,
  165.                                 (1000 * (i + 1)));
  166.                     } else {
  167.                         params.addRule(RelativeLayout.RIGHT_OF,
  168.                                 (1000 * (i + 1)) + (100 * j) + k);
  169.                     }
  170.                     if (j == quizData[i][2] - 1) {
  171.                         params.addRule(RelativeLayout.BELOW, (1000 * i) + 1);
  172.                     } else {
  173.                         params.addRule(RelativeLayout.BELOW,
  174.                                 (1000 * (i + 1)) + (100 * (j + 1)) + k + 1);
  175.                     }
  176.                     match.setLayoutParams(params);
  177.                     r1.addView(match, params);
  178.                 }
  179.             }
  180.             previd = (1000 * (i + 1)) + 1;
  181.             break;
  182.         }
  183.     }
  184.     s1.addView(r1, new LayoutParams(LayoutParams.FILL_PARENT,
  185.             LayoutParams.FILL_PARENT));
  186.     this.setContentView(s1);
  187. }
  188.  
  189. public void onClick(View v) {
  190.     // TODO Auto-generated method stub
  191.     // Beginning of variable declarations
  192.     int clickedButton = v.getId();
  193.     int questionNo = clickedButton / 1000; // Finding the question number
  194.     int i = 0; // Loop variable
  195.     int rowNo = (clickedButton / 100) % 10;
  196.     // System.out.println(questionNo);
  197.     // System.out.println(quizData[questionNo - 1][1]);
  198.     switch (quizData[questionNo - 1][1]) {
  199.     case 1:
  200.         for (i = 0; i < quizData[questionNo - 1][2]; i++) {
  201.             ImageView option = new ImageView(this);
  202.             option.setImageResource(questionImages[0][i]);
  203.             option.setId((1000 * questionNo) + i + 1);
  204.             option.setOnClickListener(this);
  205.             /*
  206.              * if (i >= quizData[questionNo - 1][2]) {
  207.              * option.setVisibility(View.INVISIBLE);
  208.              * option.setEnabled(false); }
  209.              */
  210.             RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
  211.                     RelativeLayout.LayoutParams.WRAP_CONTENT,
  212.                     RelativeLayout.LayoutParams.WRAP_CONTENT);
  213.             params.addRule(RelativeLayout.BELOW,
  214.                     ((1000 * (questionNo - 1)) + 1));
  215.             params.addRule(RelativeLayout.RIGHT_OF,
  216.                     ((1000 * questionNo) + i));
  217.             option.setLayoutParams(params);
  218.             r2.addView(option, params);
  219.         }
  220.         ImageView option = new ImageView(this);
  221.         option.setImageResource(questionImages[1][(clickedButton % 10) - 1]);
  222.         option.setId((1000 * questionNo) + (clickedButton % 10));
  223.         option.setOnClickListener(this);
  224.         RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
  225.                 RelativeLayout.LayoutParams.WRAP_CONTENT,
  226.                 RelativeLayout.LayoutParams.WRAP_CONTENT);
  227.         params.addRule(RelativeLayout.BELOW,
  228.                 ((1000 * (questionNo - 1)) + 1));
  229.         params.addRule(RelativeLayout.RIGHT_OF, ((1000 * questionNo)
  230.                 + (clickedButton % 10) - 1));
  231.         option.setLayoutParams(params);
  232.         r2.addView(option, params);
  233.         break;
  234.     case 2:
  235.         ImageView checked = new ImageView(this);
  236.         checked.setImageResource(questionImages[1][(clickedButton % 10) - 1]);
  237.         checked.setId((1000 * questionNo) + (clickedButton % 10));
  238.         checked.setOnClickListener(this);
  239.         RelativeLayout.LayoutParams params_checked = new RelativeLayout.LayoutParams(
  240.                 RelativeLayout.LayoutParams.WRAP_CONTENT,
  241.                 RelativeLayout.LayoutParams.WRAP_CONTENT);
  242.         params_checked.addRule(RelativeLayout.BELOW,
  243.                 ((1000 * (questionNo - 1)) + 1));
  244.         params_checked.addRule(RelativeLayout.RIGHT_OF,
  245.                 ((1000 * questionNo) + (clickedButton % 10) - 1));
  246.         checked.setLayoutParams(params_checked);
  247.         r2.addView(checked, params_checked);
  248.         break;
  249.     case 3:
  250.         for (i = 10; i > -1; i--) {
  251.             ImageView num = new ImageView(this);
  252.             num.setImageResource(questionImages[2][10 - i]);
  253.             num.setId((1000 * questionNo) + (100 * rowNo) + i + 1);
  254.             num.setOnClickListener(this);
  255.             RelativeLayout.LayoutParams params_num = new RelativeLayout.LayoutParams(
  256.                     RelativeLayout.LayoutParams.WRAP_CONTENT,
  257.                     RelativeLayout.LayoutParams.WRAP_CONTENT);
  258.             if (rowNo == 0) {
  259.                 params_num.addRule(RelativeLayout.RIGHT_OF,
  260.                         (1000 * questionNo));
  261.             } else {
  262.                 params_num.addRule(RelativeLayout.RIGHT_OF,
  263.                         (1000 * questionNo) + (100 * (rowNo - 1)) + i + 1);
  264.             }
  265.             if (i == 10) {
  266.                 params_num.addRule(RelativeLayout.BELOW,
  267.                         (1000 * (questionNo - 1)) + 1);
  268.             } else {
  269.                 params_num.addRule(RelativeLayout.BELOW,
  270.                         ((1000 * questionNo) + (100 * rowNo) + i + 2));
  271.             }
  272.             num.setLayoutParams(params_num);
  273.             r2.addView(num, params_num);
  274.         }
  275.         ImageView num = new ImageView(this);
  276.         num.setImageResource(questionImages[3][11 - (clickedButton % 100)]);
  277.         num.setId((1000 * questionNo) + (100 * rowNo)
  278.                 + (clickedButton % 100));
  279.         num.setOnClickListener(this);
  280.         RelativeLayout.LayoutParams params_num = new RelativeLayout.LayoutParams(
  281.                 RelativeLayout.LayoutParams.WRAP_CONTENT,
  282.                 RelativeLayout.LayoutParams.WRAP_CONTENT);
  283.         if (rowNo == 0) {
  284.             params_num
  285.                     .addRule(RelativeLayout.RIGHT_OF, (1000 * questionNo));
  286.         } else {
  287.             params_num.addRule(RelativeLayout.RIGHT_OF, (1000 * questionNo)
  288.                     + (100 * (rowNo - 1)) + (clickedButton % 100));
  289.         }
  290.         if (((clickedButton % 100) - 1) == 10) {
  291.             params_num.addRule(RelativeLayout.BELOW,
  292.                     (1000 * (questionNo - 1)) + 1);
  293.         } else {
  294.             params_num.addRule(RelativeLayout.BELOW, ((1000 * questionNo)
  295.                     + (100 * rowNo) + (clickedButton % 100) + 1));
  296.         }
  297.         num.setLayoutParams(params_num);
  298.         r2.addView(num, params_num);
  299.         break;
  300.     case 4:
  301.         for (i = 0; i < quizData[questionNo - 1][3]; i++) {
  302.             ImageView match = new ImageView(this);
  303.             match.setImageResource(questionImages[0][i]);
  304.             match.setId((1000 * questionNo) + (100 * rowNo) + i + 1);
  305.             match.setOnClickListener(this);
  306.             RelativeLayout.LayoutParams params_match = new RelativeLayout.LayoutParams(
  307.                     RelativeLayout.LayoutParams.WRAP_CONTENT,
  308.                     RelativeLayout.LayoutParams.WRAP_CONTENT);
  309.             if (i == 0) {
  310.                 params_match.addRule(RelativeLayout.RIGHT_OF,
  311.                         (1000 * questionNo));
  312.             } else {
  313.                 params_match.addRule(RelativeLayout.RIGHT_OF,
  314.                         (1000 * questionNo) + (100 * rowNo) + i);
  315.             }
  316.             if (rowNo == quizData[questionNo - 1][2] - 1) {
  317.                 params_match.addRule(RelativeLayout.BELOW,
  318.                         (1000 * (questionNo - 1)) + 1);
  319.             } else {
  320.                 params_match.addRule(RelativeLayout.BELOW,
  321.                         (1000 * questionNo) + (100 * (rowNo + 1)) + i + 1);
  322.             }
  323.             match.setLayoutParams(params_match);
  324.             r2.addView(match, params_match);
  325.         }
  326.         ImageView match = new ImageView(this);
  327.         match.setImageResource(questionImages[1][(clickedButton % 10) - 1]);
  328.         match.setId((1000 * questionNo) + (100 * rowNo)
  329.                 + (clickedButton % 10));
  330.         match.setOnClickListener(this);
  331.         RelativeLayout.LayoutParams params_match = new RelativeLayout.LayoutParams(
  332.                 RelativeLayout.LayoutParams.WRAP_CONTENT,
  333.                 RelativeLayout.LayoutParams.WRAP_CONTENT);
  334.         if (((clickedButton % 10) - 1) == 0) {
  335.             params_match.addRule(RelativeLayout.RIGHT_OF,
  336.                     (1000 * questionNo));
  337.         } else {
  338.             params_match.addRule(RelativeLayout.RIGHT_OF,
  339.                     (1000 * questionNo) + (100 * rowNo)
  340.                             + (clickedButton % 10) - 1);
  341.         }
  342.         if (rowNo == quizData[questionNo - 1][2] - 1) {
  343.             params_match.addRule(RelativeLayout.BELOW,
  344.                     (1000 * (questionNo - 1)) + 1);
  345.         } else {
  346.             params_match.addRule(RelativeLayout.BELOW,
  347.                     (1000 * (questionNo)) + (100 * (rowNo + 1))
  348.                             + (clickedButton % 10));
  349.         }
  350.         match.setLayoutParams(params_match);
  351.         r2.addView(match, params_match);
  352.         break;
  353.     case 5:
  354.         ImageView match_checked = new ImageView(this);
  355.         match_checked
  356.                 .setImageResource(questionImages[1][(clickedButton % 10) - 1]);
  357.         match_checked.setId((1000 * questionNo) + (100 * rowNo)
  358.                 + (clickedButton % 10));
  359.         match_checked.setOnClickListener(this);
  360.         RelativeLayout.LayoutParams params_mc = new RelativeLayout.LayoutParams(
  361.                 RelativeLayout.LayoutParams.WRAP_CONTENT,
  362.                 RelativeLayout.LayoutParams.WRAP_CONTENT);
  363.         if (((clickedButton % 10) - 1) == 0) {
  364.             params_mc.addRule(RelativeLayout.RIGHT_OF, 1000 * questionNo);
  365.         } else {
  366.             params_mc.addRule(RelativeLayout.RIGHT_OF, (1000 * questionNo)
  367.                     + (100 * rowNo) + (clickedButton % 10) - 1);
  368.         }
  369.         if (rowNo == quizData[questionNo - 1][2] - 1) {
  370.             params_mc.addRule(RelativeLayout.BELOW,
  371.                     (1000 * (questionNo - 1)) + 1);
  372.         } else {
  373.             params_mc.addRule(RelativeLayout.BELOW, (1000 * (questionNo))
  374.                     + (100 * (rowNo + 1)) + (clickedButton % 10));
  375.         }
  376.         match_checked.setLayoutParams(params_mc);
  377.         r2.addView(match_checked, params_mc);
  378.         System.out.println(match_checked.getDrawable());
  379.         break;
  380.     }
  381. }
  382. }
  383.        
  384. <?xml version="1.0" encoding="utf-8"?>
  385. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  386.  
  387.     <item android:state_checked="true" android:drawable="@drawable/chceked_image"/>
  388.     <item android:drawable="@drawable/unchceked_image"/>
  389.  
  390. </selector>
  391.        
  392. <CheckBox
  393.         android:layout_height="30dp"
  394.         android:layout_width="30dp"
  395.         android:button="@drawable/bt"
  396.         android:focusable="false"
  397.         android:id="@+id/ch"/>
  398.        
  399. ChcekBox ch[] = new CheckBox[3];
  400.  
  401.  layout = findViewById(R.id.main_layout);
  402.  LayoutInflater layoutInflater = LayoutInflater.from(this);
  403.  
  404.  for(int i = 0; i<3; i++)
  405.  {
  406.    View rowView = layoutInflater.inflate(R.layout.row, layout);
  407.    ch[i] = (CheckBox)rowView.findViewById(R.id.ch);
  408.    ch[i].setId(assign_new_id);
  409.  }