Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.samtech.myapplication;
- import android.app.Activity;
- import android.os.Bundle;
- import android.support.annotation.IdRes;
- import android.support.annotation.Nullable;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- /**
- * Created by MiguelS on 23/10/2017.
- */
- public class MyGenActivity extends Activity {
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.dinamicrbuttons);
- final EditText editText=(EditText)findViewById(R.id.et_no);
- findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- int number=Integer.parseInt(editText.getText().toString());
- addRadioButtons(number);
- }
- });
- }
- public void addRadioButtons(int number) {
- for (int row = 0; row < 1; row++) {
- RadioGroup ll = new RadioGroup(this);
- ll.setOrientation(LinearLayout.HORIZONTAL);
- for (int i = 1; i <= number; i++) {
- RadioButton rdbtn = new RadioButton(this);
- rdbtn.setId((row * 2) + i);
- rdbtn.setText("Radio " + rdbtn.getId());
- ll.addView(rdbtn);
- }
- ((ViewGroup) findViewById(R.id.radiogroup)).addView(ll);
- ll.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
- Toast.makeText(MyGenActivity.this, "group : "+radioGroup.toString() +""+"item "+i, Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement