Why, Both radio buttons selected? i want single selection - java

I'm creating a radio question here I want select only one button to be selected. But here both buttons are got selected. kindly give me the solutions.
private void radioButtonQuestion(String radioquest, JsonArray optionsArray) {
RadioButton radio;
RadioGroup radioGroup;
JsonArray dependenciesArray = (JsonArray) indQuestions.get("dependencies");
ArrayList<String> list = new ArrayList<>();
for(int a=0; a<optionsArray.size();a++) {
JsonObject optionsObject = (JsonObject) optionsArray.get(a);
JsonObject dependenciesObject = (JsonObject) dependenciesArray.get(a);
String option = optionsObject.get("value").getAsString();
list.add(option);
}
LinearLayout ll=new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);
cardview = new CardView(context);
layoutparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutparams.setMargins(10,20,10,20);
cardview.setLayoutParams(layoutparams);
cardview.setRadius(30);
cardview.setPadding(10, 10, 10, 10);
cardview.setCardBackgroundColor(Color.WHITE);
cardview.setMaxCardElevation(20);
cardview.setMaxCardElevation(6);
textview = new TextView(context);
// textview.setLayoutParams(layoutparams);
textview.setText(radioquest);
textview.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
textview.setTextColor(Color.BLACK);
textview.setPadding(15, 25, 25, 15);
textview.setGravity(Gravity.NO_GRAVITY);
ll.addView(textview);
for(int i = 0; i< list.size();i++) {
radio = new RadioButton(context);
radioGroup = new RadioGroup(context);
radio.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
radio.setPadding(15, 15, 15, 15);
radio.setId(i);
radio.setText(list.get(i));
radioGroup.addView(radio);
ll.addView(radioGroup);
}
ll.setId(id);
cardview.addView(ll);
cardview.setId(id);
cardview.setTag("Radio "+id);
id++;
relativeLayout.addView(cardview);
}

Your code created a different group for each individual radio button, that's why each can be selected at the same time. Each group can have one selected radio button. Instead, you should create just one group, and add all the buttons to that group.

Just place radioGroup before options like below
radioGroup = new RadioGroup(context);
for(int i = 0; i< list.size();i++) {
radio = new RadioButton(context);
radio.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
radio.setPadding(15, 15, 15, 15);
radio.setId(i);
radio.setText(list.get(i));
radioGroup.addView(radio);
ll.addView(radioGroup);
}

From the reference from your code, I am able to find the cause which produces this issue, You need to put this line(ll.addView(radioGroup)) out of for loop as below :
//...
for(int i = 0; i< list.size();i++) {
radio = new RadioButton(context);
radioGroup = new RadioGroup(context);
radio.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
radio.setPadding(15, 15, 15, 15);
radio.setId(i);
radio.setText(list.get(i));
radioGroup.addView(radio);
}
ll.addView(radioGroup);
//...
If this solution helps then please accept this answer.

Related

JavaFX Input from Buttons

I'm creating a simple ATM machine GUI using javaFX but I can't seem to know how to take an input from a button. For instance, I created a virtual number pad but I don't know how to take input from clicked buttons.
public static void displayNumbers() {
Stage window2 = new Stage();
GridPane grid = new GridPane();
window2.setTitle("Enter Value");
Button one = new Button("1");
Button two = new Button("2");
Button three = new Button("3");
Button four = new Button("4");
Button five = new Button("5");
Button six = new Button("6");
Button seven = new Button("7");
Button eight = new Button("8");
Button nine = new Button("9");
Button zero = new Button("0");
Button enterButton = new Button("Enter");
grid.add(one, 0, 0);
grid.add(two, 1, 0);
grid.add(three, 2, 0);
grid.add(four, 0, 1);
grid.add(five, 1, 1);
grid.add(six, 2, 1);
grid.add(seven, 0, 2);
grid.add(eight, 1, 2);
grid.add(nine, 2, 2);
grid.add(zero, 1, 3);
grid.add(enterButton, 2, 4);
}
To make a Button do something in JavaFX, you need to add an EventHandler. For example, let's tell button one to print something out to console:
one.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("You just clicked Button #1!");
}
});
This can be simplified with a lambda expression in JDK8+:
one.setOnAction(event -> System.out.println("You just clicked Button #1!"));
If you need to execute more than one line of code when clicking the button, just use a code block instead:
one.setOnAction(event -> {
// Do lots of things
});

