Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. package com.example.dynamicbutton;
  2. public class MainActivity extends Activity
  3. {
  4. private Button button,button1;
  5. EditText et;
  6. @Override
  7. public void onCreate(Bundle savedInstanceState)
  8. {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. button1 = (Button) findViewById(R.id.addnames);
  12. et = (EditText) findViewById(R.id.editText);
  13. button1.setOnClickListener(new OnClickListener()
  14. {
  15. @Override
  16. public void onClick(View arg0)
  17. {
  18. String theText = et.getText().toString();
  19. Intent intent = new Intent(MainActivity.this,NameActivity.class);
  20. intent.putExtra("text_names", theText);
  21. startActivity(intent);
  22. }
  23. }
  24. );
  25. }
  26.  
  27. }
  28.  
  29. public class SecondActivity extends Activity {
  30.  
  31.  
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34. Intent i = getIntent();
  35. Bundle extras = i.getExtras();
  36. String m = extras.getString("text_label");
  37. int m1=Integer.parseInt(m);
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.dynamically_create_view_element);
  40.  
  41. final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
  42.  
  43. // create the layout params that will be used to define how your
  44. // button will be displayed
  45. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
  46. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  47.  
  48. //Create four
  49. for(int j=1;j<=m1;j++)
  50. {
  51. // Create LinearLayout
  52. LinearLayout ll = new LinearLayout(this);
  53. ll.setOrientation(LinearLayout.HORIZONTAL);
  54.  
  55.  
  56.  
  57.  
  58. // Create Button
  59. final Button btn = new Button(this);
  60. // Give button an ID
  61. btn.setId(j+1);
  62. btn.setText("Add To Cart"+j );
  63. // set the layoutParams on the button
  64. btn.setLayoutParams(params);
  65.  
  66. final int index = j;
  67.  
  68. btn.setOnClickListener(new OnClickListener() {
  69. public void onClick(View v) {
  70.  
  71. Log.i("TAG", "index :" + index);
  72.  
  73. Toast.makeText(getApplicationContext(),
  74. "Clicked Button Index :" + index,
  75. Toast.LENGTH_LONG).show();
  76.  
  77. }
  78. });
  79.  
  80. //Add button to LinearLayout
  81. ll.addView(btn);
  82. //Add button to LinearLayout defined in XML
  83. lm.addView(ll);
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement