radio group not working and vertical alignment android - java

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);
}
}

Related

Why, Both radio buttons selected? i want single selection

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.

Android ArrayList without first value

I have problem with ArrayList. I have the question files structure like:
Y aaaaaa.
Y bbbbbb.
N ccccccc.
N ddddddd.
and I want to put into textview3 the first letter like Y or N
public void value (){
Scanner answerScanner = new Scanner(getResources().openRawResource(R.raw.answer));
Scanner questionScanner = new Scanner(getResources().openRawResource(R.raw.question));
ArrayList<String> answerList = new ArrayList<String>();
ArrayList<String> questionList = new ArrayList<String>();
try {
while (answerScanner.hasNextLine() ) {
answerList.add(answerScanner.nextLine());
questionList.add(questionScanner.nextLine());
}
} finally {
answerScanner.close();
questionScanner.close();
}
int nextInt = random.nextInt(questionList.size());
String answerString = answerList.get(nextInt);
String questionString = questionList.get(nextInt);
yourAnswerString = answerString.substring(0);
yourQuestionString = questionString.substring(2);
shortform = questionString.substring(0,1);
TextView textGenerateNumber = (TextView)findViewById(R.id.textView1);
TextView textGenerateNumber2 = (TextView)findViewById(R.id.textView2);
TextView textGenerateNumber3 = (TextView)findViewById(R.id.textView3);
textGenerateNumber.setText(yourQuestionString);
textGenerateNumber2.setText(yourAnswerString);
textGenerateNumber3.setText(shortform);
}
then I add:
shortform = questionString.substring(0,1);
TextView textGenerateNumber3 = (TextView)findViewById(R.id.textView3);
textGenerateNumber3.setText(shortform);
I get good answers I all cases without first .... when radom get first line from text file I get empty value ( my textView3 are empty).

getting text from dynamically created edittext into a button android java

I have created dynamically edittext on my android application like below:
protected void onCreate(Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) findViewById(R.id.linearLayout1);
final EditText[] Et = new EditText[10];
int prevTextViewId = 0;
int add = 0;
int add1 = 0;
for (int a = 0; a < Arr.length; a++) {
Et[a] = new EditText(this);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
(int) LayoutParams.WRAP_CONTENT,
(int) LayoutParams.WRAP_CONTENT);
param.leftMargin = 25;
param.topMargin = (a + 1) * (100 + add1);
Et[a].setPadding(5, 23, 5, 5);
Et[a].setLayoutParams(param);
Et[a].setSingleLine(false);
Et[a].setHorizontalScrollBarEnabled(false);
Et[a].setWidth(280);
Et[a].getText().toString();
layout.addView(Et[a]);
add = add + 25;
add1 = add1 + 15;
}
}
And I want to get text from dynamically created edittext into a button like below:
btn_doneAnswer = (Button) findViewById(R.id.btn_doneAnswer);
btn_doneAnswer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int a = 0; a < Arr.length; a++) {
Et[a] = new EditText(this);
String text += Et[a].getText().toString();
}
Bundle bundle = new Bundle();
Intent i_backToAddItem = new Intent(
AnswerActivityMultiText.this,
QuestionActivityDynamic.class);
bundle.putString("text", text);
bundle.putString("MultiQues", MultiQues);
i_backToAddItem.putExtras(bundle);
startActivity(i_backToAddItem);
}
});
But I am getting error below in the button onclick method at this line Et[a] = new EditText(this);:
The constructor EditText(new View.OnClickListener(){}) is undefined
another below error at this line += in the button onclick method:
Syntax error on token "+=", = expected
Kindly suggest me how I am getting text from dynamically created edittext into a button.
Waiting for reply.
Thanks
you dont have to initialize the EditText again in the onClick. You should directly use the EditText object to get the text. So the line
Et[a] = new EditText(this);
is not required at all!
And for the error with '+=' operator, you are declaring the string and using the operator(+=) in one line. you have to split up the the declaration part out of the for loop and then use '+='. Here's the complete code :
btn_doneAnswer = (Button) findViewById(R.id.btn_doneAnswer);
btn_doneAnswer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String text;
for (int a = 0; a < Arr.length; a++) {
text += Et[a].getText().toString();
}
Bundle bundle = new Bundle();
Intent i_backToAddItem = new Intent(
AnswerActivityMultiText.this,
QuestionActivityDynamic.class);
bundle.putString("text", text);
bundle.putString("MultiQues", MultiQues);
i_backToAddItem.putExtras(bundle);
startActivity(i_backToAddItem);
}
});
Your problem is in this lines:
for (int a = 0; a < Arr.length; a++) {
Et[a] = new EditText(this);
String text += Et[a].getText().toString();
}
In this line:
Et[a] = new EditText(this);
this is the onClickListener object, you must have class.this (where class is the class name of file that this code is created).
In this other line:
String text += Et[a].getText().toString();
You always create a new String, and instead of created, you make an add. It is a Java concept fail.
You must do this:
String text = "";
for (int a = 0; a < Arr.length; a++) {
Et[a] = new EditText(class_name.this);
text += Et[a].getText().toString();
}

How to set my textview to display all elements in my json array

parsing json and i'm trying to setText a textview to do all elements in my array. Obviously I am doing something wrong here, would appreciate any help.
TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
try {
String buildings = getJSON("http://iam.colum.edu/portfolio/api/course?json=True");
//JSONObject jsonObject = new JSONObject(buildings);
JSONArray queryArray = new JSONArray(buildings);
//queryArray = queryArray.getJSONArray(0);
List<String> list = new ArrayList<String>();
for (int i=0; i<queryArray.length(); i++) {
list.add( queryArray.getString(i) );
}
String text = list.get(0).toString();
String arr[] = text.split(" ");
arr[0] = arr[0].trim();
for(int i=0;i>queryArray.length();++i)
{
tv1.setText(list[i]);
}
you have an infinite loop here ;
for(int i=0;i>queryArray.length();++i)
{
tv1.setText(list[i]);
}
Also, the above snippet displays one line from your list on the textview each time the loop is running. It doesn't append the text on the textview. Try to optimize your code a bit, that you can do the task in one loop.
StringBuilder sb = new StringBuilder();
for (int i=0; i<queryArray.length(); i++) {
// chain each string, separated with a new line
sb.append(queryArray.getString(i) + "\n");
}
// display the content on textview
tv1.setText(sb.toString());
ref : StringBuilder - Android documentation
Use this dude :) u didnt concat all string just do with this
TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
try {
String buildings = getJSON("http://iam.colum.edu/portfolio/api/course?json=True");
//JSONObject jsonObject = new JSONObject(buildings);
JSONArray queryArray = new JSONArray(buildings);
//queryArray = queryArray.getJSONArray(0);
List<String> list = new ArrayList<String>();
for (int i=0; i<queryArray.length(); i++) {
list.add( queryArray.getString(i) );
}
String finaltext="";
for(int i=0;i>list.size();i++)
{
finaltext.concat(list.get(i));
}
tv1.setText(finaltext.trim());

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