How do I set margins for my Android app table?

I am trying a add rows to my table by pressing a button, I can add the rows but I don't know how to add layout to the added row.
Here is my code:
public void testRow(View view) {
// get a reference for the TableLayout
TableLayout table = (TableLayout) findViewById(R.id.accountTable);
// create a new TableRow
TableRow row = new TableRow(this);
TextView x = new TextView(this);
x.setText("text ");
row.addView(x);
TextView y = new TextView(this);
y.setText("text ");
row.addView(y);
TextView z = new TextView(this);
z.setText("text ");
row.addView(z);
// add the TableRow to the TableLayout
table.addView(row);
}
You should add TableRow to TableLayout with LayoutParams, then you can also set the margins with it:
LayoutParams params = table.generateDefaultLayoutParams();
params.leftMargin = ...
params.rightMargin = ...
table.addView(row, params);

radio group not working and vertical alignment android

JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());
for (int iii = 0; iii < jsonQuestion.length(); iii++) {
LinearLayout lll = new LinearLayout(Questionnaire.this);
lll.setOrientation(LinearLayout.VERTICAL);
JSONObject obj2 = (JSONObject) jsonQuestion.get(iii);
System.out.println(obj2.get("question"));
TextView tv = new TextView(Questionnaire.this);
tv.setText(obj2.get("question").toString());
lll.addView(tv);
JSONArray jsonAnswer = new JSONArray(obj.get("answer").toString());
Log.d("Reading Answer: ", jsonAnswer + "");
for (int iiii = 0; iiii < jsonAnswer.length(); iiii++) {
JSONObject obj3 = (JSONObject) jsonAnswer.get(iiii);
System.out.println(obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString()));
if (obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString())) {
TextView tv1 = new TextView(Questionnaire.this);
tv1.setText(obj3.get("inputname").toString());
RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL
if (obj3.get("inputtype").toString().matches("radio")) {
RadioButton newRadioButton = new RadioButton(Questionnaire.this);
newRadioButton.setId(Integer.parseInt(obj3.get("answerid").toString()));
newRadioButton.setText(obj3.get("inputname").toString());
group.addView(newRadioButton);
lll.addView(group);
}
}
}
ll.addView(lll);
}
This is my code for dynamically adding radio button.
Problem is when I select one option then select anther it does not deselect the previous one. Why doesn't it deselect the the first one selecting another when they are both added to same radio group. Also it is not placing the radio buttons next to each other but on top of each other when the orientation is set to horizontal
UPDATE
I moved my declaration of
RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL
out side parent for loop now i am getting
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
How should i deal with it?
you are added only one RadioButton in RadioGroup that's why you got many selected radio button. RadioGroup need minimum 2 RadioButton for check or Uncheck effect.
try this,I hope it may work.
JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());
for (int iii = 0; iii < jsonQuestion.length(); iii++) {
LinearLayout lll = new LinearLayout(Questionnaire.this);
lll.setOrientation(LinearLayout.VERTICAL);
JSONObject obj2 = (JSONObject) jsonQuestion.get(iii);
System.out.println(obj2.get("question"));
TextView tv = new TextView(Questionnaire.this);
tv.setText(obj2.get("question").toString());
lll.addView(tv);
JSONArray jsonAnswer = new JSONArray(obj.get("answer").toString());
Log.d("Reading Answer: ", jsonAnswer + "");
RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL
for (int iiii = 0; iiii < jsonAnswer.length(); iiii++) {
JSONObject obj3 = (JSONObject) jsonAnswer.get(iiii);
System.out.println(obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString()));
if (obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString())) {
TextView tv1 = new TextView(Questionnaire.this);
tv1.setText(obj3.get("inputname").toString());
if (obj3.get("inputtype").toString().matches("radio")) {
RadioButton newRadioButton = new RadioButton(Questionnaire.this);
newRadioButton.setId(Integer.parseInt(obj3.get("answerid").toString()));
newRadioButton.setText(obj3.get("inputname").toString());
group.addView(newRadioButton);
}
}
}
lll.addView(group);
ll.addView(lll);
}
try like this i have make code as you want.
ScrollView scrollView = (ScrollView) findViewById(R.id.templayout);
LinearLayout mLinearLayout = new LinearLayout(this);
mLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(mLinearLayout);
JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());
for (int iii = 0; iii < jsonQuestion.length(); iii++) {
JSONObject obj2 = (JSONObject) jsonQuestion.get(iii);
System.out.println(obj2.get("question"));
TextView tv = new TextView(Questionnaire.this);
tv.setText(obj2.get("question").toString());
mLinearLayout.addView(tv);
JSONArray jsonAnswer = new JSONArray(obj.get("answer").toString());
Log.d("Reading Answer: ", jsonAnswer + "");
RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL
for (int iiii = 0; iiii < jsonAnswer.length(); iiii++) {
JSONObject obj3 = (JSONObject) jsonAnswer.get(iiii);
System.out.println(obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString()));
if (obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString())) {
TextView tv1 = new TextView(Questionnaire.this);
tv1.setText(obj3.get("inputname").toString());
if (obj3.get("inputtype").toString().matches("radio")) {
RadioButton newRadioButton = new RadioButton(Questionnaire.this);
newRadioButton.setId(Integer.parseInt(obj3.get("answerid").toString()));
newRadioButton.setText(obj3.get("inputname").toString());
group.addView(newRadioButton);
}
}
}
mLinearLayout.addView(group);
}
}

