Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package org.samtech.myapplication;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.support.annotation.IdRes;
  6. import android.support.annotation.Nullable;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.EditText;
  10. import android.widget.LinearLayout;
  11. import android.widget.RadioButton;
  12. import android.widget.RadioGroup;
  13. import android.widget.Toast;
  14.  
  15. /**
  16. * Created by MiguelS on 23/10/2017.
  17. */
  18.  
  19. public class MyGenActivity extends Activity {
  20.  
  21. @Override
  22. protected void onCreate(@Nullable Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24.  
  25. setContentView(R.layout.dinamicrbuttons);
  26.  
  27.  
  28. final EditText editText=(EditText)findViewById(R.id.et_no);
  29.  
  30. findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
  31.  
  32. @Override
  33. public void onClick(View v) {
  34. int number=Integer.parseInt(editText.getText().toString());
  35. addRadioButtons(number);
  36. }
  37. });
  38. }
  39.  
  40. public void addRadioButtons(int number) {
  41.  
  42. for (int row = 0; row < 1; row++) {
  43. RadioGroup ll = new RadioGroup(this);
  44. ll.setOrientation(LinearLayout.HORIZONTAL);
  45.  
  46. for (int i = 1; i <= number; i++) {
  47. RadioButton rdbtn = new RadioButton(this);
  48. rdbtn.setId((row * 2) + i);
  49. rdbtn.setText("Radio " + rdbtn.getId());
  50. ll.addView(rdbtn);
  51. }
  52. ((ViewGroup) findViewById(R.id.radiogroup)).addView(ll);
  53.  
  54. ll.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  55. @Override
  56. public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
  57. Toast.makeText(MyGenActivity.this, "group : "+radioGroup.toString() +""+"item "+i, Toast.LENGTH_SHORT).show();
  58. }
  59. });
  60. }
  61.  
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement