The string answers_log is the result of a database query. So, when i loop trough the results, it generates 4 radio groups. When it try to set all radio groups enable, the radioGroup.getChildAt(j).setEnabled(false); is applied only to the last radio group. What can i do, to set all radio groups enable?
Here is my code:
radioGroup = new RadioGroup(this);
for (Answer an : answers) {
String answers_log = " " + an.getAnswer();
answer = new RadioButton(this);
answer.setText(answers_log);
radioGroup.addView(answer+log);
}
linearLayout.addView(radioGroup);
finishButton = new Button(this);
linearLayout.addView(finishButton);
finishButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < 4; i++) {
radioGroup.getChildAt(i).setEnabled(false);
}
}
});
Thanks!
Using a RadioGroup do not allow you to select multiple RadioButtons at same time. You should add 4 radio buttons without RadioGroup.
Related
This question already has answers here:
How to add button tint programmatically
(22 answers)
Closed 1 year ago.
I have dynamically created buttons. I just want change the background color of the button which I have clicked. For example, Initially all buttons should have grey background color. If I clicked a button then clicked button background color should changed to red and other buttons background color should be in grey.
Here I have tried a bit
for (int i = 0; i < 5; i++) {
Button myBtn = new Button(ProductDetailsActivity.this);
myBtn.setText("My Button"+i);
myBtn.setBackGroundColor(Color.parseColor("#cccccc"));
myBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myBtn.setBackGroundColor(Color.parseColor("#ff0000"));
}
});
}
Your code seems legit.
But instead of
myBtn.setBackGroundColor(Color.parseColor("#ff0000"));
You can use
myBtn.setBackGroundColor(Color.RED);
I think you want to change all buttons' colors dynamically according to which one is clicked. And want to turn others to gray.
So to achieve that you need to keep buttons in an array. Then by reaching others index you can modify them. You can get the index from the end of the buttonText String and parse it to integer.
Button[] buttonArray = new Button[4];
for (int i = 0; i < 5; i++) {
Button myBtn = new Button(ProductDetailsActivity.this);
myBtn.setText("My Button"+i);
myBtn.setBackGroundColor(Color.GREY);
myBtn.setId(new Random().nextInt());
myBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for(int j=0; j<buttonArray.lenght;j++){
if(view.getId() == buttonArray[j].getId()){
buttonArray[j].setBackgroundColor(Color.RED);
}else{
buttonArray[j].setBackgroundColor(Color.GREY);
}
}
}
});
buttonArray[i]=myBtn;
}
So the main idea is to keep all buttons in an array. If one of them clicked, get that one's index make it's background's red. And turn other buttons'(which is not that index) background grey again.
I'm working on a project in Android Studio where I have one EditText where the user will insert one word at a time, 10 times. Everytime the user writes a new input and clicks on the button it goes to a different TextView than the previous ones and different from the next ones.
How can I put the different inputs into the specific (different) TextViews?
Every TextView has a different sequencial ID like, word1, word2, etc.
I haven't done java in a long time, so I'm having problems with logic. I tried to do the following but the app crashes.
gameword = (EditText) findViewById(R.id.wordj);
public void onClick(View v) {
printwords(gameword.getText().toString());
}
});
public void printwords(String word) {
String[] array = new String[10];
TextView[] positions = new TextView[10];
for (int i=0; i < 10; i++){
array[i] = word;
positions[i].setText(array[i]);
}
}
}
You're creating a new array of TextView's and String every time the button is clicked. Change your code to the code below and it should work.
Make your int[] textViews , String[] array & int i = 0 class variables and then initialize them in onCreate() after setContentView()
In above code, int[] textViews is the array of ID's of TextView's from your activity.
After doing that, change your code to following:
gameword = (EditText) findViewById(R.id.wordj);
public void onClick(View v) {
array[i] = gameword.getText().toString();
YourActivity.this.findViewById(textViews[i]).setText(array[i]);
i++;
}
});
I have built a UI with 4 radio buttons, and then grouped them together in a ButtonGroup. I get the index of the button selected, and store it in a DB. But when the form is displayed back, i am required to show which button was initially selected. The following is the code i get to save the index value.
Enumeration<AbstractButton> elements = ProdType.getElements();
while (elements.hasMoreElements()) {
AbstractButton button = (AbstractButton) elements.nextElement();
if (button.isSelected()) {
if (button.getText().equals("ZERO"))
strProdType = "0";
else if (button.getText().equals("ONE"))
strProdType = "1";
else if (button.getText().equals("TWO"))
strProdType = "2";
else if (button.getText().equals("THREE"))
strProdType = "3";
else if (button.getText().equals("FOUR"))
strProdType = "4";
}
}
I made few changes to the code shown above, and it now works perfect. I am storing it as String value, and then using the same string value converted to integer while displaying. Some trouble faced cos of RadioButtons and the ButtonGroup. But quite a learning.
Enumeration<AbstractButton> elements = MyType.getElements();
while (elements.hasMoreElements()) {
AbstractButton button = elements.nextElement();
if (button.isSelected()) {
strMyTypeVal = Integer.toString(i);
}
i = i + 1;
}
I have a textView set up on my main activity, and a button. When I click the button, I'd like the textView to start updating it's value based on the code below. However, this doesn't work and the problem is the loop. Can someone explain why? I am new to Java and Android Development
button2 = (Button) findViewById(R.id.button);
button2.setOnClickListener(new OnClickListener() {
TextView textView = (TextView)findViewById(R.id.refView);
public void onClick(View arg0) {
for(i=1;i<1;i++){
i = i + 1;
textView.setText(String.valueOf(i)+"hello");
}
}
});
Thank You
Try this:
TextView textView = (TextView)findViewById(R.id.refView);
button2.setOnClickListener(new View.OnClickListener() {
int i = 0;
public void onClick(View arg0) {
i = i + 1;
textView.setText(String.valueOf(i)+"hello");
}
});
Your for loop conditions were wrong.
for(i=1;i<1;i++) won't even start, because 1<1 is already met.
Initiate count variable i before onClick and then update it before click and set new text with updated i.
Not sure what exactly you want to happen. But, you can get rid of this line
i = i + 1;
because the i++ already increments i by 1 with each iteration of the for loop.
Second, since i starts off at 1 and you want the loop to run while i<1, it will never enter the loop. It is never less than 1.
Third, if the conditions were different, say
for (int i=0; i<10; i++)
it will run through the loop so fast that you won't even recognize a change.
I have a code that generates an array of radio buttons and I want to know how to assign them in one group so the other radio buttons won't be enabled if another is enabled.
This here is my code for the generation of the said radio buttons.
for (int i = 0; i < 20; i++)
{
RadioButton myRadio = new RadioButton(this);//use array
myRadio.setId(i);
final int id_ = myRadio.getId();
myRadio.setText("button " + id_);
LinearLayout li = (LinearLayout)findViewById(R.id.buttonlayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
li.addView(myRadio, lp);
btn = (RadioButton) findViewById(id_);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Button clicked index = " + id_, Toast.LENGTH_SHORT).show();
}
});
}
btn is a button declared outside the function. It is also a radio button
Try this code sample
final RadioButton[] rb = new RadioButton[20];
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.VERTICAL);
for(int i=0; i<20; i++){
rb[i] = new RadioButton(this);
rg.addView(rb[i]);
rb[i].setText(" " + ContactsActivity.phonetype.get(i)
+ " " + ContactsActivity.phone.get(i));
rb[i].setId(i + 100);
}
ll.addView(rg);//you add the whole RadioGroup to the layout
Update - Answer for the error you mentioned in your comments
To resolve your error, find out, using the line-number in the error, which line produces the error. Look at what you are adding there
Check all your addView calls. (hint: there is a line-number in that error somewhere. use it)
To try to answer your question in the comment, you must follow these rules;
Never add any view more then once.
When a View is already used (e.g., you got it with findViewById, don't use addView on it.
When you want to add a view, use addView with a NEW view.
You can add several of these new views to one view, but you cannot add that one view multiple times.