JTabbedPane created dynamically

I have a problem when I try to crete dynamically Tabs on a JTabbePane by a for. The problem is that I don't know how to access the contents when an event happens.
I will try to show you part of the code to be easy to understand.
conteudoT = new JTabbedPane(JTabbedPane.TOP);
conteudoT.setBounds(5, 19, 477, 232);
for (int i = 0; i < players; i++) {
conteudo = new JPanel();
conteudo.setLayout(null);
Details = new JPanel();
Details.setBounds(15, 11, 307, 183);
Details.setVisible(false);
Details.setName("Details" + i);
conteudo.add(Details);
btnR = new JButton("r");
btnR.addActionListener(this);
conteudoT.addTab("Jogador " + (i + 1), conteudo);
Details.setLayout(new GridLayout(2, 1, 0, 0));
...
contentPane.add(conteudoT);
}
#Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < players; i++) {
if (e.getSource()==btnR) {
Details.setVisible(true);
}
}
}
What happens here is that the painel Details is added on the last Tab to be constructed and I would like to add it on the Tab that the event happend.
use :
conteudoT.indexOfTab(string)
to get the index. Then use :
conteudoT.getTabComponentAt(int index)
to get the component

Dynamically added views do not reset?

I have a LinearLayout in which I am adding some CheckBoxes dynamically, they are adding perfectly well on first call, when I am calling the same method with different values, then they are not appearing in that LinearLayout, old values are not getting replaced with new values.
I checked values in LogCat and they seem fine, whatever I'm passing is being displayed in LogCat but not appearing in LinearLayout.
Here's my method that adds the view:
private void setOptions(int questionID2) {
ArrayList<String> options = pustakDB.getOptions(questionID2);
Log.e("optionsInAct", options.toString() + " size " + options.size());
OptionView = new LinearLayout(this);
OptionView.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, img.getId());
p.addRule(RelativeLayout.RIGHT_OF, questionNumber.getId());
rel.addView(OptionView, p);
int i = 0;
while (i < options.size()) {
CheckBox c = new CheckBox(this);
c.setText(options.get(i));
c.setId(i);
OptionView.addView(c);
i++;
}
}
Edit: "rel" here is RelativeLayout.
// put this somewhere else
OptionView = new LinearLayout(this);
OptionView.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, img.getId());
p.addRule(RelativeLayout.RIGHT_OF, questionNumber.getId());
rel.addView(OptionView, p);
private void setOptions(int questionID2) {
ArrayList<String> options = pustakDB.getOptions(questionID2);
Log.e("optionsInAct", options.toString() + " size " + options.size());
// this will remove all views from OptionView
OptionView.removeAllViews();
int i = 0;
while (i < options.size()) {
CheckBox c = new CheckBox(this);
c.setText(options.get(i));
c.setId(i);
OptionView.addView(c);
i++;
}
}
Try this and see if it works.

Categories