Advertisement
Magwar

programmatic layout

Jul 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. public class AndroidWalkthroughApp1 extends Activity implements View.OnClickListener {
  2. final int TOP_ID = 3;
  3. final int BOTTOM_ID = 4;
  4. /** Called when the activity is first created. */
  5. @Override
  6. public void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.main);
  9. RelativeLayout top = new RelativeLayout(this);
  10. top.setId(TOP_ID);
  11. RelativeLayout bottom = new RelativeLayout(this);
  12. bottom.setId(BOTTOM_ID);
  13. for (int i = 0; i < 2; i++) {
  14. Button button = new Button(this);
  15. button.setText("Button " + i);
  16. button.setId(i);
  17. button.setOnClickListener(this);
  18. add generated button to view
  19. if (i == 0) {
  20. top.addView(button);
  21. } else {
  22. bottom.addView(button);
  23. }
  24. }
  25. RelativeLayout root = (RelativeLayout) findViewById(R.id.root_layout);
  26. LinearLayout root = (LinearLayout) this.findViewById(R.id.root_layout);
  27. root.addView(top);
  28. root.addView(bottom);
  29. }
  30. @Override
  31. public void onClick(View v) {
  32. Toast toast = Toast.makeText(AndroidWalkthroughApp1.this, "You clicked button " + v.getId(), Toast.LENGTH_LONG);
  33. toast.show();
  34. RelativeLayout parentLayout = (RelativeLayout) v.getParent();
  35. parentLayout.removeView(v);
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement